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 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 Cheatsheet — Complete MongoDB Commands, Queries & Aggregation Guide

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.

Related Article
Machine Learning Cheatsheet (Unsupervised & Reinforcement Learning)

Machine Learning (ML) is a crucial part of artificial intelligence, enabling systems to automatically learn from data.This Machine Learning Cheatsheet Read more

HTML Cheat Sheet — Reference Guide to HTML Tags, Attributes, and Examples

HTML cheat sheet, HTML tags reference, HTML attributes list, HTML examples for beginners, semantic HTML guide, HTML forms tutorial, HTML Read more

Python Cheat Sheet — Complete Syntax Reference and Programming Examples

This Python Cheat Sheet is your quick reference guide for writing efficient Python code. Whether you’re preparing for coding interviews, Read more

PHP Cheat Sheet — Complete Syntax Reference and Programming Examples for Beginners

PHP Cheat Sheet — Complete Syntax Reference & Examples This PHP Cheat Sheet serves as a quick, structured reference for Read more

JavaScript Cheat Sheet — Complete ES6 Syntax, Functions, and DOM Methods with Examples

JavaScript Cheat Sheet — Complete Syntax Reference & Examples JavaScript is the core scripting language of the web, enabling interactivity, Read more

CSS Cheat Sheet — Complete CSS3 Selectors, Properties, and Layout Examples

CSS Cheat Sheet — Complete CSS3 Syntax, Selectors & Layout Examples Cascading Style Sheets (CSS) is the language used to Read more

Java Cheat Sheet — Complete Java Syntax, Data Types, Loops, and OOP Concepts for Beginners

Java Cheat Sheet — Complete Java Syntax, Classes, and Examples Java is a powerful, object-oriented programming language widely used for Read more

HTML5 Cheat Sheet — Complete Tag Reference & Examples

HTML5 is the core markup language of the modern web, used to structure content such as text, images, forms, and Read more

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments