Let’s say we want to build a simple MongoDB project.

You can do everything right from the terminal:


1. Create a New Folder

First, make a directory for your program:

mkdir example

This command creates a new folder named example.

2. Navigate into the Folder

Move inside that folder:

cd example

Now you’re working inside your project directory.

3. Start the MongoDB Shell

Open the MongoDB interactive shell:

mongosh

(If you’re using MongoDB Atlas or a remote server, use your connection string instead.)

4. Create or Select a Database

Once you’re in the Mongo shell, create or switch to your database:

use appDB

This command tells MongoDB to use a database named appDB.

If it doesn’t already exist, MongoDB will create it automatically once you add your first document.

5. Start Writing Commands

Now you can use the commands from your cheat sheet. For example:

db.users.insertOne({ name: "Kia", age: 43 })
db.users.find()

6. Exit When Finished

When you’re done working:

exit

MongoDB Command Reference


1. Create / Insert Documents

Commands

db.collection.insertOne({})
db.collection.insertMany([{},{},...])

Description

  • Inserts one or multiple documents into a collection.

2. Read / Query Documents

Commands

db.collection.find()
db.collection.find({ field: value })
db.collection.findOne({ field: value })
db.collection.find().limit(n)
db.collection.find().skip(n)
db.collection.find().sort({ field: 1 })
db.collection.countDocuments()
db.collection.distinct("field")

Description

  • Retrieve documents, limit, skip, sort, count, and find distinct values.

3. Update Documents

Commands

db.collection.updateOne(filter, update)
db.collection.updateMany(filter, update)
db.collection.replaceOne(filter, replacement)

Example

db.users.updateOne(
  { name: "Reza" },
  { $set: { age: 46 } }
)

4. Delete Documents

Commands

db.collection.deleteOne(filter)
db.collection.deleteMany(filter)
db.collection.drop()

Description

  • Delete one, many, or drop the entire collection.

5. Aggregation

Command

db.collection.aggregate([
  { $match: { status: "A" } },
  { $group: { _id: "$item", total: { $sum: "$amount" } } }
])

Description

  • Runs pipeline stages to transform and analyze data.

6. Indexes

Commands

db.collection.createIndex({ field: 1 })
db.collection.dropIndex("indexName")
db.collection.getIndexes()

Description

  • Create, drop, or list indexes for faster queries.

7. Collection Management

Commands

db.createCollection("name")
db.getCollectionNames()
db.collection.renameCollection("newName")
db.collection.stats()

Description

  • Create, list, rename, or view stats for collections.

8. Database-Level Commands

Commands

show dbs
use myDatabase
db.dropDatabase()
db.getName()
db.stats()

Description

  • Manage databases, switch, drop, and view stats.

9. User & Role Management

Commands

db.createUser({ user: "", pwd: "", roles: [] })
db.updateUser("username", { pwd: "newpassword" })
db.dropUser("username")
db.getUsers()

Description

  • Manage database users and permissions.

10. Operators

Query Operators:

$eq, $ne, $gt, $lt, $gte, $lte, $in, $nin, $and, $or, $not, $regex

Update Operators:

$set, $unset, $inc, $mul, $rename, $push, $pull, $addToSet


✅ Closure

This list covers the most-used MongoDB commands developers rely on daily.

Start by practicing insert, find, and update operations on a test collection before moving to aggregation and indexes to handle large datasets efficiently.

You create a folder (mkdir example), navigate into it (cd example), open the MongoDB shell (mongosh), select or create your database (use appDB), and then start using your MongoDB commands.