Skip to content

Instantly share code, notes, and snippets.

@codeSTACKr
Created January 10, 2022 22:54
Show Gist options
  • Save codeSTACKr/53fd03c7f75d40d07797b8e4e47d78ec to your computer and use it in GitHub Desktop.
Save codeSTACKr/53fd03c7f75d40d07797b8e4e47d78ec to your computer and use it in GitHub Desktop.
MongoDB Cheat Sheet 2022

MongoDB Cheat Sheet 2022

MongoDB Crash Course 2022 < TODO: Add Video Link

Table of Contents

Check monosh Version

mongosh --version

Start the Mongo Shell

mongosh "YOUR_CONNECTION_STRING" --username YOUR_USER_NAME

Show Current Database

db

Show All Databases

show dbs

Create Or Switch Database

use blog

Drop Database

db.dropDatabase()

Create Collection

db.createCollection('posts')

Show Collections

show collections

Insert Document

db.posts.insertOne({
  title: 'Post 1',
  body: 'Body of post.',
  category: 'News',
  likes: 1,
  tags: ['news', 'events'],
  date: Date()
})

Insert Multiple Documents

db.posts.insertMany([
  {
    title: 'Post 2',
    body: 'Body of post.',
    category: 'Event',
    likes: 2,
    tags: ['news', 'events'],
    date: Date()
  },
  {
    title: 'Post 3',
    body: 'Body of post.',
    category: 'Tech',
    likes: 3,
    tags: ['news', 'events'],
    date: Date()
  },
  {
    title: 'Post 4',
    body: 'Body of post.',
    category: 'Event',
    likes: 4,
    tags: ['news', 'events'],
    date: Date()
  },
  {
    title: 'Post 5',
    body: 'Body of post.',
    category: 'News',
    likes: 5,
    tags: ['news', 'events'],
    date: Date()
  }
])

Find All Documents

db.posts.find()

Find Documents with Query

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

Sort Documents

Ascending

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

Descending

db.posts.find().sort({ title: -1 })

Count Documents

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

Limit Documents

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

Chaining

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

Find One Document

db.posts.findOne({ likes: { $gt: 3 } })

Update Document

db.posts.updateOne({ title: 'Post 1' },
{
  $set: {
    category: 'Tech'
  }
})

Update Document or Insert if not Found

db.posts.updateOne({ title: 'Post 6' },
{
  $set: {
    title: 'Post 6',
    body: 'Body of post.',
    category: 'News'
  }
},
{
  upsert: true
})

Increment Field ($inc)

db.posts.updateOne({ title: 'Post 1' },
{
  $inc: {
    likes: 2
  }
})

Update Multiple Documents

db.posts.updateMany({}, {
  $inc: {
    likes: 1
  }
})

Rename Field

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

Delete a Document

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

Delete Multiple Documents

db.posts.deleteMany({ category: 'Tech' })

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 } })
@Am0stafa
Copy link

Am0stafa commented Mar 8, 2022

cool

@Ashishpal438
Copy link

Ashishpal438 commented Mar 16, 2022

Thank u brad and Jessie, Love from India 💕

@fardinxd
Copy link

thanks

@SifisoDhlamini
Copy link

Thanks a lot. Much appreciated

@AliRazaSharafat
Copy link

Great work. Appreciable!!

@melxincognito
Copy link

UPDATE:
db.collection.find().count() is being depreciated and removed on the next major version

New method will be db.collection.countDocuments()

@zeelbhanderi
Copy link

thanks

@ndayishimiyeeric
Copy link

Thanks every helpfull

@Rob2097
Copy link

Rob2097 commented Jul 13, 2022

Thank you!

@aurrelkondi
Copy link

Thanks!

@yasiriqbal1990
Copy link

Awesome. Just took a few mins for me to learn MongoDB because of the way you have explained it.

@OlgaJones
Copy link

OlgaJones commented Sep 13, 2022

Thanks for the sheet. I am glad I found your post. I was searching for the MongoDB cheat sheet for so long and atlast I found it. When I was searching for it online, I also found https://letsgradeit.com/review/grademiners/ website where I can read reviews for an online essay writer easily.

@defected2020
Copy link

Much appreciated ❤️

Copy link

ghost commented Oct 14, 2022

Thanks for the awesome sheet.

@jesse-crypted
Copy link

Thanks for this.

@FdR-23
Copy link

FdR-23 commented Nov 9, 2022

Thanks a lot

@abrusu-collins
Copy link

This cheat sheet is sweet.
Love from Ghana

@Kingkobi01
Copy link

Kingkobi01 commented Jan 2, 2023

Nice one there.
Love from Ghana

@IanBriton
Copy link

Nice tut

@tenzinwoz
Copy link

😍

@devAbdulsalam
Copy link

Just started learning and stumble over this, it is a live saver 😁

@waqas-on-github
Copy link

me using first time mongodb felt amazing

@Amanjain4269
Copy link

Awesome !!!
Really helped me a lot.

@MuhammadRabi
Copy link

Great effort! Thank you!

@Pjose
Copy link

Pjose commented Dec 13, 2023

Awesome, good stuff. Thank you very much.

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