Skip to content

MongoDB Cheatsheet

MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program (as opposed to SQL), MongoDB uses JSON-like documents with optional schemas.

Import from CSV

The following command imports a CSV file into a MongoDB collection using the mongoimport tool.

mongoimport --db users --collection contacts --type csv --headerline --file contacts.csv

Specifying --headerline instructs mongoimport to determine the name of the fields using the first line in the CSV file. Use the --ignoreBlanks option to ignore blank fields. For CSV and TSV imports, this option provides the desired functionality in most cases, because it avoids inserting fields with null values into your collection.

MongoImport documentation

myCursor.forEach(console.log);

// Or.
while (myCollection.hasNext()) {
   console.log(myCollection.next());
}

Aggregation Tips

// Lowercase a string.
{ $project: { "address": { $toLower: "$address" } } },

// Extract field within embedded document.
{ $project: { "experience.location": 1 } },

// Flatten.
{ $unwind: "$experience"},
{ $group: { _id: "$_id", locs: { $push: { $ifNull: [ "$experience.location", "undefined" ] } } } }

// Output a collection.
{ $out: "myCollection2" }

// Get unique values.
{ $group: { _id: "$fulladdress" } }

Make a Copy

Don't use copyTo - it is fully blocking... and deprecated in 3.x

  • Use the Aggregation framework:
db = db.getSiblingDB("myDB"); // Set current db for $out.
var myCollection = db.getCollection("myCollection");

// Project if needed, get uniques if needed, create a new collection.
myCollection.aggregate([{ $project:{ "fulladdress": 1 } },{ $group:{ _id: "$fulladdress" } },{ $out: "outputCollection" }], { allowDiskUse:true });
  • Or use bulk update:
var outputColl = db.getCollection( "outputCollection" );
var outputBulk = outputColl.initializeUnorderedBulkOp();
myCollection.find( {}, { "fulladdress": 1 } ).forEach( function(doc) {
     outputBulk.insert(doc);
});
outputBulk.execute();

Longer Example

Add a count field to all records

// TODO Reviewed ??-??-2026

function normalizeAddressKey(address) {
    return (address || "").toString().toLowerCase();
}

function buildStatsAggregationPipeline(limit) {
    var max = Number.isInteger(limit) && limit > 0 ? limit : 100000;
    return [
        { $group: { _id: { $toLower: "$address" }, count: { $sum: 1 } } },
        { $sort: { "count": -1 } },
        { $limit: max },
        { $out: "stats" }
    ];
}

function gatherStats() {
    var start = Date.now();

    var inputDB = db.getSiblingDB("inputDB");
    var inputColl = inputDB.getCollection("inputColl");

    // Debug: inputColl.find( {} ).limit(2).forEach(printjson).

    outputDB = db.getSiblingDB("outputDB");
    db = outputDB; // Set current database for the next aggregate step.

    // Create temporary collection with count.
    inputColl.aggregate(buildStatsAggregationPipeline(100000), { allowDiskUse: true }); // Returns { _id, count } where _id is the address.

    var statsColl = outputDB.getCollection("stats");

   // Create output collection.
    var outputColl = outputDB.getCollection("outputColl");
    var outputBulk = outputColl.initializeUnorderedBulkOp();
    var counter = 0;

    var inputCursor = inputColl.find( {}, {} );
    inputCursor.forEach( function(doc) {
        var statDoc = statsColl.findOne( { _id: normalizeAddressKey(doc.address) } );
        if (statDoc) {
            doc.count = statDoc.count;
            outputBulk.insert(doc);
            counter++;
            if ( counter % 1000 == 0 ) {
                    outputBulk.execute();
                    // You have to reset.
                    outputBulk = outputColl.initializeUnorderedBulkOp();
                }
            }
        }
    );

    if ( counter % 1000 > 0 )
        outputBulk.execute();


    // Print the results.
    outputColl.find({}).sort({count: -1}).forEach(printjson);

    var end = Date.now();
    var duration = (end - start)/1000;
    console.log("Duration: " + duration + " seconds");

    console.log(" | DONE | ");
}

if (typeof module !== "undefined" && module.exports) {
    module.exports = { gatherStats, normalizeAddressKey, buildStatsAggregationPipeline };
}

if (typeof module === "undefined" || !module.parent) {
    gatherStats();
}

Alternatively move data to memory:

    var statsDict = {}; // Or better Object.create(null).
    statsColl.find({}).forEach( function(doc) { statsDict[doc._id] = doc.count } );

    // Could also use: var statsArray = statsCursor.toArray().

    inputCursor.forEach( function(doc) {
        if (doc.address in statsDict)
        {
            doc["count"] = statsDict[doc.address];
            outputBulk.insert(doc);
        }
    });
    outputBulk.execute();