General

Home > MongoDb > General

Table of Contents

  1. Tips

Tips

Connect to a replicaset

Backup and restore

Get largest document

var max = 0;
var id = 0;
db.mycollection.find().forEach(function(obj) {
    var curr = Object.bsonsize(obj); 
    if(max < curr) {
        max = curr;
		id = obj._id;
    } 
})
print(max);
print(id);

Find duplicates

db.mycollection.aggregate(
    {"$group" : { "_id": "$myproperty", "count": { "$sum": 1 } } },
    {"$match": {"_id" :{ "$ne" : null } , "count" : {"$gt": 1} } }, 
    {"$project": {"myproperty" : "$_id", "_id" : 0} }
)

Load file

Example of loading a javascript file containing index creation:

print('-- Reset indexes for mycollection --');

print('*** Drop indexes ***');
var dropResult = db.mycollection.dropIndexes();
printjson(dropResult);

print('*** Create indexes ***');
var createResult = db.mycollection.createIndexes(
    [
        { "myfield1" : 1, "date" : -1 },
        { "nested.myfield2" : 1}
    ]);

printjson(createResult);

var createIndexesSparse = db.mycollection.createIndexes(
    [
        { "sparse.field1": 1 },
        { "sparse.field2" : -1 }
    ],
    {
        sparse: true
    });

printjson(createIndexesSparse);

print('*** Get all indexes ***');
var indexes = db.mycollection.getIndexes();
indexes.forEach(function (index) {
    print(index.ns + '.' + index.name);
});

In the shell, type: load("D:\\path\\to\\file\\initialize_indexes.js").

Save to file

Example to save the result to a file: mongo --quiet mydatabase --eval "printjson(db.mycollection.findOne({'_id' : ObjectId('59511bd38186f446441fc552')}))" > result.json.