mongorestore dropping collections using -drop option

10,771

Solution 1

No, actually, according to the official documentation :

--drop Before restoring the collections from the dumped backup, drops the collections from the target database. --drop does not drop collections that are not in the backup.

Solution 2

When used --drop, mongorestore will drop the collections if they are existing in both DB as well as in backup collection. It will not drop the collections which are not there in the Backup collection.

so in your example, collB will be dropped and restored, while collA remains untouched.

Share:
10,771

Related videos on Youtube

John Jai
Author by

John Jai

Updated on June 04, 2022

Comments

  • John Jai
    John Jai about 2 years

    Suppose if I have a database testdb with two collections collA and collB and try to restore only collB from a dump.

    Will using --drop option with mongorestore drops both the existing collections (collA and collB) from testdb even though only collB is in the dump?

    I am using MongoDB 2.6.5

  • John Jai
    John Jai over 9 years
    I just tried to do a small testing, and see that one of the collection is not dropped if it's not part of the restored dump
  • wdberkeley
    wdberkeley over 9 years
    In the same database? Are you sure? If so, that's a bug. It's documented to drop every collection in the targeted database before restoring.
  • John Jai
    John Jai over 9 years
    Yes, I am sure it did not drop all the collections before restoring using the --drop option
  • hlynbech
    hlynbech almost 4 years
    The documentation says " --drop does not drop collections that are not in the backup."