Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MongoDB Cheat Sheet

MongoDB Cheat Sheet

Show All Databases

show dbs

Show Current Database

db

Create Or Switch Database

use acme

Drop

db.dropDatabase()

Create Collection

db.createCollection('posts')

Show Collections

show collections

Insert Row

db.posts.insert({
  title: 'Post One',
  body: 'Body of post one',
  category: 'News',
  tags: ['news', 'events'],
  user: {
    name: 'John Doe',
    status: 'author'
  },
  date: Date()
})

Insert Multiple Rows

db.posts.insertMany([
  {
    title: 'Post Two',
    body: 'Body of post two',
    category: 'Technology',
    date: Date()
  },
  {
    title: 'Post Three',
    body: 'Body of post three',
    category: 'News',
    date: Date()
  },
  {
    title: 'Post Four',
    body: 'Body of post three',
    category: 'Entertainment',
    date: Date()
  }
])

Get All Rows

db.posts.find()

Get All Rows Formatted

db.find().pretty()

Find Rows

db.posts.find({ category: 'News' })

Sort Rows

# asc
db.posts.find().sort({ title: 1 }).pretty()
# desc
db.posts.find().sort({ title: -1 }).pretty()

Count Rows

db.posts.find().count()
db.posts.find({ category: 'news' }).count()

Limit Rows

db.posts.find().limit(2).pretty()

Chaining

db.posts.find().limit(2).sort({ title: 1 }).pretty()

Foreach

db.posts.find().forEach(function(doc) {
  print("Blog Post: " + doc.title)
})

Find One Row

db.posts.findOne({ category: 'News' })

Find Specific Fields

db.posts.find({ title: 'Post One' }, {
  title: 1,
  author: 1
})

Update Row

db.posts.update({ title: 'Post Two' },
{
  title: 'Post Two',
  body: 'New body for post 2',
  date: Date()
},
{
  upsert: true
})

Update Specific Field

db.posts.update({ title: 'Post Two' },
{
  $set: {
    body: 'Body for post 2',
    category: 'Technology'
  }
})

Increment Field ($inc)

db.posts.update({ title: 'Post Two' },
{
  $inc: {
    likes: 5
  }
})

Rename Field

db.posts.update({ title: 'Post Two' },
{
  $rename: {
    likes: 'views'
  }
})

Delete Row

db.posts.remove({ title: 'Post Four' })

Sub-Documents

db.posts.update({ title: 'Post One' },
{
  $set: {
    comments: [
      {
        body: 'Comment One',
        user: 'Mary Williams',
        date: Date()
      },
      {
        body: 'Comment Two',
        user: 'Harry White',
        date: Date()
      }
    ]
  }
})

Find By Element in Array ($elemMatch)

db.posts.find({
  comments: {
     $elemMatch: {
       user: 'Mary Williams'
       }
    }
  }
)

Add Index

db.posts.createIndex({ title: 'text' })

Text Search

db.posts.find({
  $text: {
    $search: "\"Post O\""
    }
})

Greater & Less Than

db.posts.find({ views: { $gt: 2 } })
db.posts.find({ views: { $gte: 7 } })
db.posts.find({ views: { $lt: 7 } })
db.posts.find({ views: { $lte: 7 } })
@delias

This comment has been minimized.

Copy link

@delias delias commented Jul 23, 2019

You are the best brother!

@cakesforfree

This comment has been minimized.

Copy link

@cakesforfree cakesforfree commented Jul 25, 2019

Gracias!

@hansgarcia96

This comment has been minimized.

Copy link

@hansgarcia96 hansgarcia96 commented Jul 28, 2019

This is AMAZING!!!!

@LucasPLopes

This comment has been minimized.

Copy link

@LucasPLopes LucasPLopes commented Aug 5, 2019

Really helpful, thanks Brad.

@ChousinRahit

This comment has been minimized.

Copy link

@ChousinRahit ChousinRahit commented Aug 5, 2019

Thank you soo much Brad

@dmahdiii

This comment has been minimized.

Copy link

@dmahdiii dmahdiii commented Aug 7, 2019

great Job , thank u Brad but in Mongodb we have a document instead of a row, it can be confusing

@bessa3301

This comment has been minimized.

Copy link

@bessa3301 bessa3301 commented Aug 7, 2019

Brad is the best

@cindyledev

This comment has been minimized.

Copy link

@cindyledev cindyledev commented Aug 7, 2019

Thank you!

@Giyomeh

This comment has been minimized.

Copy link

@Giyomeh Giyomeh commented Aug 10, 2019

Thanks!

@JohnZaccari

This comment has been minimized.

Copy link

@JohnZaccari JohnZaccari commented Aug 13, 2019

You're the man!

@Isaac-Tait

This comment has been minimized.

Copy link

@Isaac-Tait Isaac-Tait commented Aug 20, 2019

Thank you :D

@thenaveensaggam

This comment has been minimized.

Copy link

@thenaveensaggam thenaveensaggam commented Aug 22, 2019

You are always the best Brad..

@TechLead-21

This comment has been minimized.

Copy link

@TechLead-21 TechLead-21 commented Aug 26, 2019

👏

@praveeng1618

This comment has been minimized.

Copy link

@praveeng1618 praveeng1618 commented Sep 4, 2019

brad the best

@SaifRehman99

This comment has been minimized.

Copy link

@SaifRehman99 SaifRehman99 commented Sep 7, 2019

You're Love BRAD!

@SimonShameem

This comment has been minimized.

Copy link

@SimonShameem SimonShameem commented Sep 9, 2019

You are the best Brad!

@richardantao

This comment has been minimized.

Copy link

@richardantao richardantao commented Sep 16, 2019

My man!

@satisdrms

This comment has been minimized.

Copy link

@satisdrms satisdrms commented Sep 16, 2019

nothing but awesomeness!!

@MarcoAlejandro

This comment has been minimized.

Copy link

@MarcoAlejandro MarcoAlejandro commented Sep 24, 2019

Thanks!!

@RUPAK7406

This comment has been minimized.

Copy link

@RUPAK7406 RUPAK7406 commented Sep 24, 2019

Thanks for doing a tutorail on updated version of MongoDB

@Freshi

This comment has been minimized.

Copy link

@Freshi Freshi commented Sep 24, 2019

Thanks Brad...really helpful.

@talha-ghaffar

This comment has been minimized.

Copy link

@talha-ghaffar talha-ghaffar commented Oct 6, 2019

Thank you Brad, for so much help.

@truestbyheart

This comment has been minimized.

Copy link

@truestbyheart truestbyheart commented Oct 9, 2019

Thanks a lot

@NadirTellai

This comment has been minimized.

Copy link

@NadirTellai NadirTellai commented Oct 10, 2019

thank you Brad,
but just in Find By Element in Array ($elemMatch) I think it is user rather than author.

@bradtraversy

This comment has been minimized.

Copy link
Owner Author

@bradtraversy bradtraversy commented Oct 11, 2019

thank you Brad,
but just in Find By Element in Array ($elemMatch) I think it is user rather than author.

Fixed, thank you

@bharathasdev

This comment has been minimized.

Copy link

@bharathasdev bharathasdev commented Oct 21, 2019

Thanks Brad! really appreciated

@yassineakermi

This comment has been minimized.

Copy link

@yassineakermi yassineakermi commented Oct 21, 2019

Thanks, Brad! You're the developers' hero!

@bilalkhanamin

This comment has been minimized.

Copy link

@bilalkhanamin bilalkhanamin commented Oct 25, 2019

Thanks man!

@izzaturrahman19

This comment has been minimized.

Copy link

@izzaturrahman19 izzaturrahman19 commented Oct 30, 2019

Thanks !!

@theblackcoder33

This comment has been minimized.

Copy link

@theblackcoder33 theblackcoder33 commented Nov 2, 2019

It should be db.posts.find().pretty() instead of db.find().pretty(). You get an error if that posts isn't there.

@m-antoni

This comment has been minimized.

Copy link

@m-antoni m-antoni commented Nov 5, 2019

Than you Sir Brad

@zchebil

This comment has been minimized.

Copy link

@zchebil zchebil commented Nov 10, 2019

Thanks for your job
for the Get All Rows Formatted db.posts.find().pretty()
and not db.find().pretty()

@suprth

This comment has been minimized.

Copy link

@suprth suprth commented Nov 14, 2019

Thanks Brad!

@walter-clayton

This comment has been minimized.

Copy link

@walter-clayton walter-clayton commented Dec 1, 2019

Thanks Man!

@roneicostajr

This comment has been minimized.

Copy link

@roneicostajr roneicostajr commented Dec 6, 2019

Can't express how awesome you're man, thank you

@Hungsolo3241

This comment has been minimized.

Copy link

@Hungsolo3241 Hungsolo3241 commented Dec 12, 2019

Awesome sauce. Thank you for your service

@DezDoss

This comment has been minimized.

Copy link

@DezDoss DezDoss commented Dec 14, 2019

Thaks, man. Great job!!!

@addegbenga

This comment has been minimized.

Copy link

@addegbenga addegbenga commented Dec 15, 2019

this is great man thanks

@zahqresh

This comment has been minimized.

Copy link

@zahqresh zahqresh commented Dec 23, 2019

This is really helping!!!

@samofoke

This comment has been minimized.

Copy link

@samofoke samofoke commented Dec 24, 2019

thanks very much for the MongoDB Cheat Sheet it helps a lot.

@FormidablePencil

This comment has been minimized.

Copy link

@FormidablePencil FormidablePencil commented Jan 3, 2020

Thanks Brad

@HDB-1

This comment has been minimized.

Copy link

@HDB-1 HDB-1 commented Jan 7, 2020

Thanks Brad, really useful. Just to check... for the "Get all rows formatted", do you need to add "posts" as to make the command: "db.posts.find().pretty()"?

@7issouDev

This comment has been minimized.

Copy link

@7issouDev 7issouDev commented Jan 8, 2020

thank your brad for this gist.

@jbactual

This comment has been minimized.

Copy link

@jbactual jbactual commented Jan 24, 2020

I'm sitting here watching your MongoDB Crash Course and thought about how cool it would be to have a cheat sheet and here it is. That's great! Thanks man!

@Patwan

This comment has been minimized.

Copy link

@Patwan Patwan commented Jan 27, 2020

Thanks alot

@hapizal

This comment has been minimized.

Copy link

@hapizal hapizal commented Feb 17, 2020

Nice, thanks man!

@Helloiain

This comment has been minimized.

Copy link

@Helloiain Helloiain commented Feb 25, 2020

This is super useful, thanks man.

@NeuronButter

This comment has been minimized.

Copy link

@NeuronButter NeuronButter commented Feb 27, 2020

2020 and still a great reference. Thanks Brad!

@SoumyaRoy95

This comment has been minimized.

Copy link

@SoumyaRoy95 SoumyaRoy95 commented Mar 8, 2020

Thank you Brad :D

@varundeva

This comment has been minimized.

Copy link

@varundeva varundeva commented Mar 8, 2020

Thanks

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Mar 21, 2020

Thank you so much!

@bltzr75

This comment has been minimized.

Copy link

@bltzr75 bltzr75 commented Mar 24, 2020

Thank you Brad!!!

@prwhoeatsnonstop

This comment has been minimized.

Copy link

@prwhoeatsnonstop prwhoeatsnonstop commented Apr 2, 2020

Thank you Brad!

@noehoro

This comment has been minimized.

Copy link

@noehoro noehoro commented Apr 2, 2020

Thanks!

@saltlake1

This comment has been minimized.

Copy link

@saltlake1 saltlake1 commented Apr 3, 2020

U r just awesome. Thanks Brad

@yl3m

This comment has been minimized.

Copy link

@yl3m yl3m commented Apr 8, 2020

Thanks Brad, really useful!

@CJMaxwell

This comment has been minimized.

Copy link

@CJMaxwell CJMaxwell commented Apr 11, 2020

Many thanks Brad

@Debayon

This comment has been minimized.

Copy link

@Debayon Debayon commented Apr 21, 2020

Thank You Brad!

@jackradan

This comment has been minimized.

Copy link

@jackradan jackradan commented Apr 21, 2020

tnx

@theSLWayne

This comment has been minimized.

Copy link

@theSLWayne theSLWayne commented Apr 22, 2020

Thanks a lot, mate!!

@zenUnicorn

This comment has been minimized.

Copy link

@zenUnicorn zenUnicorn commented Apr 24, 2020

Thanks Brad! really appreciate this a lot

@Parvathy84

This comment has been minimized.

Copy link

@Parvathy84 Parvathy84 commented Apr 24, 2020

Thanks! This is very useful.

@Nxele

This comment has been minimized.

Copy link

@Nxele Nxele commented Apr 25, 2020

your teaching skills are something else my bother thanks alot.

@ingridinara

This comment has been minimized.

Copy link

@ingridinara ingridinara commented Apr 26, 2020

Thank You!

@tugumeandree

This comment has been minimized.

Copy link

@tugumeandree tugumeandree commented Apr 26, 2020

Thank you, bro

@emlycool

This comment has been minimized.

Copy link

@emlycool emlycool commented Apr 29, 2020

Thanks a lot, just what I needed. So useful

@geekaurora

This comment has been minimized.

Copy link

@geekaurora geekaurora commented May 4, 2020

Thanks!

@copdev

This comment has been minimized.

Copy link

@copdev copdev commented May 9, 2020

Thanks a lot brad.

@Igor2122

This comment has been minimized.

Copy link

@Igor2122 Igor2122 commented May 10, 2020

gracias amigo :), u are awesome !!!

@SudiDav

This comment has been minimized.

Copy link

@SudiDav SudiDav commented May 10, 2020

Merci beaucoup!!!!👌👍

@HiroNakamura

This comment has been minimized.

Copy link

@HiroNakamura HiroNakamura commented May 17, 2020

db.github.find({ message:'Thanks'}) 👍

@mahfuzlafin

This comment has been minimized.

Copy link

@mahfuzlafin mahfuzlafin commented May 19, 2020

Thanks a lot, brad.

@Hollyn

This comment has been minimized.

Copy link

@Hollyn Hollyn commented May 20, 2020

Awesome! Thanks a lot

@niksm7

This comment has been minimized.

Copy link

@niksm7 niksm7 commented May 24, 2020

Thank you so much!😃

@vicmwass

This comment has been minimized.

Copy link

@vicmwass vicmwass commented May 31, 2020

Great!!!...and Thanks

@shraddhatawde

This comment has been minimized.

Copy link

@shraddhatawde shraddhatawde commented May 31, 2020

Thanx a lot!

@anis1011

This comment has been minimized.

Copy link

@anis1011 anis1011 commented Jun 8, 2020

Thanks a lot

@Paula2001

This comment has been minimized.

Copy link

@Paula2001 Paula2001 commented Jun 12, 2020

very useful thanks

@mannawar

This comment has been minimized.

Copy link

@mannawar mannawar commented Jun 14, 2020

Thanks, Under Count
"db.posts.find({ category: 'news' }).count()" should be "db.posts.find({ category: 'News' }).count()"

@Shah-Shishir

This comment has been minimized.

Copy link

@Shah-Shishir Shah-Shishir commented Jun 21, 2020

Thanks Brad

@mamun919

This comment has been minimized.

Copy link

@mamun919 mamun919 commented Jul 2, 2020

Thanks for your great work!

@atarek12

This comment has been minimized.

Copy link

@atarek12 atarek12 commented Jul 14, 2020

Thanks Brad, you really do great work

@dulzeedg

This comment has been minimized.

Copy link

@dulzeedg dulzeedg commented Jul 20, 2020

Thank You

@sanjujus

This comment has been minimized.

Copy link

@sanjujus sanjujus commented Jul 24, 2020

Thank you so very much Brad.

@theashggl

This comment has been minimized.

Copy link

@theashggl theashggl commented Jul 25, 2020

Is mongo used as Firebase for storing data online and using other services that firebase has or are these just for local data storage?

@XiovV

This comment has been minimized.

Copy link

@XiovV XiovV commented Jul 26, 2020

@theashggl

Is mongo used as Firebase for storing data online and using other services that firebase has or are these just for local data storage?

Mongo is used for storing data, you can use it to store data locally or put it on a server and have your backend interact with it. It's kind of like using Firebase for storing stuff online.

@neha-nayeem

This comment has been minimized.

Copy link

@neha-nayeem neha-nayeem commented Aug 7, 2020

Thank you! Under 'Get all Rows Formatted', should it be:
db.posts.find().pretty()
rather than:
db.find().pretty() ?

@cmjagtap

This comment has been minimized.

Copy link

@cmjagtap cmjagtap commented Aug 16, 2020

Thanks

@Pradip-p

This comment has been minimized.

Copy link

@Pradip-p Pradip-p commented Aug 21, 2020

Thanks !! it's really helpful for me !!

@raghuwanshisatyam

This comment has been minimized.

Copy link

@raghuwanshisatyam raghuwanshisatyam commented Aug 30, 2020

thank-you brother

@AnykeyAnydoor

This comment has been minimized.

Copy link

@AnykeyAnydoor AnykeyAnydoor commented Aug 31, 2020

app.get(‘/posts/:id’, (req, res) => {
Database.run(‘SELECT * FROM ITEMS WHERE ID=’ + req.params.id,(err, result) => {
res.json(result);
});
});

@AbhiruchiMaurya

This comment has been minimized.

Copy link

@AbhiruchiMaurya AbhiruchiMaurya commented Sep 2, 2020

Thanks!!

@aldrinjenson

This comment has been minimized.

Copy link

@aldrinjenson aldrinjenson commented Sep 3, 2020

Thank you Brad!

@patman007

This comment has been minimized.

Copy link

@patman007 patman007 commented Sep 11, 2020

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.