mongodump ignore some specified collections

37,010

Solution 1

Now available from version 3.0.0

--excludeCollection <collection_name>
--excludeCollectionsWithPrefix <collection_prefix>

Repeat to exclude more than 1

Checkout the documentation

Solution 2

mongodump --db test --excludeCollection=users --excludeCollection=salaries

Solution 3

You can add --collection COLLECTION_NAME to dump the collection you need. By default, if you do not specify a collection to dump from a database, MongoDump will dump all collections in that database.

Solution 4

As of Mongo 3.4, you can now specify an --nsExclude <namespace pattern> option when restoring from a Mongo database dump, which will exclude the specified namespaces from the restore operation. This is particularly useful when the mongodump operation has already occurred.

Official documentation here: https://docs.mongodb.com/manual/reference/program/mongorestore/#cmdoption-nsexclude

You can exclude multiple collections with wildcards:

mongorestore --db test --nsExclude 'test.*_tmp'

Or alternatively, specifying multiple --nsExclude options work as well:

mongorestore --db test --nsExclude 'test.collection1' --nsExclude 'test.collection2'
Share:
37,010

Related videos on Youtube

sashimi
Author by

sashimi

Updated on April 05, 2021

Comments

  • sashimi
    sashimi about 3 years

    I was trying to backup my mongo database on the product sever.and then restore then back to the staging server. and here comes some problem, there are a lot of collections in db, I want to igonre some collections that I don't want to restore on staging server.

    I can approach this by dumpping the staging db, dumpping the producting db, and then restore the prodct to staging useing --drop option. and restore the specified collections in staging db. uh..it's really bad.

    1. dump producting db

    mongodump --host product-server-host --username abcd --password bcda -d db -o pruduct-dump-dir
    

    2. dump staging db

    mongodump --host staging-server-host --username abcd --password bcda -d db -o staging -dump-dir
    

    3. restore all collection, then restore the collection back restore pruduct-dump-dir to staging server

    mongorestore --host staging-server-host --username abcd --password bcda --drop pruduct-dump-dir
    
    mongorestore --host staging-server-host --username abcd --password bcda --drop --collection coll pruducting-dump-dir
    

    Is there any option like ignore-collection when I'm dumpping? any suggestion will be appreciated :3

  • sashimi
    sashimi about 11 years
    what if I have 20 collections need to dump except 4 collections
  • Nick
    Nick about 11 years
    You'll need to dump each collection individually.
  • tester
    tester over 10 years
    yeah something like --exclude_collection col1 col2 col3 would be amazing. No mention of it in docs.mongodb.org/manual/reference/program/mongodump . There appears to be a feature request in the MongoDB Jira: jira.mongodb.org/browse/SERVER-2459 you should vote for the issue
  • Thor84no
    Thor84no about 9 years
    Said documentation seems to give absolutely no indication of what it means by "array of strings". There isn't really a unified idea of arrays as parameters in the shell, so that could mean anything. Is it one string with comma separators? Is it any number of strings until a new flag is encountered? Are we supposed to put ()s or [] around it? What about repeating the flag with a different collection? All of these are used as replacements for arrays, it'd be nice if someone could clarify what they actually want.
  • Daniel Pérez Rada
    Daniel Pérez Rada about 9 years
    @Thor84no you are right, no indication what it means. But to exclude more than one collection you can repeat the excludeCollection parameter (I think this is what they mean with array). Example: mongodump --excludeCollection=users --excludeCollection=jobs -d mydatabase
  • Thor84no
    Thor84no about 9 years
    Thanks, that's very helpful.
  • cristianoms
    cristianoms almost 9 years
    Finally I'm able to use the --excludeCollection! Thanks @DanielPérezRada
  • Aniruddha Das
    Aniruddha Das over 6 years
    would it be possible to add description to answer. I am not sure code only answers are good and stackoveflow standard.
  • Anup Shetty
    Anup Shetty over 2 years
    You can use --nsInclude as well instead of --db mongorestore --nsInclude test --nsExclude 'test.collection1' --nsExclude 'test.collection2'