Table of Contents:
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
| Term | Description |
|---|---|
| Database | A container for collections. |
| Collection | Similar to a table in SQL. |
| Document | JSON-like data structure. |
| Field | Key-value pair inside a document. |
| BSON | Binary JSON format used for data storage. |
Example Document:
{
"_id": 1,
"name": "John Doe",
"age": 30,
"skills": ["Python", "MongoDB", "Node.js"]
}
MongoDB Shell Basics
| Command | Description |
|---|---|
show dbs | List all databases |
use myDatabase | Switch or create a database |
show collections | List 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
| Operator | Description | Example |
|---|---|---|
$eq | Equals | { age: { $eq: 25 } } |
$ne | Not equal | { city: { $ne: "London" } } |
$gt / $lt | Greater/Less than | { age: { $gt: 20 } } |
$in / $nin | In/Not in list | { city: { $in: ["Delhi", "Paris"] } } |
$and | Logical AND | { $and: [{ age: { $gt: 20 } }, { city: "Paris" }] } |
$or | Logical OR | { $or: [{ city: "London" }, { city: "Berlin" }] } |
$exists | Check 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:
| Operator | Usage | Example |
|---|---|---|
$match | Filter documents | { $match: { city: "London" } } |
$group | Group by field | { $group: { _id: "$city", total: { $sum: 1 } } } |
$sort | Sort results | { $sort: { total: -1 } } |
$project | Select specific fields | { $project: { name: 1, city: 1 } } |
$limit | Limit 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)
| Type | Description | Example |
|---|---|---|
| Embedding | Store related data in the same document. | { name: "Alice", address: { city: "NY", zip: 10001 } } |
| Referencing | Store 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.

