πŸŽ‰ New: Top 75 PHP Interview Questions for 2026 β€” Free for all learners

MongoDB Cheatsheet β€” Complete MongoDB Commands, Queries & Aggregation Guide

P
php Guru
Β· November 24, 2025 Β· 3 min read Β· Updated November 24, 2025
mongodb cheatsheet, mongodb tutorial for beginners, mongodb commands list, mongodb query examples, mongodb aggregation pipeline, mongodb vs sql, mongodb update query, mongodb insert example, mongodb index, nosql database guide

πŸ“Œ Key Takeaways

  • MongoDB Cheatsheet β€” Complete MongoDB Commands, Queries & Aggregation Guide
  • MongoDB Cheatsheet
  • Basic MongoDB Concepts
  • MongoDB Shell Basics
  • CRUD Operations in MongoDB
  • MongoDB Query Operators
Advertisement

MongoDB Cheatsheet β€” Quick Reference for NoSQL Developers

MongoDB is a NoSQL document-based database designed for high scalability, flexibility, and performance. Instead of storing data in tables like SQL, MongoDB uses JSON-like documents known as BSON (Binary JSON).

This MongoDB Cheatsheet provides essential syntax, commands, and examples to help you query and manage databases effectively.


Basic MongoDB Concepts

TermDescription
DatabaseA container for collections.
CollectionSimilar to a table in SQL.
DocumentJSON-like data structure.
FieldKey-value pair inside a document.
BSONBinary JSON format used for data storage.

Example Document:

{
  "_id": 1,
  "name": "John Doe",
  "age": 30,
  "skills": ["Python", "MongoDB", "Node.js"]
}

MongoDB Shell Basics

CommandDescription
show dbsList all databases
use myDatabaseSwitch or create a database
show collectionsList collections in the current database
db.dropDatabase()Delete current database

CRUD Operations in MongoDB

CRUD = Create, Read, Update, Delete β€” the four basic operations in MongoDB.


Insert Documents

Insert a single document:

db.users.insertOne({ name: "Alice", age: 28, city: "New York" });

Insert multiple documents:

db.users.insertMany([
  { name: "Bob", age: 32 },
  { name: "Charlie", age: 25 }
]);

Read Documents

Retrieve all documents:

db.users.find();

Find documents with condition:

db.users.find({ age: { $gt: 25 } });

Pretty print results:

db.users.find().pretty();

Update Documents

Update one document:

db.users.updateOne(
  { name: "Alice" },
  { $set: { age: 29 } }
);

Update multiple documents:

db.users.updateMany(
  { city: "New York" },
  { $set: { city: "Los Angeles" } }
);

Delete Documents

Delete one:

db.users.deleteOne({ name: "Charlie" });

Delete many:

db.users.deleteMany({ age: { $lt: 25 } });

MongoDB Query Operators

OperatorDescriptionExample
$eqEquals{ age: { $eq: 25 } }
$neNot equal{ city: { $ne: "London" } }
$gt / $ltGreater/Less than{ age: { $gt: 20 } }
$in / $ninIn/Not in list{ city: { $in: ["Delhi", "Paris"] } }
$andLogical AND{ $and: [{ age: { $gt: 20 } }, { city: "Paris" }] }
$orLogical OR{ $or: [{ city: "London" }, { city: "Berlin" }] }
$existsCheck if field exists{ phone: { $exists: true } }
mongodb cheatsheet, mongodb tutorial for beginners, mongodb commands list, mongodb query examples, mongodb aggregation pipeline, mongodb vs sql, mongodb update query, mongodb insert example, mongodb index, nosql database guide

MongoDB Projection

Select specific fields to return in the query result.

db.users.find({}, { name: 1, city: 1, _id: 0 });

MongoDB Sorting and Limiting

db.users.find().sort({ age: 1 }); // Ascending
db.users.find().sort({ age: -1 }); // Descending
db.users.find().limit(5); // First 5 documents

Aggregation Pipeline

Aggregation allows data transformation and computation like SQL GROUP BY.

Basic example:

db.users.aggregate([
  { $match: { age: { $gte: 25 } } },
  { $group: { _id: "$city", total: { $sum: 1 } } }
]);

Common Aggregation Operators:

OperatorUsageExample
$matchFilter documents{ $match: { city: "London" } }
$groupGroup by field{ $group: { _id: "$city", total: { $sum: 1 } } }
$sortSort results{ $sort: { total: -1 } }
$projectSelect specific fields{ $project: { name: 1, city: 1 } }
$limitLimit number of results{ $limit: 10 }

MongoDB Indexing

Indexes speed up data retrieval operations.

Create an index:

db.users.createIndex({ name: 1 });

View indexes:

db.users.getIndexes();

Drop an index:

db.users.dropIndex("name_1");

MongoDB Relationships (Embedding vs Referencing)

TypeDescriptionExample
EmbeddingStore related data in the same document.{ name: "Alice", address: { city: "NY", zip: 10001 } }
ReferencingStore reference (ID) to another collection.{ user_id: ObjectId("..."), order_id: ObjectId("...") }

Tip: Use embedding for one-to-few and referencing for one-to-many relationships.


MongoDB Backup and Restore

Backup a database:

mongodump --db myDatabase --out /backup/

Restore a database:

mongorestore /backup/myDatabase

FAQ β€” MongoDB Cheatsheet

Q1: What is MongoDB used for?
MongoDB is used for storing unstructured or semi-structured data in JSON format for web apps, analytics, and IoT platforms.

Q2: Is MongoDB faster than SQL?
For large, unstructured data and high read/write operations, MongoDB can outperform traditional SQL databases.

Q3: How is data stored in MongoDB?
MongoDB stores data as BSON documents inside collections.

Q4: What language is MongoDB written in?
MongoDB is written in C++, JavaScript, and Go.

Q5: What is the default port for MongoDB?
The default port number is 27017.

P
php Guru
← Previous Post
SQL Cheatsheet β€” Quick Reference for Beginners and Professionals
Next Post β†’
PostgreSQL Cheatsheet β€” Quick Reference for SQL Developers

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove your humanity: 2   +   3   =