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.


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.

  • 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
    Jun 8 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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