Beliebte Suchanfragen

Cloud Native



Agile Methoden



MongoDB: First Contact

1.11.2012 | 8 minutes of reading time

I am still not 100% sure why I did this, but some days ago I spontaneously enrolled for an online MongoDB Developers Course. Me! MongoDB! That is after more than ten years of developing software heavily based on Oracle and another three years using DB2. And believe me: The first shock was already waiting for me in one of the very first lectures: These guys do not have tables! They are not having Joins and – but I was expecting this somewhat from all this NoSQL-talk – they do not support SQL!

Probably I have been unknowingly assimilated by some of my colleagues. Of course I do not mention any names here. But I can somehow still hear them during lunch time, in our intranet and blog: We are MongoDB! We will add your tables and indexes to our JSON Document Store! Resistance is futile! Well, ok, might be that I am exaggerating a little bit, but really only a little bit ;-).

So if you are expecting some brilliant technical insight from a MongoDB guru, you better stop reading here. Otherwise you are risking quite some disappointment. But if you are interested to look me over the shoulder while I am taking my first steps into the world of a NoSQL database – and this through the eyes of a long-term RDMS -guy – I would be happy if you (virtually) join me.

The above mentioned class is lasting seven weeks and I am planning to keep you posted on any progress, fancy insights and of course how much I will miss a good old “select” on some good old database tables (or maybe not anymore in the end?). Well, indeed the class started roughly a week before me starting to write this. Thus I am a little late already and have to divide myself between blogging and studying. So here we go …

MongoDB – Week 1

The good thing is: I have really hardly any clue about this NoSQL database stuff. Thus I can start this class absolutely unloaded. The bad thing is: I really have no clue about this NoSQL database stuff. Thus I have to start totally from scratch, which means 43 lectures for the first week (the one that is already over).

“Joins are not horizontally scalable.” – Quotes from the course

In the first few lectures for me it is not so much about finding out what MongoDB has or can, but more what it does not support. The list is rather long:

  • There are no database tables.
  • There are no “joins”.
  • There are no transactions.
  • And MongoDB is schema-less.

Honestly I am a little bit sceptical. But at the same time a lot of these things are giving me quite some headache in a current project running on DB2. Transactions about server boundaries? Using two-phase-commit or not? Every change in the structure of our database tables requires quite some additional work. All these problems would simply not exist anymore? Well, I am far away from believing that all my problems will just vanish using MongoDB. Experience tells me that there will be other problems ;-).

“The only source of knowledge is experience.” – Albert Einstein

What do we have on the other side so far: Installation of MongoDB on my Mac runs very smoothly and five minutes after the download has been finished I created my first object to the database and retrieved it again:

MongoDB shell version: 2.2.1
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
Questions? Try the support group
> db.names.find()
{ "_id" : ObjectId("50916fb5ddb9073fa519a952"), "name" : "thomas" }

Data is stored to the database using JavaScript Object Notation (JSON) . The MongoDB shell seems to be operating on some kind of JavaScript-syntax as well. I cannot say that I am a particularly big fan of JavaScript as such. But I have to admit that I would bet quite some amount of money that storing a first record to some Oracle database that has just been installed would take significantly longer.

“It is hard to scale-out a RDMS on commodity hardware.” – Quotes from the course

Finally Python it is! Python!

The whole class, which is by the way so far really excellent and really entertaining to follow – even at 21:17 the evening before a public holiday – is based on some sample application. And guess what: That sample application is written in Python . People reading my articles on the Robot Framework might guess why I really have to laugh about this. The Robot Framework is written in Python, but also supports Java. And I am dodging Python ever since my first contact with the Robot Framework more than six or seven years ago. And now these MongoDB lessons must be done using Python. Hey, I am not sure if this will be summed up on the positive side of this exercise. But anyway I wanted to do something different and then why not even Python.

At least the first framework shown seems to be pretty neat. It’s called Bottle and turns out to be an extremely easy way of doing some web-based development. Obviously also some kind of driver is required as the next thing to connect to MongoDB from Python. That driver is PyMongo . Again things look pretty straightforward when it comes to a first sample program. It simply retrieves one data row from our MongoDB database (did I already say our MongoDB?).

1import pymongo
3from pymongo import Connection
4connection = Connection('localhost', 27017)
6db = connection.test
8names = db.names
12print item['name']

Executing that script using Python returns then “thomas”, which was the name we added in the MongoDB shell beforehand.

Looking at the forthcoming lectures I can see a complete crash course in Python, but no worries, I will not bore you to death with too many details on that stuff, but will put my focus back on MongoDB. (At least for this blog post. I do have the feeling I have some heavy Python-work in front of me.)

JSON and JavaScript to the rescue

I would not really say that I am the biggest expert in SQL. But over the time I learned some bits and pieces. Inserting data to tables, querying it and changing it feels quite natural in some SQL shell. And of course then there is this lucky feeling doing some of the more complicated queries involving a lot of table-joins for example. Looking at the examples here I can simply forget about all this. Everything that seems to matter in the MongoDB shell is: JSON and JavaScript

Remember the new element we created a few paragraphs back using:


Changing that entry using the shell is as different from SQL as some Borg drone from Q :

> var j = db.names.findOne()
> = 'seven'
> db.names.find()
{ "_id" : ObjectId("50916fb5ddb9073fa519a952"), "name" : "seven" }

It simply means learning a completely new syntax unless one is already well familiar with JavaScript and JSON. Luckily the MongoDB shell offers auto-completion which makes things of course much easier for a beginner. I am curious how more complicated “queries” will look like. For the representation of the results I suspect that it will be a little bit messy in this JSON representation in comparison to a – formatted – table representation. Of course this remains to be seen.
Ok, ok, the second I am writing about the messy representation I am learning about “db.names.find().pretty()”, which will do some nice formatting on the results retrieved.

“Schema? What Schema?” – Quotes from the course

The more I am digging into the JSON-part of this the more I like it I have to say. I like JSON being a standard . And I like its simplicity as you basically only have two different structures:

  • Dictionaries – Those are basically nothing than associated lists, aka keyword-value pairs and
  • Arrays – Those are a list of values, where a value can as well be again an Array or a Dictionary.

I will finish this by trying to create a more complicated object on my own (before I silently catch up with my ten lessons on Python I left out so far).

>{'type':'ships','class':'intrepid','ships':['USS Evans','USS Lincoln',
  {'name':'USS Voyager','captain':'Kathryn Janeway'}]})
> db.startrek.find().pretty()
    "_id" : ObjectId("50925e96ddb9073fa519a953"),
    "type" : "ships",
    "class" : "intrepid",
    "ships" : [
        "USS Evans",
        "USS Lincoln",
            "name" : "USS Voyager",
            "captain" : "Kathryn Janeway"

So we have an object that contain key:value pairs, an array and inside that array another dictionary. At the same time this is a good example for the pretty-print feature of the MongoDB shell.

That’s it for catching up with the first week of my MongoDB class (beside tons of Python stuff, did I mention this already?). I have to say I enjoyed it (more than I expected ;)) and hopefully you did reading this article.

To be continued …

The MongoDB class series

Part 1 – MongoDB: First Contact
Part 2 – MongoDB: Second Round
Part 3 – MongoDB: Close Encounters of the Third Kind
Part 4 – MongoDB: I am Number Four
Part 5 – MongoDB: The Fith Element
Part 6 – MongoDB: The Sixth Sense
Part 7 – MongoDB: Tutorial Overview and Ref-Card

Java Supplemental Series

Part 1 – MongoDB: Supplemental – GRIDFS Example in Java
Part 2 – MongoDB: Supplemental – A complete Java Project – Part 1
Part 3 – MongoDB: Supplemental – A complete Java Project – Part 2

share post




More articles in this subject area

Discover exciting further topics and let the codecentric world inspire you.


Gemeinsam bessere Projekte umsetzen.

Wir helfen deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.