ChatGPT解决这个技术问题 Extra ChatGPT

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?


N
Niels van der Rest

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.


is the mongodb actually being dropped with using option --drop? In mine case, on restore, i can see the previous records that was added after taking the dump. can you solve my issue here stackoverflow.com/questions/22424347/…
Mongorestore ref for the answer above: docs.mongodb.org/manual/reference/program/mongorestore/…
@codeofnode from the documentation: --drop does not drop collections that are not in the backup.. You should verify that the collection you expect to be dropped is indeed present in the backup.