MongoDB and the rest of NoSQL solutions

Posted by – August 8, 2009

In the last weeks I have been evaluating two NoSQL solutions available in the market, MongoDB and CouchDB, each solution has very interesting features, but besides of its features we must consider how much easy to use each one is, right?

Let’s talk a little about CouchDB first, CouchDB is a document-oriented solution written in Erlang language, with CouchDB you can easily persist documents in JSON format it also has a very powerful RESTful interface from where you can retrieve database information and persist/query documents.

But how to perform basic operations on CouchDB like persist or query data? It’s simple, you just need to know how to use its RESTful interface, the example below shows how much easy is persist data in CouchDB with help of curl tool:

1
curl -X PUT -d '{"name": "Rogerio Araujo", "address": "Estrada dos Bandeirantes", "city": "Rio de Janeiro", "state": "RJ", "country": "Brazil" }' "http://127.0.0.1:5984/contacts/user3"

The command above will create the following JSON document on database:

1
{ "_id": "user3", "_rev": "3771096703", "name": "Rogerio Araujo", "address": "Estrada dos Bandeirantes", "city": "Rio de Janeiro", "state": "RJ", "country": "Brazil" }

Once we have few documents stored on CouchDB we need to create a view used to help on query data, to do it, you must write the following javascript function and store it as a document on database:

1
2
3
4
function(doc) {
  if(doc.name)
     emit(doc.name, doc);
}

You can create a view that uses the map function above by running the following command:

1
curl -s -i -X PUT -H 'Content-Type: application/json' -d '{"views":{"find_by_name":{"map": "function(doc){if(doc.name) {emit(doc.name, doc);}}"}}}' 'http://localhost:5984/contacts/_design/queries'

To query data by using the previously created view you can use curl command to issue a HTTP request on CouchDB server like below:

1
curl -X GET "http://127.0.0.1:5984/contacts/_view/queries/find_by_name?key=\"Rogerio%20Araujo\""

The command above will produce the following output:

1
2
3
{"total_rows":2,"offset":0,"rows":[
{"id":"user2","key":"Rogerio Araujo","value":{"_id":"user1","_rev":"1594867904","name":"Rogerio Araujo", "address":"Estrada dos Bandeirantes", "city":"Rio de Janeiro", "state":"RJ", "country":"Brazil"}}
]}

As you can see, query data on CouchDB isn’t a easy task, if you need perform more complex queries must must go deep on MapReduce concepts behind CouchDB, if you need pass arguments to your views you must spend some time trying to understand how “key”, “startkey” and “endkey” parameters are passed to your view and how they can help on execution of your queries.

Now that we know how to persist/query data on CouchDB, it’s time to compare it with MongoDB, MongoDB is the other well known document-oriented database written in C++, it comes with a interactive interface called mongo where you can type commands to persist/query documents on database collections, you also have drivers available in several languages.

Since MongoDB doesn’t have a RESTful interface, we must run his interactive shell to perform few operations on our collections, the example below shows how to run mongo interactive shell to execute database commands:

1
mongo my_database

The example above will connect to the database called my_database running in my local machine at port 27017, once you are connected to my_database, you must create a database collection on which we will stored few documents, you can easily create a collection on database by running the command below:

1
db.createCollection("contacts")

And now you can run the following command to add a new document to contacts collection:

1
db.contacts.save({name: "Rogerio Araujo", address: "Estrada dos Bandeirantes", city: "Rio de Janeiro", state: "RJ", country: "Brazil" })

You can query the document persisted above by running the following command:

1
db.contacts.find({name: "Rogerio Araujo"})

That will produce the following output:

1
{"_id" :  ObjectId( "4a7dd122d9385b931492690c")  , "name" : "Rogerio Araujo", "address": "Estrada dos Bandeirantes", "city": "Rio de Janeiro", "state": "RJ", "country": "Brazil" }

If you don’t remember the full name of your contact, you can use a regex in your query:

1
db.contacts.find({name: "/Araujo/i"})

It will produce the same output as the first query.

Despite of missing REST interface to query data, MongoDB has a query language that is very easy to understand, you don’t need to spend any effort on creating views or figuring out how to pass arguments to your queries.

But what about Project Valdermort or Tokio Cabinet? How can we compare these two solutions with MongoDB or CouchDB?

Since both projects are key-value databases, I’m not sure if we can do a good comparission with document-oriented databases like MongoDB or CouchDB, in a first look they have a very easy way to persist data, but I’m not sure if they have a easy to understand and flexible query language as MongoDB has.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>