0% found this document useful (0 votes)
15 views6 pages

All MongoDb Commands You Will Ever Need (MongoDb Cheatsheet) - CodeWithHarry

Uploaded by

Akshay. Badgujar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

All MongoDb Commands You Will Ever Need (MongoDb Cheatsheet) - CodeWithHarry

Uploaded by

Akshay. Badgujar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CodeWithHarry Menu Login

All MongoDb commands you will ever need (MongoDb


Cheatsheet)
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.
I will assume that you are working inside a collection named 'comments' on a MongoDB
database of your choice

1. Database Commands
View all databases

show dbs

Watch Free Web Series in


Hindi
Explore Amazon miniTV
Watch Free Web Series like Ishq Express,
Crushed, Please Find Attached On miniTV
amazon.in

OPEN

Create a new or switch databases

use dbName

View current Database


db

Delete Database

db.dropDatabase()

2. Collection Commands
Show Collections

show collections

Create a collection named 'comments'

db.createCollection('comments')

Drop a collection named 'comments'

db.comments.drop()

3. 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: 'Harry'})
Explore Amazon miniTV
On Amazon Shopping App
Always Free Always Entertaining. Your
Favourite Web Series On Amazon Shopping
App Now!
amazon.in

OPEN

Insert One Row

db.comments.insert({
'name': 'Harry',
'lang': 'JavaScript',
'member_since': 5
})

Insert many Rows

db.comments.insertMany([{
'name': 'Harry',
'lang': 'JavaScript',
'member_since': 5
},
{'name': 'Rohan',
'lang': 'Python',
'member_since': 3
},
{'name': 'Lovish',
'lang': 'Java',
'member_since': 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()

Explore Amazon miniTV


On Amazon Shopping App
Always Free Always Entertaining. Your
Favourite Web Series On Amazon Shopping
App Now!
amazon.in

OPEN

Update a row

db.comments.updateOne({name: 'Shubham'},
{$set: {'name': 'Harry',
'lang': 'JavaScript',
'member_since': 51
}}, {upsert: true})

Mongodb Increment Operator


db.comments.update({name: 'Rohan'},
{$inc:{
member_since: 2
}})

Mongodb Rename Operator

db.comments.update({name: 'Rohan'},
{$rename:{
member_since: 'member'
}})

Delete Row

db.comments.remove({name: 'Harry'})

Less than/Greater than/ Less than or Eq/Greater than or Eq

db.comments.find({member_since: {$lt: 90}})

db.comments.find({member_since: {$lte: 90}})

db.comments.find({member_since: {$gt: 90}})

db.comments.find({member_since: {$gte: 90}})

Add a new comment

Type Your Comment

Post Comment
Comments (21)

shaikhinamulhaq01 2022-12-01
Badhiya bhai Your content is really useful

REPLY

shoeb4dev_gm 2022-11-29
HARRY BHAI very useful but how can I download this sheet

REPLY

deepak.meena.18002_ii 2022-11-28
How to download this cheat sheet. Harry Bhai, please tell me.

REPLY

zubairsyed928_gm 2022-11-24
ThankYou Bro That's very helpful ❤
REPLY

smrutigml_gm 2022-10-22
Hey Harry, Please add commands for aggregation.

REPLY

CodeWithHarry

Copyright © 2022 CodeWithHarry.com

You might also like