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

@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

@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?

@OmarSawalmeh
Copy link

OmarSawalmeh commented Jul 3, 2022

Thanks

@luizfernandorabelo
Copy link

luizfernandorabelo commented Jul 10, 2022

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

This cheat sheet is related to MongoDB 4.0.10 version

@Eugene-Mokrushin
Copy link

Eugene-Mokrushin commented Jul 25, 2022

Thanks!

@famartinez
Copy link

famartinez commented Aug 18, 2022

Awesome, thank you!

@datmt
Copy link

datmt commented Aug 23, 2022

Thanks! I also created some cheat sheets here covering crud/aggregation too https://datmt.com/series/mongodb-cheat-sheets/

@Richard-vinu
Copy link

Richard-vinu commented Aug 23, 2022

thank you!

@rohit-patel-azilen
Copy link

rohit-patel-azilen commented Sep 5, 2022

Thank you very much!..A great reference for beginners.

@RiyaadHossain
Copy link

RiyaadHossain commented Sep 10, 2022

Thank a lot

@1002jpvc
Copy link

1002jpvc commented Sep 25, 2022

Thank You BRAD

@ShandanaShahid
Copy link

ShandanaShahid commented Oct 14, 2022

Thankyou

@Ogden777
Copy link

Ogden777 commented Nov 7, 2022

Сенкью

@phieraditya
Copy link

phieraditya commented Nov 16, 2022

Thank you @bradtraversy

I use mongosh with MongoDB 6.0.1, and some methods in the cheat sheet are deprecated. Here is what works with my version:

Get All Rows Formatted

db.posts.find()

already formatted without pretty()

Count Rows

db.posts.countDocuments()
db.posts.countDocuments({ category: ‘News' });

count() is deprecated

Update Specific Field

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

update() is deprecated

Delete Row

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

remove() is deprecated

@fughesi
Copy link

fughesi commented Nov 16, 2022

Epstein didn't kill himself

@Raj-Dave-1
Copy link

Raj-Dave-1 commented Nov 21, 2022

Thank You!

@pxzxj
Copy link

pxzxj commented Dec 1, 2022

Thank You!

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