Using MongoDB
By: Satish Chandra
Tags: Mongodb, No Sql, Tools

MongoDB is a popular document based NO-SQL database. Some of the key features of MongoDB are

Document-oriented storage Querying Full Index support Replication and High Availability Map-Reduce Auto Sharding

Installation:

Installing mongodb is easy and the ways to do that are very well documented here

If you are using Mac OSx, Here the steps to follow:

Install Brew:

> ruby -e “$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)”

> brew help

Install mongodb using Brew:

> brew install mongodb

Running MongoDB:

Create a folder for mongodb to store data. MongoDB uses /data/db as default data store location. If you want to use a different folder you can create any new directory. Make sure you give read and write permissions to the folder.

> mkdir /data/db

> chmod 777 /data/db

You can also configure db path in located in /usr/local/etc/mongod.conf

You can start the mongodb with default configurations using the following command

> mongod

If you want to use a custom data directory use the following command

> mongod — dbpath <Path to data directory>

To have launchd start mongodb at login, run the following commands:

> ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents

> launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

Getting Started:

Here are some basic commands you can try on mongodb.

Connecting to Mongodb:

> mongo

Viewing databases

> show dbs;

Create new database: Below command checks for the database named testdatabase, if it is not found, it creates one and connects to it.

> use testdatabase

To check which database are we connected to run the following command

> db

Inserting a document

Documents are added to a collection in a database;

You can see the collections present in a database with the following command after connecting to a database:

> show collections;

Creation of collection and inserting a document to it can be done with a single command as follows:

> db.mycollection.insert(j={name:’Test User’});

WriteResult({ “nInserted” : 1 })

> k = { “pi” : 3.141 }

You can also use “save” instead of “insert” to insert a document

> db.mycollection.save(k)

WriteResult({ “nInserted” : 1 })

From above two commands you can see that you can observe few things:

1. Document will be in a JSON format (Formally, it is a BSON document a binary form of JSON)

2. Documents can have different attibutes. The advantage of this way is you need to spend much time in designing or pre-processing the document structure.

Querying:

> db.mycollection.find();

{ “_id” : ObjectId(“54026ca917de850fd8ed2ad9”), “name” : “Test User” }

{ “_id” : ObjectId(“54026d3b17de850fd8ed2ada”), “pi” : 3.141 }

If there are lot of documents, and you want to see one document(which meets your condition) you can using findOne() method:

> db.mycollection.findOne();

{ “_id” : ObjectId(“54026ca917de850fd8ed2ad9”), “name” : “Test User” }

Also, you can specify number of documents to return as follows:

> db.mycollection.find().limit(2)

{ “_id” : ObjectId(“54026ca917de850fd8ed2ad9”), “name” : “Test User” }

{ “_id” : ObjectId(“54026d3b17de850fd8ed2ada”), “pi” : 3.141 }

Removing document from a collection:

> db.mycollection.remove(k)

WriteResult({ “nRemoved” : 1 })

> db.mycollection.remove({name : “Test User”})

WriteResult({ “nRemoved” : 1 })

You can also remove all documents as follows:

>db.mycollection.remove()

Dropping a database

>db.dropDatabase()

I believe with this basic knowledge of MongoDB, you can get started and explore more about MongoDB. MongoDB is very well documented and I believe their official webpage is a best source for learning MongoDB.

Thanks for viewing this article. If you have any questions, please leave them in comments section and I will be happy to help you.