MongoDB Basics

CAP Theorem

The CAP Theorem states that, in a distributed system (a collection of interconnected nodes that share data), you can only have two out of the three guarantees across a write/read pair: Consistency, Availability, and Partition Tolerance.

Consistency - If a value is written and then fetched, I will always get back the new value.
Availability - If a value is written, a success message should always be returned. If a subsequent read returns a stale value, it's ok.
Partition Tolerance - The system will continue to function when network partitions occur.

Why Mongodb?

Here is a visual guide to NoSQL systems:

alt text

You can see that mongodb has picked consistency and partition tolerance. This diagram also gives you a good big picture view of how different NoSQL databases fit in the CAP triangle.

When to Use Mongodb?

For most modern web apps that requires:

  1. Account and user profiles
  2. CMS
  3. Form data
  4. Blogs / user-generated content
  5. Messaging
  6. System configuration
  7. Log data of any kind
  8. Graphs
  9. Location based data

Installing on Mac

brew install mongodb

Initial Setup

Create the data directory:

$ sudo mkdir -p /data/db
$ ls -ld /data/db
drwxr-xr-x  2 root  wheel  68 Sep 25 17:03 /data/db

The data directory must have read / write permission for the user who will run the mongoid process. Here you see only root has all the permission. Let's provide the current logged in user the permission to read and write:

sudo chown -R $USER /data/db

You will get : '10309 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating' error if you don't have the read / write privileges.

Running Mongodb


Connect to Mongod


This will open mongodb shell that allows you to interact with mongodb.

~$ mongo
MongoDB shell version: 2.6.3
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
Questions? Try the support group
Server has startup warnings:
2014-09-25T17:13:59.763-0700 [initandlisten]
2014-09-25T17:13:59.763-0700 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

Current Database

By default, mongodb uses test database, you can see the current database by using the 'db' command:

> db

List all Databases

To see a list of all databases:

> show dbs
admin  (empty)
local  0.078GB

New Database

To use a new database 'mydb':

> use mydb
switched to db mydb

To check the current database:

> db

When you use a new database, the current database changes to the new database.

> show dbs
admin  (empty)
local  0.078GB

But the show dbs command does not show the mydb in it's list. Let's create two new documents using javascript functions:

 j = { name : 'Mongo' }
{ "name" : "Mongo" }
> k = { x : 3 }
{ "x" : 3 }

Insert Documents

To insert the two documents into the mydb database:

> db.testData.insert(j)
WriteResult({ "nInserted" : 1 })
> db.testData.insert(k)
WriteResult({ "nInserted" : 1 })

Here the testData is a collection that can hold documents. In this case we have two documents in the testData collection. The collection is equivalent to table in relational databases.

View Collections

To view all the collections:

> show collections

You can see we have testData collection.

> show dbs
admin  (empty)
local  0.078GB
mydb   0.078GB

Now we also see the mydb database, it was created when new documents were inserted into the testData collection. Let's check the two new documents we created exists in the database:

> db.testData.find()
{ "_id" : ObjectId("5424b92e0b73fd80bdd0724a"), "name" : "Mongo" }
{ "_id" : ObjectId("5424b9380b73fd80bdd0724b"), "x" : 3 }

_id has unique value for every document. Mongodb automatically generates the value and assigns to a document when it is created.


  1. MongoDb and the 10309 Unable to create/open lock file on Ubuntu
  2. Getting Started with MongoDB
  3. Install MongoDB on OS X
  4. CAP Theorem: Revisited
  5. Why is MongoDB wildly popular?
  6. Where does MongoDB stand in CAP Theorem

Related Articles

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.