MongoDB cheatsheet

Frequently used MongoDB commands for developers

Subscribe to my newsletter and never miss my upcoming articles

MongoDB is an open-source document-oriented NoSQL database that is designed to store a large scale of data and also allows you to work with that data very efficiently. It stores data in the form of JSON documents. MongoDB provides a SQL-like query language to query records based on the internal structure of the document itself. Document stores provide high flexibility and are often used for working with occasionally changing data.

In this post, I will mention a few MongoDB commands which are used more frequently by the developers.


Database Operations

1. Show All Databases

show dbs

2. Show current Database


3. Create or switch to new Database

use hashnode

4. Delete Database



1. Show All Collections of Current Database

show collections

2. Create new collection


Create Documents

1. Insert One document

   {title: "blog post title", body: "blog post content"}


   {title: "blog post title", body: "blog post content"}

2. Insert Multiple document

db.posts.insert( [ 
    {title: "blog post 1 title", body: "blog post 1 content"},
    {title: "blog post 2 title", body: "blog post 2 content"},

Read Documents

1. Find One document


2. Find Multiple documents

/* returns a cursor - show 20 results - "it" to display more */

3. Find Multiple documents with formatted json

/* returns a cursor - show 20 results - "it" to display more */

4. Find documents by field value.

db.posts.find({'title' : 'blog 1 title'})

Update Documents

1. Update one

db.posts.updateOne({"_id": 1}, {$set: {"title": 'updated title'}})

2. Update Multiple

/* update only specific fields */ 
db.posts.update({"category": "technology"}, {$set: {"category": 'computer science'}})

3. Upsert complete Row

db.posts.update({ '_id' : 1 },
  title: 'Post one',
  body: 'New body for post 1',
  upsert: true

4. Increment Field Value

db.posts.update({ "_id": 1 },
  $inc: {
    views: 5

Delete Documents

1. Delete

db.posts.remove({ title: 'Post 1' })


Fetch results by sorting on field.

# ascending order
db.posts.find().sort({ title: 1 }).pretty()

# descending order
db.posts.find().sort({ title: -1 }).pretty()

Limit and Offset

Fetch results by pagination.

/* Skip 3 results*/

/* Fetch only 3 results*/

/* Sort by title , Skip first 10 results, fetch only next 3 documents*/
db.posts.find({}).sort({"title": 1}).skip(10).limit(3)

Add and Drop Index

1. Add Index

/* Create Index on single field */
db.posts.createIndex({"title": 1})  

/* Create compound Index */
db.posts.createIndex({"title": 1, "date": 1})

2. Drop Index

db.posts. dropIndex("title_1")

Range Queries

Find documents by range query

/* find posts where views are greater than 50 */
db.posts.find({'views' : { '$gt' : 50 }})

/* find posts where views are greater than or equal to 50 */
db.posts.find({'views' : { '$gte' : 50 }})

/* find posts where views are less than 50 */
db.posts.find({'views' : { '$lt' : 50 }})

/* find posts where views are less than or equal to 50 */
db.posts.find({'views' : { '$lte' : 50 }})

1. Create Text Index on field

db.posts.createIndex({content: "text"})

2. Search by Text

  $content: {
    $search: "post content"

Thank you for reading

Hope you find these resources useful. If you like what you read and want to see more about system design, microservices, and other technology-related stuff... You can follow me on

Buy Me A Coffee

Interested in reading more such articles from vishnu chilamakuru?

Support the author by donating an amount of your choice.

Recent sponsors

Comments (2)

Praveen Reddy Govind's photo

That's complete mongo DB course here :), Good job Vishnu.