0% found this document useful (0 votes)
149 views1 page

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

learn and enjoy

Uploaded by

Darshan Shewale
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)
149 views1 page

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

learn and enjoy

Uploaded by

Darshan Shewale
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/ 1

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

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'})

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()

Update a row

db.comments.update({name: 'Shubham'},
{'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}})

You need to be logged in to post a comment!

Comments
jhapanepal.np Sep 29, 2021

wow harry bhai

coderkaran Sep 27, 2021

Thank You so much harry sir.

vamsi.gundarapu18 Aug 24, 2021

how to download this cheat sheet

Show 1 replies

meet_777 Aug 14, 2021

create a forum for our community

meet_777 Aug 14, 2021

create a forum for our community

meet_777 Aug 14, 2021

create a forum for our community

adityamangal0202 Jul 31, 2021


Wonderful Blog

baralabhishek538 Jul 16, 2021

Very nice harry da..totally appreciable..thanks for


all your efforts

jidnyasamane9336 Jul 15, 2021


Nice blog

Asawir Jul 10, 2021

Thank you very much Harry bhai

pritamdas.pd2005 Jun 28, 2021


Where are the upsert commands

shivanggup Jun 16, 2021

nice

pragupta2007 Jun 10, 2021


Wow Sir! What a nice try for all of us...

Show 1 replies

qwertysachin3 Jun 7, 2021

WARNING!! Create a collection named


'comments' is needs to be changed to Delete
collection named 'comments'

samuel Jun 6, 2021


THIS COURCE REALLY HELPFULL SIR...!

techtigertham Jun 6, 2021


Thanks harry sir,
Can you please make a video on ruby, please
make video sir

somenath Jun 6, 2021


Thanks harry sir for this awesome tutorial of
MongoDB.
I have only one request to you.
Can you please show how can we connect our
website with mongoDB database please

Show 2 replies

shaikhkahinoor Jun 5, 2021

Sir You are great ...


Mene apka hi C program video dekh kar
programming sikhaa hai.

Avi apka python in one video wo dekh raha hun..

Sir Mongodb ko hum mobile me kese use kar


sakte hai ?

Pritamcode Jun 5, 2021

Harry bhai please please please Hosting ka ek


tutorial bana dijiye with nodejs . Please web
development wale playlist dekha par mere paas
credit card nahi ha. Debit card se nahi hoga kya
ya upi se? Please

Show 4 replies

arkodev Jun 5, 2021


Sir please explain the last three commands of php
tutorial

Show 1 replies

arkodev Jun 5, 2021

Please read the full message. Sir please tell the


roadmap to become an ethically hacker. I know
that you will not teach us that ,as YouTube will not
allow you to do so and that's absolutely ok, no
problem but please give the roadmap like what
are the good courses, books, online tutorials and
website to study hacking. Please don't ignore
read the full message.

Show 1 replies

Copyright © 2020-2021 CodeWithHarry.com

You might also like