Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 04ce1cbc69
Fetching contributors…

Cannot retrieve contributors at this time

238 lines (159 sloc) 5.615 kb

A (Gentle) Introduction to MongoDB

What is MongoDB?

MongoDB is made up of databases which contain collections. A collection is made up of documents. Each document is made up of fields. Collections can be indexed, which improves lookup and sorting performance. Data is retrieved through a cursor whose actual execution is delayed until necessary.

How do we get MongoDB?

brew install mongodb

How do we start MongoDB?

sudo mongod | tail -f /usr/local/var/log/mongodb/mongo.log 

How do we insert data?{title: 'I am a title', content: 'Yadda, yadda, yadda', date: new Date(), tags: ['tag1', 'tag2'], upvotes: 1})

Where's the schema?

Implicitly defined in the application(s).

How do we list the collections in a DB?


How do we list the documents in a collection?


What's up with that _id field?

  • db.system.indexes.find()

  • ObjectId("505bd76785ebb509fc183733").getTimestamp()

Where is some test data?

Here you go:

(function() {
    var titlePrefix = ['Some', 'More', 'About', 'Consider The'],
            titleSuffix = ['Stuff', 'Things', 'Problems'],
            tags = ['omg', 'humm', 'wtf'],
            upvoteDelta = 16,
            maxDocuments = 10,
            rand = function(max) { return Math.floor(Math.random() * max); },
            decide = function(what) { return rand(2) > 0; },

    for (i = 0; i < maxDocuments; ++i) {
          var doc = {
            title: titlePrefix[rand(titlePrefix.length)] + ' ' + titleSuffix[rand(titleSuffix.length)],
            content: 'Yadda, yadda, yadda',
            date: (function(d) { return new Date(d.setDate(d.getDate() + rand(maxDocuments))); })(new Date()),
            upvotes: rand(upvoteDelta) * (decide('if negative') ? 1 : -1),
            tags: (function() {
                var n = rand(tags.length),
                        ts = tags.length;
                return n > 1 ? randInts(n, ts).map(function(i) { return tags[i]; }) : tags[rand(ts)];

        if (decide('has comments')) 
            doc.comments = ['Some Comment', 'Another Comment'];


  function randInts(howMany, maxValue) {
    var values = new Array(howMany),

        for (i = 0; i < howMany; ++i) 
            values[i] = rand(maxValue);

        return values;

How do I query a collection?

In lots of ways:

  •{upvotes: {$gte: 0}})
  •{upvotes: {$gte: 0}}).count()
  •{comments: {$exists: true}})
  •{upvotes: {$gte: 0}, comments: {$exists: true}})
  •{upvotes: {$gte: 0}, $or: [{tags: 'omg'}, {tags: 'wtf'}]})
  •{_id: ObjectId("TheObjectId")})
  •{upvotes: -1})
  •, {tags: 1, upvotes: 1}).sort({tags: 1, upvotes: -1})
  •{upvotes: -1}).limit(5).skip(2)
  •{$where: function() { return this.tags instanceof Array; }})
  • ...

How do I update a document?{title: 'I am a title'}, {content: 'Some interesting content'})

Oops.{content: 'Some interesting content'}){title: 'I am a title', content: 'Yadda, yadda, yadda', date: new Date(), tags: ['tag1', 'tag2'], upvotes: 1}){title: 'I am a title'}, {$set: {content: 'Some interesting content'}})

What more can I do with update?

  •{title: 'I am a title'}, {$inc: {upvotes: -1}})
  •{title: 'I am a title'}, {$push: {tags: 'another tag'}})

What about upserts?{title: 'I am not a title'}, {$inc: {upvotes: 3}}, true)

Are there multiple updates?{}, {$set: {public: true}}, false, true)

Cute. Now show us some aggregations!

var map = function() {
    var i;

    if (this.tags === void 0)

    if (!(this.tags instanceof Array)) {
        emit(this.tags, {count: 1});

    for (i in this.tags)
        emit(this.tags[i], {count: 1});

var reduce = function(key, values) {
    var sum = 0;

    values.forEach(function(value) {
        sum += value.count;

    return sum;
}, reduce, {out: {inline: 1}})

Are there indexes?


  •{title: 1})
  •{title: 1})

Can they be unique?

No problem:{title: 1}, true)

Do queries actually use them?

  •{title: "I am a title"}).explain()

What about concurrency?

DB db...;
DBCollection coll...;
try {
   DBObject err = db.getLastError();
} finally {

Tell us more!

  • Drivers in lots of languages
  • Fire and forget writes: call db.getLastError() to be notified about a failed write
  • Capped collections: db.createCollection('logs', {capped: true, size: 1048576})
  • Bulk inserts
  • Geospatial
  • Replication (for availability)
  • Durability with journal = true
  • Consistency à la carte (e.g., db.runCommand({ getlasterror : 1 , w : "majority" }))
  • Configurable read preference
  • Auto-sharding
  • Stats: db.stats()
  • The Manual
Jump to Line
Something went wrong with that request. Please try again.