Cheatsheet: Mongo DB Commands

A table of some useful mongo commands. These were used and tested on Cloud 9.

| Command | Description | | -------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- | | $ mkdir data $ echo ‘mongod –bind_ip=$IP –dbpath=data –nojournal –rest “$@”‘ > mongod $ chmod a+x mongod | Setup Mongo to use a dir “data” | | ./mongod | Start mongo server | | mongo | Start mongo shell | | show dbs | Print a list of all databases on the server. | | use dbName | Switch current database to db. The mongo shell variable db is set to the current database. | | show collections | Print a list of all collections for current database. | | db.collection.find() | Find all documents in the collection and returns a cursor. | | db.collection.insert() | Insert a new document into the collection. | | db.collection.update() | Update an existing document in the collection. | | db.collection.save() | Insert either a new document or update an existing document in the collection. | | db.collection.remove() | Delete documents from the collection. | | db.collection.drop() | Drops or removes completely the collection. | | mongod –repair –repairpath path | Repairs the mongo db if incorrectly shut down. |

Other useful mongo Links:

Mongo command reference

Setup mongo on cloud 9