Notes from M101JS MongoDB for Node.js Developers Courseware

I signed up for the online MongoDB University MongoDB for Node.js Developers course aka M101JS. MongoDB sounds like a promising technology and MongoDB’s courseware uses EdX’s excellent software that I had already used for the MIT 6.002x Electronics course so I was pretty confident of the usefulness of all this.

Work in Progress! I switched from Python to Node.js and started over. I’m currently in:

Introduction to MongoDB

MongoDB is a non relational datastore. It stores something close from JSON called BSON, which stands for Binary JSON. It is schemaless.

The tradeoff to gain scalability and performance is that you have to give up joins and transactions that relational databases provide.

CRUD

In MongoDB vocabulary:

Table is collection. _id is created by default.

Mongo Shell

MongoShell is basically a JavaScript interpreter. You’ll be able to CRUD data but also run simple JavaScript instructions.

Features include:

Connextion to the shell: mongo

Example:

> show dbs
> use mydb

> for (i = 0; i < 3; i++) print("Hello Bootstragram");
Hello Bootstragram
Hello Bootstragram
Hello Bootstragram

> help

> obj = { "a" : 1, "b": "hello", "c": ["apple", "tomatoes"]}

> NumberInt(1)

> NumberLong(1)

> new Date()

> exit

Query Language

_id is returned by default with queries. pretty() helps formatting answers in a readable way. The second argument tells MongoDB the name of the fields we want to fetch.

> db.collection.findOne()
> db.collection.findOne({"name": "Jones"})
> db.collection.findOne({"name": "Jones"}, {"name": true, "_id": false})
> db.scores.find( { type: "essay", score: 50 }, { student: true, _id: false } );

Query operators

Example:

> db.scores.find( { type: { $exists: true } } );

Prefix Query operators

Tricky : db.scores.find( { score : { $gt : 50 }, score : { $lt : 60 } } ); # only less than 60 are returned. Beware that $and is not used, but the 2nd score field value is replacing the 1st score field value (this is kind of a normal dictionary behavior).

Querying inside arrays

Cursor Operations

Processed on the server-side:

Update options

Error feedback

db.runCommand( { getLastError: 1 } )

Dot notation

Don’t forget the double-quotes!

Performance

Basic Usage of Indexes

Creating an index

db.collection.ensureIndex( { my_key: 1 }, opts ) # 1 is ascending, -1 is descinding

opts can be:

Indexes can be created in the background.

Finding existing indexex

db.system.indexes.find()

or

db.collection.getIndexes()

Dropping an index

db.collection.dropIndex( { my_key: 1 } )

Fine Tuning

Explain

Adding .explain() on a query produces a document explaining how the request was executed. Some of the most relevant information of this document are:

Size of an index

Calling .stats() on a collection gives you a document with some interesting information. One particularly important information is the size of your index (you want your index to fit inside the memory) that you can also get directly via .totalIndexSize().

Profiling

http://docs.mongodb.org/manual/reference/method/db.setProfilingLevel/

db.system.profile.find({millis: { $gt: 1000 }}).sort({ts: -1})

http://docs.mongodb.org/manual/reference/program/mongotop/