Mongo from "humongous" (very big).


Why to use mongoDB

  • High performance read and write
  • Flexible schema
  • Need replication
  • Start small and fast and grow easy
  • High availability
  • Strong data consistency
  • mongoDB scales horizontally (Vertical scale: Get a better server, Horizontal scale: Get more servers)
  • Replication is easy and good

Install and Start the database

Install mongoDB on Windows.

Download and unpack or install it to a folder. You also need a folder where mongodb may store the data, in this example we created a folder at p:mongo_test_db

# mongod.exe --dbpath p:mongo_test_db

This needs to be running the whole time.

Now start in a different window a client

# mongo.exe

And try the Getting Started Tutorial.

Connect to differnt computer


Command to connect to a single database in Mongo

mongos> show dbs
mongos> use mydb
switched to db mydb
mongos> show collections


mongoDB Commands

Create to documents (in SQL: insert new rows)

> j = { name : "mongo" }
> k = { x : 3 }

and store them into a collection (in SQL: store them into a table)

> db.testData.insert( j )
WriteResult({ "nInserted" : 1 })
> db.testData.insert( k )
WriteResult({ "nInserted" : 1 })

What collections do we have

> SHOW collections


Show content of collection

> db.testData.find()
{ "_id" : ObjectId("54bd1995fe284fb914531bbc"), "name" : "mongo" }
{ "_id" : ObjectId("54bd199afe284fb914531bbd"), "x" : 3 }

Show documents where x=3

> db.testData.find( { "x" : 3 } )
{ "_id" : ObjectId("54bd199afe284fb914531bbd"), "x" : 3 }

You can also interate over the result with the a cursor

> var c = db.testData.find()
> while ( c.hasNext() ) printjson( )

See also Iterate the Returned Cursor

Jump directly to a result at a given position

> var c = db.testData.find()
> printjson( c [ 2 ] )
{ "_id" : ObjectId("54bd2adefe284fb914531bbe"), "x" : 10 }

> printjson( c [ 99 ] )

Maybe you want to sort the data

> db.testData.find().sort( { x: -1 } )

Find exactly one document

> db.testData.findOne()

Regular expressions (like search)

db.getCollection('categories').find({"message": /.*exception.*/})

Find in sub fields

db.getCollection('categories').find({ "": "Doe" })

Find all rows where a field exists


Output only some fields This will only print the name field found within the product field. It will even exclude the id which is normally always included.

db.getCollection('cars').find({}, {"":1, _id:0} )

JavaScript in Mongo

Use a JavaScript For Loop to insert data, e.g. for tests

> FOR (var i = 10; i <= 35; i++) {
   db.testData.insert( { x : i } )

> db.testData.find()

You can store JavaScript functions in your .mongorc.js file to have it started when you start the mongoDB shell.

Export / Import

mongoexport -h --db myDb --collection myCollection --out  /tmp/myexport.json
mongoimport -h --db myDb --collection myCollection --file /tmp/myexport.json



Like in relational databases you can reference in one attribute another entry

"_id":        42,
"name":       "John Doe",
"address_id": 37

"_id":     37,
"city":    "New York",
"country": "USA",
"zip-code": "1234",

> db.testData.insert( a )
> db.testData.insert( c )

But mongoDB does not provide joins (you have to resolve them yourself) and this very common strategy in the relational world is often discouraged here.


Consider to avoid referencing but use embedding. This will improve the performance but has the disadvantage that you might have to change the same information in different places. E.g. when the zipcode changes you have to change in all the affected Persons instead of only fixing one Address entry. An embedded version might look like this

"_id":        42,
"name":       "John Doe",
"address": {
  "_id":     37,
  "city":    "New York",
  "country": "USA",
  "zip-code": "1234",

One to Many

One person may have many phone numbers. You may use arrays for this

"_id":        42,
"name":       "John Doe",
"phone": [
   "type":   "work",
   "number": "12345"
   "type":   "home",
   "number": "2468"  


Add an Array at least to one side and reference the others.



Hibernate and mongoDB


How to create an Index