Mongorestore specific documents?


We are using a fairly basic backup strategy. Every day, we dump our entiry collections (although we have just one for now) with:

mongodump --uri="mongodb://backup@mongo1:27017,mongo2:27017,mongoarb:27017/?replicaSet=rs0&readPreference=secondaryPreferred" --oplog --gzip --out="C:\Temp\MongoBackups\"

Assuming I’m only interested in my database…

My questions:

  1. If I want to restore just the documents the were deleted, is it just a matter of ignoring the duplicate key errors from:

    mongorestore --uri="mongodb://admin@mongo1:27017,mongo2:27017,mongoarb:27017/?replicaSet=rs0" --dir="C:\Temp\MongoBackups\" --nsInclude="" --gzip
    continuing through error: E11000 duplicate key error collection: index: _id_ dup key: { _id: "xxxxxxxxxxx" }

    Is there a way to tell it to try and restore only the missing ones, as to not have these errors showing up?

  2. Even better, how could I eventually restore specific document(s)? The only way I could think of right now is to restore the hole collection into another temporary collection, and then manually go in and get the documents from there and update the good collection afterwards with it, i.e.:

    mongorestore --uri="mongodb://admin@mongo1:27017,mongo2:27017,mongoarb:27017/?replicaSet=rs0" --dir="C:\Temp\MongoBackups\" --nsInclude="" --nsFrom="" --nsFrom="LCB.jobs_restore" --gzip --drop // specifying drop to make sure it's a clean temp restore

    …then update with values from
    LCB.jobs_restore.documentID type of thing.

    Once finished, cleanup with db.jobs_restore.drop()
    Is there a better way for this?

Much thanks for your precious time.


hi @Patrick_Roy,
i have two possible answers for your questions:

  1. it isn’ t possibile to restore a specific document, so the way to restore all the collections from that db is to add the --drop option’s in mongorestore to avoid this error, in order to restore completely the collections in this db.
  2. You can add option --query in mongodump to specificy in a query which documents you need to dump.

hoping this answer is useful for you!


Thanks @Fabio_Ramohitaj. Unfortunately, none of the two solutions would work in my case. I need to backup everything on a nightly basis: I cannot know in advance which documents my end-users will mess up that’ll need restoring afterwards :wink:

And, the --drop option also isn’t a solution for us: other documents might have been modified and we might not want to lose the changes…

I guess my only duable way is to restore into another temp collection and use it to update my good collection afterwards…

Thanks for your return though.

One trivial way to do that is to restore your backup into another database/collection/server and then pick and chose which documents from the restored location needs to be copied into the real system.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.