Best Practices for Schema Management, Migrations, and Scaling in MongoDB

Hi MongoDB Community,

I’m currently working on a JavaScript-based project using MongoDB as our database, and I have a few concerns regarding schema management, migrations, and cluster operations. I’d really appreciate any insights or recommendations on the following topics:

  1. Maintaining Schema Change History
    How can I effectively maintain the history of schema changes for my collections in my code repository? For example, in relational databases, we use tools like Liquibase or Flyway to manage schema versioning. Is there an equivalent tool or best practice for MongoDB that integrates well with version control (Git)?

  2. Tools for Schema Change Management
    Are there specific tools available that can help track schema changes or migrations for MongoDB collections? I’ve come across options like migrate-mongo and mongobee, but I’d love to know if there are better solutions or if these are widely recommended.

  3. Managing Property Changes
    If I need to change a field’s structure, such as converting address (an object) to addresses (an array of objects), what’s the best approach?

  • Should I run a migration to transform the existing documents and then start reading from the new property immediately?
  • This feels risky, especially for large collections. Running migrations during these scenarios might be impractical, and I’d also need to run another migration to remove the old field afterward. Is there a more efficient or safer way to handle such updates?
  1. M20 Cluster Deadlock Issue During Index Creation
    Recently, I ran into a serious issue when adding an index to a collection. The cluster experienced a sudden spike in CPU usage, triggering scaling. However, the scaling process got stuck, and the Atlas portal became unresponsive for a while. This led to connection timeouts in my application, causing downtime in the production system.
  • At the time, the collection had only around 20,000 documents, which makes me worry about how such operations would behave as the collection grows further.
  • Is there a recommended way to handle such scenarios?
  • Are there specific best practices for adding indexes to large collections in a production cluster to minimize downtime?
  • Could I have done something differently to prevent the deadlock and scaling delay?

I’m looking forward to hearing your suggestions and experiences. Thanks in advance for your help!