enzoyang
enzoyang

Reputation: 877

mongodb how to mongodump only indexes to another mongodb instance

I have a mongodb instance with a lot of data, now I need to start up a new instance with the same structure without data.

how to get it done?

Upvotes: 15

Views: 12895

Answers (4)

superkeci
superkeci

Reputation: 171

Based on Ivan's answer, I improved the script by adding more options like expireAfterSeconds (which was crucial for me) and an flag variable to drop indexes before creating them. dropFirst variable at the top of the script can be set to true to drop every index before creating it. Also, this script keeps existing names of the indexes.

var dropFirst = false;

for(var collection of db.getCollectionNames()) {
    var indexes = db.getCollection(collection).getIndexes().filter(i => i.name !== '_id_');
    if(indexes.length === 0) continue;
    print(`\n// Collection: ${collection}`);
    for(var index of indexes) {
        var key = JSON.stringify(index.key);
        var opts = [`name: "${index.name}"`, 'background: true'];
        if(index['unique']) opts.push('unique: true');
        if(index['hidden']) opts.push('hidden: true');
        if(index['sparse']) opts.push('sparse: true');
        if(index['expireAfterSeconds'] !== undefined) opts.push(`expireAfterSeconds: ${index['expireAfterSeconds']}`);
        if(dropFirst) {
            print(`try { db.getCollection("${collection}").dropIndex(${key}); } catch(e) { print('failed to drop ${key}:', e); }`);
        }
        print(`try { db.getCollection("${collection}").createIndex(${key}, {${opts.join(', ')}}) } catch(e) { print('failed to create ${key}:', e) }`);
    }
}

Upvotes: 1

OzzyCzech
OzzyCzech

Reputation: 10342

There is really short and briliant script for create backup of indexes queries:

print(`// Backup indexes of : ${db.getName()} : database`);
print(`use ${db.getName()};`);

db.getCollectionNames().forEach(function (collection) {
    indexes = db.getCollection(collection).getIndexes().forEach(function (index) {
        if (index.name === '_id_') return; // skip defalut _id indexes
        const keys = tojsononeline(index.key);
        delete index.id; delete index.key; delete index.v; delete index.ns;
        print(`db.${collection}.createIndex(${keys}, ${tojsononeline(index)});`);
    });
});

You can run it directly from mongo shell like this:

mongo --quiet mongodb://localhost:27017/mydatabase indexes-backup.js

Output looks like:

db.user.createIndex({"user.email":1}, {"name":"userEmail", "background":true});

Upvotes: 7

Ivan Chen
Ivan Chen

Reputation: 101

You can login into mongo shell and execute the following code statements to generate creating indexes statements. After that, use the statements to recreate indexes.

var collectionList = db.getCollectionNames();
for(var index in collectionList){
    var collection = collectionList[index];
        var cur = db.getCollection(collection).getIndexes();
        if(cur.length == 1){
            continue;
        }
        for(var index1 in cur){
            var next = cur[index1];
            if(next["name"] == '_id_'){
                continue;
            }
       var unique=next["unique"]?true:false;
       print("try{ db.getCollection(\""+collection+"\").createIndex("+JSON.stringify(next["key"])+",{unique:"+unique+"},{background:1})}catch(e){print(e)}");}}

Upvotes: 9

Tug Grall
Tug Grall

Reputation: 3510

You can do that with the "query" option, with a query that does not return any document. Something like:

mongodump -q '{ "foo" : "bar"  }'

This will dump all the dbs and indexes, you can then do a mongorestore to recreate them into another mongod instance

See documentation: http://docs.mongodb.org/manual/reference/program/mongodump/#cmdoption--query

Upvotes: 19

Related Questions