Friday, January 31, 2014

Personal mongodb Cheatsheet

I'm trying to learn more mongodb (taking one of their free courses), so I figured I'll keep here the things that I use often (and maybe it will be useful to others).

Printing

You can use the print method to print in the shell print('hello')

Iterating over a cursor

The value returned by find and findOne is not a collection but a 'cursor', so you can't just do 
for(var obj in db.xyz.find())
you need to do
var cursor=db.xyz.find()
while(cursor.hasNext()) {
  obj=cursor.next();
  ...
}

or define a function and use forEach()