Cheatsheet: Mongo DB Commands

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

CommandDescription
$ mkdir data $ echo ‘mongod –bind_ip=$IP –dbpath=data –nojournal –rest “$@”‘ > mongod $ chmod a+x mongodSetup Mongo to use a dir “data”
./mongodStart mongo server
mongoStart mongo shell
show dbsPrint a list of all databases on the server.
use dbNameSwitch current database to db. The mongo shell variable db is set to the current database.
show collectionsPrint 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. e.g. db.dogs.insert({name: “Victory”})
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 pathRepairs the mongo db if incorrectly shut down.
Other useful mongo Links:

Mongo command reference: https://docs.mongodb.com/manual/reference/mongo-shell/

Setup mongo on cloud 9: https://community.c9.io/t/setting-up-mongodb/1717