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 commented Jul 23, 2019

You are the best brother!

@cakesforfree

This comment has been minimized.

Copy link

cakesforfree commented Jul 25, 2019

Gracias!

@hansgarcia96

This comment has been minimized.

Copy link

hansgarcia96 commented Jul 28, 2019

This is AMAZING!!!!

@LucasPLopes

This comment has been minimized.

Copy link

LucasPLopes commented Aug 5, 2019

Really helpful, thanks Brad.

@ChousinRahit

This comment has been minimized.

Copy link

ChousinRahit commented Aug 5, 2019

Thank you soo much Brad

@dmahdiii

This comment has been minimized.

Copy link

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 commented Aug 7, 2019

Brad is the best

@cindyledev

This comment has been minimized.

Copy link

cindyledev commented Aug 7, 2019

Thank you!

@Giyomeh

This comment has been minimized.

Copy link

Giyomeh commented Aug 10, 2019

Thanks!

@JohnZaccari

This comment has been minimized.

Copy link

JohnZaccari commented Aug 13, 2019

You're the man!

@Isaac-Tait

This comment has been minimized.

Copy link

Isaac-Tait commented Aug 20, 2019

Thank you :D

@thenaveensaggam

This comment has been minimized.

Copy link

thenaveensaggam commented Aug 22, 2019

You are always the best Brad..

@TechLead-21

This comment has been minimized.

Copy link

TechLead-21 commented Aug 26, 2019

👏

@praveeng1618

This comment has been minimized.

Copy link

praveeng1618 commented Sep 4, 2019

brad the best

@SaifRehman99

This comment has been minimized.

Copy link

SaifRehman99 commented Sep 7, 2019

You're Love BRAD!

@SimonShameem

This comment has been minimized.

Copy link

SimonShameem commented Sep 9, 2019

You are the best Brad!

@richardantao

This comment has been minimized.

Copy link

richardantao commented Sep 16, 2019

My man!

@satisdrms

This comment has been minimized.

Copy link

satisdrms commented Sep 16, 2019

nothing but awesomeness!!

@MarcoAlejandro

This comment has been minimized.

Copy link

MarcoAlejandro commented Sep 24, 2019

Thanks!!

@RUPAK7406

This comment has been minimized.

Copy link

RUPAK7406 commented Sep 24, 2019

Thanks for doing a tutorail on updated version of MongoDB

@Freshi

This comment has been minimized.

Copy link

Freshi commented Sep 24, 2019

Thanks Brad...really helpful.

@talha-ghaffar

This comment has been minimized.

Copy link

talha-ghaffar commented Oct 6, 2019

Thank you Brad, for so much help.

@truestbyheart

This comment has been minimized.

Copy link

truestbyheart commented Oct 9, 2019

Thanks a lot

@NadirTellai

This comment has been minimized.

Copy link

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 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 commented Oct 21, 2019

Thanks Brad! really appreciated

@yassineakermi

This comment has been minimized.

Copy link

yassineakermi commented Oct 21, 2019

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

@bilalkhanamin

This comment has been minimized.

Copy link

bilalkhanamin commented Oct 25, 2019

Thanks man!

@izzaturrahman19

This comment has been minimized.

Copy link

izzaturrahman19 commented Oct 30, 2019

Thanks !!

@theblackcoder33

This comment has been minimized.

Copy link

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 commented Nov 5, 2019

Than you Sir Brad

@zchebil

This comment has been minimized.

Copy link

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 commented Nov 14, 2019

Thanks Brad!

@walter-clayton

This comment has been minimized.

Copy link

walter-clayton commented Dec 1, 2019

Thanks Man!

@roneicostajr

This comment has been minimized.

Copy link

roneicostajr commented Dec 6, 2019

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

@Hungsolo3241

This comment has been minimized.

Copy link

Hungsolo3241 commented Dec 12, 2019

Awesome sauce. Thank you for your service

@DezDoss

This comment has been minimized.

Copy link

DezDoss commented Dec 14, 2019

Thaks, man. Great job!!!

@addegbenga

This comment has been minimized.

Copy link

addegbenga commented Dec 15, 2019

this is great man thanks

@zahqresh

This comment has been minimized.

Copy link

zahqresh commented Dec 23, 2019

This is really helping!!!

@samofoke

This comment has been minimized.

Copy link

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 commented Jan 3, 2020

Thanks Brad

@HDB-1

This comment has been minimized.

Copy link

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 commented Jan 8, 2020

thank your brad for this gist.

@jbactual

This comment has been minimized.

Copy link

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 commented Jan 27, 2020

Thanks alot

@hapizal

This comment has been minimized.

Copy link

hapizal commented Feb 17, 2020

Nice, thanks man!

@Helloiain

This comment has been minimized.

Copy link

Helloiain commented Feb 25, 2020

This is super useful, thanks man.

@NeuronButter

This comment has been minimized.

Copy link

NeuronButter commented Feb 27, 2020

2020 and still a great reference. Thanks Brad!

@SoumyaRoy95

This comment has been minimized.

Copy link

SoumyaRoy95 commented Mar 8, 2020

Thank you Brad :D

@varundeva

This comment has been minimized.

Copy link

varundeva commented Mar 8, 2020

Thanks

@ghost

This comment has been minimized.

Copy link

ghost commented Mar 21, 2020

Thank you so much!

@bltzr75

This comment has been minimized.

Copy link

bltzr75 commented Mar 24, 2020

Thank you Brad!!!

@prwhoeatsnonstop

This comment has been minimized.

Copy link

prwhoeatsnonstop commented Apr 2, 2020

Thank you Brad!

@noehoro

This comment has been minimized.

Copy link

noehoro commented Apr 2, 2020

Thanks!

@latifasaee

This comment has been minimized.

Copy link

latifasaee commented Apr 3, 2020

Thank you Brad!

@saltlake1

This comment has been minimized.

Copy link

saltlake1 commented Apr 3, 2020

U r just awesome. Thanks Brad

@yl3m

This comment has been minimized.

Copy link

yl3m commented Apr 8, 2020

Thanks Brad, really useful!

@CJMaxwell

This comment has been minimized.

Copy link

CJMaxwell commented Apr 11, 2020

Many thanks Brad

@Debayon

This comment has been minimized.

Copy link

Debayon commented Apr 21, 2020

Thank You Brad!

@jackradan

This comment has been minimized.

Copy link

jackradan commented Apr 21, 2020

tnx

@theSLWayne

This comment has been minimized.

Copy link

theSLWayne commented Apr 22, 2020

Thanks a lot, mate!!

@zenUnicorn

This comment has been minimized.

Copy link

zenUnicorn commented Apr 24, 2020

Thanks Brad! really appreciate this a lot

@Parvathy84

This comment has been minimized.

Copy link

Parvathy84 commented Apr 24, 2020

Thanks! This is very useful.

@Nxele

This comment has been minimized.

Copy link

Nxele commented Apr 25, 2020

your teaching skills are something else my bother thanks alot.

@ingridinara

This comment has been minimized.

Copy link

ingridinara commented Apr 26, 2020

Thank You!

@tugumeandree

This comment has been minimized.

Copy link

tugumeandree commented Apr 26, 2020

Thank you, bro

@emlycool

This comment has been minimized.

Copy link

emlycool commented Apr 29, 2020

Thanks a lot, just what I needed. So useful

@geekaurora

This comment has been minimized.

Copy link

geekaurora commented May 4, 2020

Thanks!

@copdev

This comment has been minimized.

Copy link

copdev commented May 9, 2020

Thanks a lot brad.

@Igor2122

This comment has been minimized.

Copy link

Igor2122 commented May 10, 2020

gracias amigo :), u are awesome !!!

@SudiDav

This comment has been minimized.

Copy link

SudiDav commented May 10, 2020

Merci beaucoup!!!!👌👍

@HiroNakamura

This comment has been minimized.

Copy link

HiroNakamura commented May 17, 2020

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

@mahfuzlafin

This comment has been minimized.

Copy link

mahfuzlafin commented May 19, 2020

Thanks a lot, brad.

@Hollyn

This comment has been minimized.

Copy link

Hollyn commented May 20, 2020

Awesome! Thanks a lot

@niksm7

This comment has been minimized.

Copy link

niksm7 commented May 24, 2020

Thank you so much!😃

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.