First Experiences with MongoDB 4 years ago

I have never had the change to really try out document databases. Sure, I have tried them out in some small console app just to experiment/try it out. Not before this website have i really commited a project with a document database as a backstore. But developing with MongoDB has been a pleasure.

Sure, my website is kinda small and I'm the only one who writes to the database, so maybe MongoDB's ease-to-use and it's utility isn't universally true.

Document Databases and MongoDB

An ordinary relational database, e.g. Sql Server, Postresql and MySQL, relies on tables and relations between these tables to represent data. A document database on the other hand (No-SQL database) uses the concept of a document. A document is in MongoDB's case represented as a JSON document. Internally though, MongoDB stores the documents using BSON, which stands for Binary JSON, which essentially is what it sounds like: "bin­ary-en­coded seri­al­iz­a­tion of JSON" (though when talking to MongoDB one uses ordinary JSON).

MongoDB in practice

Let's say we have two objects we would like to store:

  "name": "Erik",
  "hobbies" : [ "climbing", "skiing" ]


  "name": "Mikael",  
  "city": "Bern"

These documents share some properties, but some properties are unique. In a relational database we would need to create two tables (e.g. person, and hobbies) where person would need to have three columns (id, name, city) and the hobbies-table would need two columns (person_id and hobby). And we would need to set city to null for the first document.

MongoDB uses dynamic schemas. This allows for quick development since new elements can be added to documents within a collection on-the-fly. No structural defining schema has to be modified. I have also noticed when using SQL Server or PostgreSQL that migratig data is a real hassle. For example, all the work entries on this site I did while i was developing the site locally (i.e. on my PC's mongodb instance), so i was fearing moving all this to my VM's instance -- how wrong i was. This was the only commands needed:

mongoexport --db erikfromsweden --collection work --out work.json

mongoimport --db erikfromsweden --collection work work.json

And that was it. The database erikfromsweden and the collection work was created automatically when I imported the documents. It was really that easy (excluding scp to upload work.json, and ssh to execute mongoimport on the server). I have never needed to configure MongoDB, all i did was apt-get install mongodb and is just worked. Magic. At the moment my favourite stack to be working with is NodeJS + Jade + ExpressJS + MongoDB.

So what about querying? It works good aswell. In the MognoDB CLI client/shell mongo:

> use erikfromsweden
>{ published: true })

The syntax and semantics is simillar when working with the NodeJS MongoDB client (you would have from callback-function).

Technical Stuff

MongoDB is not the only document databae available, there is many other projects out there. Here are some technical specs for MongoDB:

  • Supports Static Indexing
  • Sharding, Replication and Full Backup
  • Written in C++
  • No Concurrency Management "atomic writes on a per-document level, and fully-consistent reads.". If you need it, checkout RavenDB (cost monies), or Apache CouchDB
  • No multi-document transaction support

Checkout the MongoDB Fundamentals FAQ for more information (you can also find more FAQs there).

Conclusion / When to use

I would say that for personal projects with limited writers MongoDB is a perfect solution. The extremely user-friendly interfaces and behaviour made me feel comfortable after 3 minutes of use. It also has a large documentation with lots of examples. If used in a commercial or larger project a case-specific analysis of the different databases would be needed.

Checkout MongoDB's website!

Tweet about this post