Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB mongorestore and existing collection with records

I need to import (restore) a collection generated with mongodump into an existing database and I'd like the records to be merged into the existing collection.

Does mongorestore merge the records in the same collection or it will drop the existing collection before restoring the records?

like image 366
Simone Carletti Avatar asked Oct 26 '10 08:10

Simone Carletti


People also ask

Does Mongorestore overwrite existing data?

However, mongorestore performs inserts only and does not perform updates. That is, if restoring documents to an existing database and collection and existing documents have the same value _id field as the to-be-restored documents, mongorestore will not overwrite those documents.

Does Mongodump overwrite?

Overwrite Filesmongodump overwrites output files if they exist in the backup data folder. Before running the mongodump command multiple times, either ensure that you no longer need the files in the output folder (the default is the dump/ folder) or rename the folders or files.

How do I restore a specific collection in MongoDB?

Run the MongoDB mongodump command to create a dump file of any specific database or collection. Run the mongorestore command to restore the dump file into any MongoDB instance. Choose either the original MongoDB instance that the backup was created for, or any alternative instance.

What is the difference between Mongoexport and Mongodump?

mongoexport is a command-line tool that produces a JSON or CSV export of data stored in a MongoDB instance. mongodump is a utility for creating a binary export of the contents of a database.


1 Answers

mongorestore will only drop the existing collection if you use the --drop argument.

If you don't use --drop, all documents will be inserted into the existing collection, unless a document with the same _id already exists. Documents with the same _id will be skipped, they are not merged. So mongorestore will never delete or modify any of the existing data by default.

like image 96
Niels van der Rest Avatar answered Oct 18 '22 21:10

Niels van der Rest