MongoDB Database Cheatsheet

MongoDB is a popular NoSQL database that provides a flexible and scalable solution for managing large amounts of data. Whether you’re a beginner or an experienced developer, having a cheatsheet handy can save time and help you navigate MongoDB with ease. In this blog, we’ll cover essential MongoDB commands and concepts organized into a cheatsheet format.

Connecting to MongoDB

Connection String

mongo "mongodb+srv://<username>:<password><database>"

Connect to Local Instance


Basic Operations

Create Database

use mydatabase

Show Databases

show dbs

Create Collection


Show Collections

show collections

CRUD Operations

Insert Document

db.mycollection.insertOne({ key: "value" })

Find Documents


Update Document

db.mycollection.updateOne({ key: "value" }, { $set: { key: "newvalue" } })

Delete Document

db.mycollection.deleteOne({ key: "value" })


Basic Query

db.mycollection.find({ key: "value" })


db.mycollection.find({ key: "value" }, { key: 1, _id: 0 })


db.mycollection.find().sort({ key: 1 })


Create Index

db.mycollection.createIndex({ key: 1 })

List Indexes


Drop Index




  { $group: { _id: "$key", count: { $sum: 1 } } }


  { $project: { key: 1, _id: 0 } }

Backup and Restore

Export Data

mongodump --uri="mongodb+srv://<username>:<password><database>" --out /path/to/dump

Import Data

mongorestore --uri="mongodb+srv://<username>:<password><database>" /path/to/dump

This MongoDB cheatsheet provides a quick reference for essential commands and operations. Whether you’re inserting documents, querying data, or performing advanced aggregations, having these commands at your fingertips can significantly boost your productivity. Keep this cheatsheet handy for a seamless MongoDB experience.


1. What is MongoDB and why use it?

MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. It is designed for scalability, performance, and ease of development, making it an ideal choice for applications with evolving data requirements.

2. How do I connect to a MongoDB database?

To connect to MongoDB, you can use a connection string specifying the database server, username, password, and database name. Alternatively, for a local instance, the mongo command in the terminal can be used.

3. What is the difference between findOne and find in MongoDB?

The findOne method returns the first document that matches the query criteria, while the find method returns a cursor to the documents that match the query. To retrieve all matching documents, you can iterate over the cursor or use methods like toArray().

4. How can I create an index in MongoDB?

You can create an index in MongoDB using the createIndex method. Specify the field(s) on which to create an index and the desired order (ascending or descending). Indexing can significantly improve query performance.

5. What is the purpose of MongoDB’s Aggregation framework?

MongoDB’s Aggregation framework is used for data transformation and analysis. It provides powerful tools for grouping, filtering, and projecting data, making it useful for tasks like calculating aggregates, sorting, and shaping data before it is presented to an application.