In this post, we will see a comprehensive list of all the MongoDB commands you will ever need as a MongoDB beginner. This list covers almost all the most used commands in MongoDB.
Database Commands
- View all databases
show dbs
- Create a new or switch databases
show dbs
- View current Database
db
- Delete Database
db.dropDatabase()
These are the basic Database Commands of MongoDb.
I will assume that you are working inside a collection(tables in sql) named 'comments' on a MongoDB database of your choice
Collection Commands
- Show Collections
show collections
- Create a collection named 'comments'
db.createCollection('comments')
- Drop a collection named 'comments'
db.comments.drop()
Row(Document) Commands
- Show all Rows in a Collection
db.comments.find()
- Show all Rows in a Collection (Prettified)
db.comments.find().pretty()
- Find the first row matching the object
db.comments.findOne({name: 'yash'})
- Insert One Row
db.comments.insert({
'name': 'yash',
'lang': 'JavaScript',
'meme': 1
})
- Insert many Rows
db.comments.insertMany([{
'name': 'yash',
'lang': 'JavaScript',
'meme': 1
},
{'name': 'Rohan',
'lang': 'Python',
'meme': 3
},
{'name': 'jagu',
'lang': 'Java',
'meme': 4
}])
- Search in a MongoDb Database
db.comments.find({lang:'Python'})
- Limit the number of rows in output
db.comments.find().limit(2)
- Count the number of rows in the output
db.comments.find().count()
- Update a row
db.comments.update({name: 'Teja'},
{'name': 'yash',
'lang': 'JavaScript',
'meme': 1
}, {upsert: true})
- Mongodb Increment Operator
db.comments.update({name: 'Rohan'},
{$inc:{
meme: 2
}})
- Mongodb Rename Operator
db.comments.update({name: 'Rohan'},
{$rename:{
meme: 'member'
}})
- Delete Row
db.comments.remove({name: 'yash'})
- Less than
db.comments.find({member_since: {$lt: 90}})
- Less than equals to
db.comments.find({member_since: {$lte: 90}})
- Greater than
db.comments.find({member_since: {$gt: 90}})
- Greater than equals to
db.comments.find({member_since: {$gte: 90}})
These are all basic MongoDb commands
Top comments (0)