I've traditionally worked with MySQL databases, they are really powerful, but... sometimes it gets really complicated with long and messy SQL Statements for storing simple data... Maybe that's because we are just using the wrong database schema.

When I first saw JSON compared to XML files it was like night and day. JSON is much more simple to use, with less grammar and easy to read. If on top of that you combine it with Python and Dictionaries we have a winner! Now, this is very cool, but, how to store all these json files in a database? MongoDB to the rescue.

One of the best things of MongoDB is that you don't need to create a database, schema, etc beforehand for your collections and documents. Everything is dynamic! So you may store different documents in the same collection but each of them with different fields.

Lets see some examples and and tips. Bear in mind that I'm using PyMongo (Python library to access a Mongo DB). The syntax in other languages will be the same or very similar (check the documentation).

DB Connection

First thing after installing the corresponding library for MongoDB in you preferred language is to connect to our database. Remember that databases and collections are dynamically created! In this case a new database named "Library" will be created.

import pymongo

client = pymongo.MongoClient('127.0.0.1', 27017)  
db = client["Library"]   #create/connect to the Library Database  

Create a collection

We can create different collections in our database. For instance, we could have a collection of books, a collection of authors, etc. Lets create the collection of books:

import pymongo  
collection = db["Books"]  #create/connect our collection of books  

Adding a document to our collection

There are different scenarios here: We may want to add the book only if it doesn't exist already or update the book with the new information if it exists.

By default MongoDB generates a unique id for every document. It looks like this:

"_id" : ObjectId("53f4e21f3b58152222782689")

the default "_id" created by MongoDB stores the creation date of the document see here.

If we have a unique key for every document we can use it instead of the default one. In this example, we know books got a unique id called ISBN so we will be using this for the "_id" value.

If we would want to use the default _id but still have a unique key for the ISBN we would have to declare it like so:

collection.ensureIndex("ISBN", unique=True)  

This is extremly important to not generate duplicates in our collection and to update the information of our existing documents.

Insert a document

Sample document

First of all we need to create a new book for our collection:

book = {}  
book["_id"] = "978-3-16-148410-0"  #The unique key  
book["title"] = "MongoDB book"  
book["year"] = 2014  
book["authors"] = ["Author 1", "Author 2"]  
book["sells"] = 0  
Insert only if it doesn't exist

This will insert the book if it doesn't exist. If it does, it gives an exception.

try:  
    collection.insert(book)
except:  
    print("Document already exists")
Update if exists / Insert if it doesn't

There are 2 options here:

  • update(): This will update the document with the specified id or create a new one if it doesn't exists because of the parameter upsert=True
collection.update({'_id':book["_id"]},book,upsert=True)  
  • save(): It does the same thing.
collection.update({'_id':book["_id"]},book)  

Update a document

The update shown before replace an entire document. But if we just want to replace one field we have to do it like this:

collection.update({'_id':book["_id"]},{"$set":{"sells":1}})  

While the example above works fine, tipically we will need to increase the number of sells by 1 instead of hard-coding the number.

collection.update({'_id':book["_id"]}, {"$inc": {"sells": 1}})  

Find documents

It's also very easy to find documents. If we want to search for books from "Author 1":

cursor = collection.find({'author':"Author 1"})  
for book in cursor:  
    print book["title"]
Sort cursor

It's also possible to sort the cursor results depending on a key value:

cursor = collection.find({'author':"Author 1"}).sort('Title', pymongo.ASCENDING):  
for book in cursor:  
    print book["title"]
Limit cursor size

To get just a fixed number of results

cursor = collection.find({'author':"Author 1"}).limit(2):  
for book in cursor:  
    print book["title"]

Remove documents

It works the same way as find. This will remove all books with less than 2 sells.

cursor = collection.remove({'sells':{"$lt":2}})  

Drop DB / collection

To remove the entire Database with all the collections:

#db.drop_[DATABASE_NAME]  # In this example it's Library
db.drop_Library  

To remove a collection

collection.drop()  

Admin GUI for MongoDB

While MongoDB has got a console it's very usefull to visually view our database. I've tried these two options which work very well.

Robomongo (Standalone app)

Robomongo is a multi-platform app that connects to any MongoDB. You can see all the collections, documents and perform find queries, edit values etc.

Robomongo

Official website

Genghis (Web)

Genghis has the same capabilities of Robomongo but it's a full web server. So you can access your database from any web browser.

Genghis

Official website

I hope this post will get you started with MongoDB!