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. 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 path | Repairs 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