0

Do any pattern exists for renaming a MongoDB collection live in production environment? The collection is accessed both for reading and for writing in a non sharded environment. The requirement is to not have downtime. Some different microservices access the collection in different ways. The result must be obtained in a progressive way, releasing updated services independently from each other.

For relational databases several patterns were discovered and documented in the excellent book Refactoring Databases: Evolutionary Database Design by Scott Ambler and Pramod Sadalage and I would have used the Rename Table pattern to implement if the database was relational, that but it seems that with MongoDB this is not possible.

1 Answer 1

0

Usually you can simply rename a collection. If an application writes into a collection and such collection does not exists (because it was renamed), then a new collection is created automatically. Please note, apart from _id_ this new collection does not have any indexes, you may create them manually.

For reading, you have to tell your application the new name, otherwise it reads from the old collection.

1
  • Thanks for your answer. The problem is that the reading processes should fetch data from two different collections. This could be feasible but I was looking for a more pattern-like answer.
    – mxb
    Commented Jun 8, 2021 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.