MongoDB Shell 🧵🍃
What is MongoDB Shell?
MongoDB shell is a CLI(Command Line Tool) which is used to access databases, collections, and documents present in MongoDB.
To start it, we just have to open the terminal and type:
sudo systemctl start mongod
To open the mongo shell we have to open the terminal and type:
mongo
Now you are in the mongo shell.
Mongo Shell Commands
To see all the databases:
show dbs
To create a new database/To switch to any existing database:
use Name-Of-Database
To see all the collections of a database:
show collections
To create a new collection in a database:
db.createCollection(Name-of-new-collection)
To see all the documents of a collection:
db.Name-of-Collection.find()
To find a document matching the query:
db.Name-of-Collection.find({key:value})
To find the first occurrence of the document:
db.Name-of-Collection.findOne({key:value})
To insert a document in the collection:
db.Name-of-Collection.insert({key:value})
To insert multiple documents in a collection:
db.Name-of-Collection.insert( [ {key:value}, {key:value}, {key:value} .... ] )
To update any document in a collection:
db.Name-of-Collection.update({key:value},{key:value});