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.posts.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 } })
@D3CIPH3R
Copy link

D3CIPH3R commented Aug 3, 2021

Amazing work buddy
I was looking for something like this. thank you

@zubair-kamboh
Copy link

zubair-kamboh commented Aug 26, 2021

Awesome

@Rey79-coder
Copy link

Rey79-coder commented Aug 27, 2021

Thanks!

@DionMS8
Copy link

DionMS8 commented Aug 29, 2021

Thank you so much!

@jimjam-13
Copy link

jimjam-13 commented Sep 2, 2021

Thanks for the cheatsheet bro.

@ccf19881030
Copy link

ccf19881030 commented Sep 5, 2021

Thanks! It's very helpful for me.

@lewisc99
Copy link

lewisc99 commented Sep 13, 2021

thanks a lot hehe

Copy link

ghost commented Sep 27, 2021

These kinds of summaries or cheat sheets are really helpful.
Thank you, bro.

@saddam734
Copy link

saddam734 commented Oct 6, 2021

Thank You BRAD

@tengine8000
Copy link

tengine8000 commented Nov 11, 2021

Thank you Brad. God bless your work!

@Sourav-1234
Copy link

Sourav-1234 commented Dec 5, 2021

Thank you Sir Inspiration

@anish72
Copy link

anish72 commented Dec 15, 2021

Thank you so much....!!

@ahmedabukar01
Copy link

ahmedabukar01 commented Dec 29, 2021

thank you so much brad. we appreciate you

@dmwambua
Copy link

dmwambua commented Jan 9, 2022

thanks

@gerald-encabo
Copy link

gerald-encabo commented Jan 9, 2022

Its 2022 and still very helpful, thank you!!!

@miShelbyT
Copy link

miShelbyT commented Jan 14, 2022

Ditto what @geraldencabo said. Your instructional videos and other materials are a Godsend. Thank you!!!

@ameerhamza0403
Copy link

ameerhamza0403 commented Jan 26, 2022

Thanks

@mrtushartiwari
Copy link

mrtushartiwari commented Feb 3, 2022

Awesome for quick reference.

@KMaleesha
Copy link

KMaleesha commented Mar 12, 2022

Thank you sir

@SilasRodrigues19
Copy link

SilasRodrigues19 commented Mar 13, 2022

Thank you bro

@ispravnic
Copy link

ispravnic commented Mar 15, 2022

Thank you!

@Shehanx86
Copy link

Shehanx86 commented Mar 25, 2022

Thank you <3

@GitFincy
Copy link

GitFincy commented Mar 29, 2022

thank you!

@YitayalT
Copy link

YitayalT commented Apr 1, 2022

thank you!

@connectwithKrishnadas
Copy link

connectwithKrishnadas commented May 7, 2022

👏👏👏

@priyanka657
Copy link

priyanka657 commented May 16, 2022

thanks

@samitkumarsinha
Copy link

samitkumarsinha commented May 24, 2022

Thanks for making life little easier

@Owais28
Copy link

Owais28 commented May 31, 2022

Amazing!! Thank you brother @bradtraversy 😎

@sumeetprajapati1996
Copy link

sumeetprajapati1996 commented Jun 6, 2022

Great reference for beginners. Thank you very much @bradtraversy

@ShandanaShahid
Copy link

ShandanaShahid commented Jun 24, 2022

thankyou.. this cheat sheet is for which version of mongo... can anyone tell please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment