Skip to content

Instantly share code, notes, and snippets.

@maestrow
Last active August 29, 2015 14:26
Show Gist options
  • Save maestrow/cae8cd6bcd41a0db745d to your computer and use it in GitHub Desktop.
Save maestrow/cae8cd6bcd41a0db745d to your computer and use it in GitHub Desktop.

MongoDB is:

  • JSON storage
  • schemaless
  • NOT support SQL

Functionality that missing in MongoDB (in order to retain scalability):

  • Joins between collections
  • Transactions across multiple collections

Console tools

mongoimport -d course -c grades grades.json

Mongo Shell

mongo shell is a complete JavaScript environment so we can write: for (var i=0; i<10; i++) db.things.insert({index: i});

mongo script.js - run script

Collection Methods

  • insert or save
  • find
  • find(...).pretty()
  • findOne
  • count
  • update
  • remove
  • drop

BSON

Data constructors:

  • NumberInt(1)
  • NumberLong(4)
  • ISODate("2012-10-21T17:20:09.759Z")

Cursurs

  • sort({name:-1})
  • limit(5)
  • skip(10)

nodejs methods:

  • each
  • toArray

Update

  • db.persons.update(criteria, newObj, options)
  • db.persons.update(criteria, operatorObj, options)
  • options: {multi:true}, {upset: true}
  • $set
  • $unset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment