My MongoDB scratchpad

Importing bulk json files using mongoimport:

A bash script is needed for doing this. An example bash script:

FILES=/Users/arefindk/datasets/gitnet/*  
for f in $FILES  
do  
  echo "Importing file $f..."
  mongoimport --db github_watch --collection gt_watch --file $f
  echo "Done $f"
done  

Bulk convert a string field to double in MongoDB:

db.gt_watch_nofork.find({  
repository_watchers:{$type:"string"}}).forEach(function(doc)  
{ 
    doc.repository_watchers = parseInt(doc.repository_watchers);
    db.gt_watch_nofork.save(doc);  
})

Dumping a specific DB or even a specific collections:

mongodump --collection myCollection --db test  

It will create a new directory called dump where you have run the command.

You can restore the DB and the collection using the mongorestore command:

mongorestore dump/  

Source:MongoDB

Saving an aggregation result to a collection

db.<collection>.aggregate( [  
     { <operation> },
     { <operation> },
     ...,
     { $out : "<output-collection>" }
],
     {"allowDiskUse":true}
 )

Source:StackOverflow

Remove unnecessary temporary collection if output of an aggregation to a collection or a map-reduce was terminated ungracefully

function f() {  
    var names = db.getCollectionNames();  
    for(var i = 0; i < names.length; i++){    
        if(names[i].indexOf("tmp.agg_out.") === 0){      
            db[names[i]].drop();    
    }  
}};
f();  

Source:Richard Huangs Blog

Sample random documents from the collection and create a small collection from the sample

db.users.aggregate(  
   [ { $sample: { size: 3 } } ,{$out:"<outputcollection>"}]
)

Source:MongoDB Manual

Doing full text search in one field and sort the result based on meta text score:

db.all_names.find({$text: {$search: "bangkok"}}, {score: {$meta: "textScore"}}).sort({score:{$meta:"textScore"}}).pretty()  

Source: Tutsplus

Find the max value in a field throughout the database:

db.articles.find({},{year:1}).sort({year:-1}).limit(1).pretty()  

Source: Stackoverflow

Show Comments