DEV Community

yaswanthteja
yaswanthteja

Posted on

3 1

All MongoDb Database commands (MongoDb Cheatsheet)

In this post, we will see a comprehensive list of all the MongoDB commands you will ever need as a MongoDB beginner. This list covers almost all the most used commands in MongoDB.

Database Commands

  • View all databases
show dbs
Enter fullscreen mode Exit fullscreen mode
  • Create a new or switch databases
show dbs
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

These are the basic Database Commands of MongoDb.

I will assume that you are working inside a collection(tables in sql) named 'comments' on a MongoDB database of your choice

Collection Commands

  • Show Collections
show collections
Enter fullscreen mode Exit fullscreen mode
  • Create a collection named 'comments'
db.createCollection('comments')
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: 'yash'})
Enter fullscreen mode Exit fullscreen mode
  • Insert One Row
db.comments.insert({
    'name': 'yash',
    'lang': 'JavaScript',
    'meme': 1
 })
Enter fullscreen mode Exit fullscreen mode
  • Insert many Rows
db.comments.insertMany([{
    'name': 'yash',
    'lang': 'JavaScript',
    'meme': 1
    }, 
    {'name': 'Rohan',
    'lang': 'Python',
    'meme': 3
    },
    {'name': 'jagu',
    'lang': 'Java',
    'meme': 4
}])
Enter fullscreen mode Exit fullscreen mode
  • Search in a MongoDb Database
db.comments.find({lang:'Python'})
Enter fullscreen mode Exit fullscreen mode
  • Limit the number of rows in output
db.comments.find().limit(2)
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.update({name: 'Teja'},
{'name': 'yash',
    'lang': 'JavaScript',
    'meme': 1
}, {upsert: true})
Enter fullscreen mode Exit fullscreen mode
  • Mongodb Increment Operator
db.comments.update({name: 'Rohan'},
{$inc:{
    meme: 2
}})
Enter fullscreen mode Exit fullscreen mode
  • Mongodb Rename Operator
db.comments.update({name: 'Rohan'},
{$rename:{
    meme: 'member'
}})
Enter fullscreen mode Exit fullscreen mode
  • Delete Row
db.comments.remove({name: 'yash'})
Enter fullscreen mode Exit fullscreen mode
  • Less than
db.comments.find({member_since: {$lt: 90}})
Enter fullscreen mode Exit fullscreen mode
  • Less than equals to
db.comments.find({member_since: {$lte: 90}})
Enter fullscreen mode Exit fullscreen mode
  • Greater than
db.comments.find({member_since: {$gt: 90}})
Enter fullscreen mode Exit fullscreen mode
  • Greater than equals to
db.comments.find({member_since: {$gte: 90}})
Enter fullscreen mode Exit fullscreen mode

These are all basic MongoDb commands

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay