Search code examples
mongodbmongoimportmongodump

Importing mongo collection into existing collection without overriding it


Is it possible to import a mongodump into a existing collection adding to it (not overriding it)? If yes, how?

Would it be using mongoimport --db mydb?

The collections are the same in structure but simple have different datasets from different instances of the same app. I'd like to merge all data into a single database.


Solution

  • Use mongorestore.

    It is overwriting the collection only if you specify --drop parameter. So if you do nothing it will just add new element (thus doing what you want).

    The only problem might arise if you have the same _id elements, in which case it will just ignore them.

    If you restore to an existing database, mongorestore will only insert into the existing database, and does not perform updates of any kind. If existing documents have the same value _id field in the target database and collection, mongorestore will not overwrite those documents.