MongoDB Commands
Commands
mongod- start mongo db server instancemongo- start mongo db command prompt to execute commandsshow dbs- show databasesmongoimport -d [DATABASE_NAME] -c [COLLECTIONS] --file [FILE_NAME]- import into database and collection from filemongoexport -d [DATABASE_NEM] -c [COLLECTIONS] --out [FILE_NAME]- export db in jsonmongodump- binary exportmongorestore- restore binary exportbsondumpmongostatuse [DB_NAME]- change/switch DB. There is NO command to create DBdb- set variable as DB (after user [DB_NAME])db.[COLLECTIONS]- e.g. db.links. Don't have to create collections (similar to DB)db.[COLLECTIONS].count()- to check number of documents. e.g. db.links.count()db.[COLLECTIONS].insert({[JSON_DATA]})- e.g. db.links.insert({title: "google search", url:"www.google.com", comment:"top search engine", tags:["default","main page"], saved_on: new Date()})db.[COLLECTIONS].save({[JSON_DATA]})- e.g. db.links.save({title: "yahoo search", url: "www.yahoo.com", saved_on: new Date()})db.[COLLECTIONS].find()- e.g. db.links.find(). Query the database to find the documents
Examples
show collections- show collections for dbdb.users.insert({"name":"username"})- insert collection user with datadb.users.count()- show count of collectionsdb.users.find()- find all data from collectiondb.users.find().explain()- explain execution planner for query to find datadb.users.find().explain("executionStats")- more information on explaindb.users.createIndex({number:1})- to create an index on collectiondb.users.update({"name":"username"}, $set: {"first_name":"Name"})"- $set update recorddb.users.update({"name":"username"}, {$addToSet: {"hobbies":"running"}})"- $addToSet update array with new valuedb.users.remove({"name":"username"})- remove recorddb.users.drop()- drop entire users collection
Notes
- bson: binary JSON
 - Collection: Like an RDBMS table; but collection has no schemas.
 - Document: Like an RDBMS record or row. There are bson documents.
 - Field: Like an RDBMS column; {key: value}