Move Mongodb Dev Changes to Running Production MongoDB


I am new with monogodb. My Question is how we can move local mongo db changes to production mongodb without loosing data. For Instance I have already running monogodb which my client is using 24/7, Now I have added two more Fields in user collection, added and removed indexing on different collection e.t.c. Now I want to apply the changes to production db.

Is it a good pratice

  1. to put site under maintance and copy prod db , apply changes local and replace with production db.
    2) is there any CI/CD pipelines we can use to automate db changes like we can do for sql server.
    3) via Script
    4) check and apply indexing inside web api code , so no need to apply changes to different environment.-- is it agood way
    5) Any other good way

Mongodb is running in container.


i donot know it’s the right channel to ask but any help will higly appericated.

MongoDB uses flexible schema, meaning you can change fields freely. unless you need strict schema in your API as many frameworks need that. you need versioning if you change too many things.

if those two fields (or any new field) are not mandatory inputs (meaning they can be null), then you don’t need any change in the production database. that is the flexibility of document databases.

otherwise, you need to add a versioning field to your new schema (model) as well as a second schema to process old documents. this introduces extra (inescapable) work to your application as you have to work with 2 schemas every time you read a document. you should also start a migration logic about old documents because at some point you will have many versions to deal with any time you add a mandatory field.

index contents are based on the current documents in the database and changed with each insert/delete/update operation. so even if they use the same keys, production indexes are different than your development indexes because documents are different. to keep your customer’s data intact, you need to script your way for removing/adding indexes.