Skip to content

Instantly share code, notes, and snippets.

@Yogendra0Sharma
Created January 10, 2017 10:33
Show Gist options
  • Save Yogendra0Sharma/6e1a111b09786ed804b95f73a643331a to your computer and use it in GitHub Desktop.
Save Yogendra0Sharma/6e1a111b09786ed804b95f73a643331a to your computer and use it in GitHub Desktop.
MongoDB Commands

Basic Mongo shell commands

mongo             //Start mongo shell
show dbs          //Display databases
use mydb          //Start using 'mydb' database
db                //Display current database
show collections  //Display collections
help              //help

Create and insert a document into the collection "testData"

j = { name : "mongo" }
db.testData.insert( j )

// or you can

db.testData.insert({ name : "mongo" })

save a data

db.products.save( { name: "kevin", age: 40 } )
db.products.save( { name: "kevin", age: 40 }, { name: "colin", age: 45 } )

Retrieve data from dataabse

db.testData.find()                     //Retrieve all data in the collection "testData"
db.testData.findOne()                  //Retrieve the first data found
db.testData.find({"name": "kevin"})   //Retrieve the specific data (name field is equal to "kevin")
db.testData.find().limit(3)           //Retrieve the data limited to 3 documents

Remove data

db.testData.remove({})                  //Remove all data from this collection
db.testData.remove({"name": "kevin"})   //Remove the data which has name "kevin"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment