Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb dump multiple collections or exclude collections version 2.6

Tags:

mongodb

How do I dump multiple collections or maybe exclude collections on mongodump? mongodb is version 2.6

I'm just doing it like this now.

mongodump  --collection map_accounts_clicks --out /dumps
like image 717
Boy Avatar asked Sep 26 '16 06:09

Boy


People also ask

Can a MongoDB have multiple collections?

Yes, you can have multiple collections within a database in MongoDB.

What is the difference between Mongodump and Mongoexport?

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.

What is Mongodump and Mongorestore?

Deployments. The mongodump and mongorestore utilities work with BSON data dumps, and are useful for creating backups of small deployments. For resilient and non-disruptive backups, use a file system or block-level disk snapshot function, such as the methods described in the MongoDB Backup Methods document.


1 Answers

As --excludeCollection is introduced with mongo 3.0 and --collection is a parameter than can only submitted once, there is no official way to do that.

What you could do is query mongo inbefore to get all the wanted collections as an array and then loop through this array to dump each collection:

First, let's get to your bash script. It will call the mongo client to receive a list of collections matching to a given search term:

database_name="database1" # target database
searchTerm="foobar" # if you do not want to dump all collections
strCollections=$(mongo localhost:27017/${database_name} getCollections.js --eval "var searchTerm = ${searchTerm}")

The referenced java script file looks like this:

{

    // remember to set slaveOk if you working on a replica set and query a slave
    // rs.slaveOk();

    // init your result array
    var wantedCollections = [];

    // first get all existing collections
    var collections = db.getCollectionNames();

    // count number of existing collections
    var collectionsCount = collections.length;

    // prepare your regular expression, searchTerm is a variable coming from the command line
    var regEx = new RegExp(searchTerm, 'g');

    // now loop through all existing collections
    for (var i = 0; i < collectionsCount; i++) {

        // check if search term matches collection name
        var regExMatch = regEx.test(collections[i]);

        // if so, add it to our result array
        if (regExMatch == true) {

            wantedCollections.push(collections[i]);

        }

    }

    // finally print result array, it's a comma separated list, perfect to work with in bash
    print(wantedCollections);

}

Back to your bash script. Now we have a list of collections that we want to export. First you need to convert the comma separated list of collections into an array:

IFS=', ' read -r -a collections <<< "$strCollections"

Now you just loop through the collections in your array and dump them as usual:

for collection in "${collections[@]}"
do
    mongodump \
        --db=${database_name} \
        --collection ${collection} \
        --out ${destination_folder}     
done

That's all. Disadvantage is: If you run mongodump for each single collection, you can not take advantage of parallel exports. There is a workaround for this, too. But this again requires you to use the --exclude-collection.

like image 152
n.r. Avatar answered Sep 19 '22 05:09

n.r.