There are three general strategies that you can use to modify schemas in MongoDB. I saw that all three work well; which you will use depends on your specific use case.
First: you can simply add a new field to new documents and write your code to handle the case when this field does not exist. For example, you can add the " address " field to your "user" documents, but you will have to write a client code to handle the case when this field does not exist.
Secondly: you can write your code to look at an existing document and update it when it sees a "old style" document. For example, you might have code that checks to see if there is a " name " field in your "user" document. If he finds this field, he will divide it into the fields " first_name " and " sur_name ", the $unset field " name " in this document and $set new " first_name ", and " sur_name " to their calculated values.
Third: you can batch update all documents in the collection to use the new scheme. You should write the same code as above, but instead of lazily applying it when your application reads a document, you should apply it to all documents in the collection.
Please note that this last strategy may have a performance impact: if you download a lot of documents that have not been available for a while, you will add extra workload to your MongoDB system.
source share