Skip to content

Instantly share code, notes, and snippets.

@akankshach29
Created August 21, 2017 06:29
Show Gist options
  • Save akankshach29/eace290931cbfc23d102744cf13024c4 to your computer and use it in GitHub Desktop.
Save akankshach29/eace290931cbfc23d102744cf13024c4 to your computer and use it in GitHub Desktop.
Mongo DB commands frequently used
Mongo DB unlike relational database, is a JSON-like document oriented database. The mongo shell is an interface used to add, query, update, etc data.
Here’s a cheat sheet of Mongo Shell commands often used:
1 Login to the server
2 Type `mongo` to get the mongo terminal
To create a new database:
use database_name
To See the list of all database present:
show dbs
To use the database you want to use:
use <dbname>
To see all Collections(group of MongoDB documents) in current database:
show collections
To see all the data present in db:
db.collectionname.find({}).pretty()
To count the existence of a particular data:
db.collectionname.find({"title" : “Some data”}).count()
To find a document:
db.collectionname.find({"title" : “Some data" }).pretty()
To update a single document:
db.collectionname.update({"title" : “Some data" }, { $set: {"title": “New data”}})
To update multiple documents at once:
db.collectionname.update({"title" : “Some data" }, { $set: {"title": “New data"}}, {"multi": true})
To remove any document:
db.collectionname.remove({"title" : “Some data”})
To remove documents not equal to “Some data”:
db.collectionname.remove({"title" : { $ne : “Some data"}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment