MongoDB Cheatsheet

Manthan Ankolekar - Nov 18 '22 - - Dev Community

Connect & Check version :

mongo
mongosh
mongod
Enter fullscreen mode Exit fullscreen mode

Database Commands

View all databases

show dbs
Enter fullscreen mode Exit fullscreen mode

Create a new or switch databases

use dbName
Enter fullscreen mode Exit fullscreen mode

View current Database

db
Enter fullscreen mode Exit fullscreen mode

Delete Database

db.dropDatabase()
Enter fullscreen mode Exit fullscreen mode

Collection Commands

Show Collections

show collections
Enter fullscreen mode Exit fullscreen mode

Create a collection named 'comments’

db.createCollection('data')
Enter fullscreen mode Exit fullscreen mode

Drop a collection named 'comments’

db.comments.drop()
Enter fullscreen mode Exit fullscreen mode

Row(Document) Commands

Show all Rows in a Collection

db.comments.find()
Enter fullscreen mode Exit fullscreen mode

Show all Rows in a Collection (Prettified)

db.comments.find().pretty()
Enter fullscreen mode Exit fullscreen mode

Find the first row matching the object

db.comments.findOne({name: 'Manthan'})
Enter fullscreen mode Exit fullscreen mode

Insert One Row

db.comments.insert({
    'firstName': 'Manthan',
    'lastName': 'Ank'
 })
Enter fullscreen mode Exit fullscreen mode

Insert many Rows

db.comments.insertMany(
[
    {
        'firstName': 'Manthan',
      'lastName': 'Ank'
  },
  {
   'firstName': 'Gagan',
   'lastName': 'BA'
  }
])
Enter fullscreen mode Exit fullscreen mode

Search in a MongoDB Database

db.comments.find({lang:'JavaScript'})
Enter fullscreen mode Exit fullscreen mode

Limit the number of rows in output

db.comments.find().limit(3)
Enter fullscreen mode Exit fullscreen mode

Count the number of rows in the output

db.comments.find().count()
Enter fullscreen mode Exit fullscreen mode

Update a row

db.comments.updateOne(
    {name: 'Manthan'},
    {$set: {
            'name': 'Manthan',
        'lang': 'JavaScript',
        'mem_since': 1
        }
    }, 
    {upsert: true}
)
Enter fullscreen mode Exit fullscreen mode

MongoDB Increment Operator

db.comments.update(
    {name: 'Manthan'},
    {
        $inc:{
        mem_since: 2
        }
    }
)
Enter fullscreen mode Exit fullscreen mode

MongoDB Rename Operator

db.comments.update(
    {name: 'Manthan'},
    {
        $rename:{
        mem_since: 'member'
        }
    }
)
Enter fullscreen mode Exit fullscreen mode

Delete Row

db.comments.remove({name: 'Manthan'})
Enter fullscreen mode Exit fullscreen mode

Less than/Greater than/ Less than or Eq/Greater than or Eq

db.comments.find({member_since: {$lt: 90}})

Enter fullscreen mode Exit fullscreen mode
db.comments.find({member_since: {$lte: 90}})
Enter fullscreen mode Exit fullscreen mode
db.comments.find({member_since: {$gt: 90}})
Enter fullscreen mode Exit fullscreen mode
db.comments.find({member_since: {$gte: 90}})
Enter fullscreen mode Exit fullscreen mode

Reference Link

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .