0% found this document useful (0 votes)
7 views75 pages

Mongodb Theory

MongoDB is an open-source NoSQL database that utilizes a flexible, schema-less data model and is optimized for high performance, scalability, and availability. It supports various applications including content management systems, e-commerce, and real-time analytics, and offers features like document-oriented storage, indexing, and aggregation. Additionally, tools like MongoDB Compass and MongoDB Atlas facilitate database management and cloud hosting, while Mongoose provides a library for easier interaction with MongoDB in Node.js applications.

Uploaded by

wub0fuhubis3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views75 pages

Mongodb Theory

MongoDB is an open-source NoSQL database that utilizes a flexible, schema-less data model and is optimized for high performance, scalability, and availability. It supports various applications including content management systems, e-commerce, and real-time analytics, and offers features like document-oriented storage, indexing, and aggregation. Additionally, tools like MongoDB Compass and MongoDB Atlas facilitate database management and cloud hosting, while Mongoose provides a library for easier interaction with MongoDB in Node.js applications.

Uploaded by

wub0fuhubis3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 75

MongoDB 🍃

 MongoDB Basics
o What is MongoDB ?
1. MongoDB is an open-source NoSQL database.
2. Document-based and cross-platform compatible.
3. Offers high performance and high availability.
4. Supports easy scalability for large data handling.
5. Differs from relational databases by using a flexible, schema-less data
model.
6. Built on BSON (Binary JSON), allowing for efficient storage and
querying of non-structured data.
o Key Features of MongoDB ?
1. Document-oriented: MongoDB stores data in JSON-like documents
(BSON format). The flexible data model adapts to real-world object
representations.
2. Scalability: Offers automatic scaling. Can scale horizontally by
sharding (partitioning data across servers) and vertically by adding
storage.
3. Indexing: Supports indexing on any document attribute to enhance
query performance.
4. Replication: Provides high availability through replica sets. Primary
and secondary nodes maintain data copies.
5. Aggregation: Features a powerful aggregation framework. Enables
complex data operations like transformations, filtering, and sorting.
6. Ad hoc queries: Supports searching by field, range, and regular
expressions.
o When we Use MongoDB ?
1. Content Management Systems (CMS): MongoDB’s dynamic schema
efficiently supports various content types, making it ideal for CMS
needs.
2. E-Commerce Platforms: With its scalability and flexibility,
MongoDB handles complex product catalogs, customer data, and
orders seamlessly.
3. Social Networks: Designed to store large volumes of posts, user
profiles, and interactions, MongoDB is well-suited for social media
platforms.
4. Real-Time Analytics: Equipped with aggregation and indexing,
MongoDB enables the analysis and processing of live data for
actionable insights.
5. IoT Applications: MongoDB’s flexible data model captures and stores
data from countless devices and sensors, a key requirement in IoT
applications.
6. Gaming: MongoDB manages player profiles, game progress, and other
real-time data needed for a responsive gaming experience.
7. Location-Based Services: With robust geospatial capabilities,
MongoDB efficiently handles map data, locations, and geolocation
queries.
8. Big Data: MongoDB’s flexible model and horizontal scalability make
it ideal for managing vast amounts of unstructured and semi-structured
data.
9. Mobile Applications: The flexible data model in MongoDB adapts
well to the evolving data types and dynamic nature of mobile apps.

Conclusion: MongoDB is a powerful and versatile NoSQL database,


optimized for handling unstructured data across a variety of applications, from
CMS and e-commerce to IoT, gaming, and real-time analytics.

o MongoDB Terminology ?
1. Database: Holds collections and structures needed to manage data.
2. Collection: A group of related documents, like a table in traditional
databases.
3. Document: A single record in a collection, made up of fields.
Documents don’t need a fixed structure.
4. Field: A piece of data in a document, similar to a column in a table.
5. Index: A structure that speeds up searches, making data retrieval
faster.
6. Query: A way to get specific data from a collection based on
conditions.
7. Cursor: A pointer to the query result set, enabling efficient processing
of individual documents.
8. Aggregation: Summarizes and transforms data in collections for
complex analysis or report generation.
9. Replica Set: A group of MongoDB instances with the same data,
providing redundancy, high availability, and automatic failover.
10. Sharding: Distributes data across multiple machines to scale
horizontally, breaking the dataset into manageable chunks called
shards.
o MongoDB Compass vs MongoDB Atlas


1. MongoDB Compass

MongoDB Compass is a graphical tool (like an app or software) that


helps you view and manage your MongoDB database. Instead of
writing complex code or using the command line, you can use
Compass to:

 Explore your data visually (see how your data is stored).


 Query your database (search for specific data).
 Create and update data (add or change records).
 Analyze the structure of your data and see if there are any
issues.

It’s like a dashboard or control panel for MongoDB that makes


working with data much easier.


2. MongoDB Atlas

MongoDB Atlas is a cloud service that hosts and manages MongoDB


databases for you. Instead of setting up and maintaining MongoDB on
your own servers, Atlas allows you to:

 Create a MongoDB database in the cloud without worrying


about setup or maintenance.
 Scale your database easily if your app grows (add more
storage or servers).
 Backup your data automatically.
 Ensure security with features like encryption and access
control.

In short, Atlas is like renting a database from MongoDB in the cloud.


You don’t need to worry about the infrastructure—it’s all handled for
you.

 MongoDB Compass: A tool for managing and viewing MongoDB


data on your local computer.
 MongoDB Atlas: A cloud service for hosting and managing your
MongoDB database online
o What is Mongoose?

Mongoose is a library that makes working with MongoDB easier in a Node.js


application. It helps you define the structure of your data (using schemas), and
it provides a set of tools for interacting with the database. Key Features:

 Schema-based Validation

With Mongoose, you create a schema to define the rules for your data.
For example, you can specify:

 What data types each field should be (e.g., String, Number).


 Which fields are required.
 Default values for certain fields.

const userSchema = new mongoose.Schema({


name: { type: String, required: true }, // Name is
required and should be a string.
email: { type: String, required: true, unique: true },
// Email is required and must be unique.
age: { type: Number, min: 18 }, // Age must be at
least 18.
});

 Model-based Queries

Once you define the schema, you create a model. This model helps
you interact with the data in your MongoDB collection (e.g., adding,
finding, updating, or deleting data).
 Adding Data: .save()
 Finding Data: .find(), .findOne()
 Updating Data: .updateOne()
 Deleting Data: .deleteOne()

const User = mongoose.model('User', userSchema); //


Create the model based on the schema.

// Add a new user


const newUser = new User({ name: 'Alice', email:
'[email protected]', age: 25 });
newUser.save(); // Save the new user to the database.

 Middleware (Hooks)

Mongoose lets you run custom logic before or after certain actions.
For example:

 Hashing a password before saving.


 Logging actions after an update.

userSchema.pre('save', function(next) {
console.log('Saving user:', this.name);
next(); // Proceed to save the document.
});

 Population

Mongoose lets you link data from different collections. For example,
you can link a User with their Posts. This is called population.

User.findOne({ name:
'Alice' }).populate('posts').then(user =>
console.log(user));

 Query Building

Mongoose provides an easy way to build complex queries. You can


chain methods like .find(), .sort(), .limit(), etc., without
needing to write raw MongoDB queries.

User.find({ age: { $gte: 18 } }).sort({ age: -


1 }).limit(10).then(users => console.log(users));

 Aggregation

Mongoose supports MongoDB's aggregation framework, which lets


you perform more advanced operations, like grouping, sorting, or
transforming data
User.aggregate([
{ $match: { age: { $gte: 18 } } },
{ $group: { _id: null, averageAge: { $avg: '$age' } } }
]).then(result => console.log(result));

o Mongo export

mongo export is a command-line tool in MongoDB used to export data from


a MongoDB database into a JSON or CSV file. It allows you to extract data
from MongoDB collections and save it to a file for backup, sharing, or other
purposes.

mongoexport --db=school --collection=students --


out=students.json

o mongodump
 mongodump is a MongoDB utility for creating database backups.
 Exports data into BSON (Binary JSON) format.
 Can back up entire databases or specific collections.
 Default output directory is ./dump (can be changed with -out).
 Use mongorestore to restore the backup.
 Basic Usage:
 mongodump --uri <MongoDB connection string>
 Common Use Cases:

0. Backup All Databases:


1. mongodump --uri "mongodb://localhost:27017"
 Creates a backup in the default ./dump directory.
2. Backup a Specific Database:
3. mongodump --db mydatabase --out /path/to/backup
 Dumps only the mydatabase to the specified directory.
4. Backup a Specific Collection:
5. mongodump --db mydatabase --collection students --
out /path/to/backup
6. Change the Output Directory:
7. mongodump --uri "mongodb://localhost:27017" --
out /path/to/custom/backup
 Restore the Backup:

To restore the data:

mongorestore /path/to/backup

o SQL vs NoSQL
 SQL

 Definition: SQL databases, also known as relational databases,


store data in tables with a predefined schema.
 Schema: Uses a structured schema, with data organized in rows
and columns.
 ACID Compliance: Ensures reliable transactions through
ACID properties (Atomicity, Consistency, Isolation,
Durability).
 Examples: MySQL, PostgreSQL, and Microsoft SQL Server.
 Advantages SQL

0. Predefined schema: Ideal for applications with a fixed


structure.
1. ACID transactions: Ensures data consistency and reliability.
2. Support for complex queries: Rich SQL queries can handle
complex data relationships and aggregation operations.
3. Scalability: Vertical scaling by adding more resources to the
server (e.g., RAM, CPU).
 Limitations SQL
0. Rigid schema: Schema changes can be time-consuming and
may lead to downtime.
1. Scaling challenges: Horizontal scaling and sharding are
complex.
2. Not suited for hierarchical data: Requires multiple tables and
JOINs to model tree-like structures.
 NoSQL

 Definition: NoSQL databases are non-relational and do not


follow a fixed schema, using formats like JSON documents,
key-value pairs, or graphs.
 Schema: Flexible and semi-structured.
 BASE Model: Many NoSQL databases follow the BASE
(Basically Available, Soft state, Eventual consistency) model,
favoring availability over strict consistency.
 Examples: MongoDB, Cassandra, Redis, and Couchbase
 Advantages NoSQL

0. Flexible schema: Easily adapts to changes without disrupting


the application.
1. Scalability: Horizontal scaling by partitioning data across
multiple servers (sharding).
2. Fast: Designed for faster read and writes, often with a simpler
query language.
3. Handling large volumes of data: Better suited to managing
big data and real-time applications.
4. Support for various data structures: Different NoSQL
databases cater to various needs, like document, graph, or key-
value stores.
 Limitations NoSQL

 Limited query capabilities: Some NoSQL databases lack


complex query and aggregation support or use specific query
languages.
 Weaker consistency: Many NoSQL databases follow the
BASE (Basically Available, Soft state, Eventual consistency)
properties that provide weaker consistency guarantees than
ACID-compliant databases.
o BSON vs JSON
 BSON

BSON: A binary-encoded format (not human-readable) for JSON-like


documents, BSON is faster for machine processing and is optimized
for efficient storage, traversal, and encoding/decoding.

 Binary Encoding: BSON’s binary format improves


performance and supports data types not available in JSON.
 Additional Data Types: Supports types like Date, Binary,
ObjectId, and Decimal128, allowing for more accurate data
representation.
 Efficient Traversability: In BSON, each piece of data has its
size marked, enabling MongoDB to easily skip over parts it
doesn’t need. This helps MongoDB find information faster, like
flipping pages to locate specific content instead of reading
every line.
 Size: BSON is more compact and optimized for storage
efficiency.
 Usage: BSON serves as the internal storage format for
MongoDB.
 Schema: BSON allows for a flexible schema without strict
requirements.
 JSON

JSON: A text-based format (human-readable) for representing


structured data, JSON is widely used for data interchange between
systems and is easy for both humans and machines to read and write.

 Text-Based Format: JSON is a text format that is easily


readable by humans, making it straightforward to understand
and edit.
 Data Types: Supports a limited set of data types, including
strings, numbers, arrays, objects, and booleans, but does not
handle more complex types like dates or binary data.
 Size: JSON can be larger due to its text representation, which
may lead to increased storage requirements compared to binary
formats.
 Efficiency: JSON is slower for machine processing compared
to binary formats like BSON, especially when handling large
datasets.
 Usage: JSON is commonly used for data interchange between
web applications and servers, especially in APIs.
 Schema: JSON is schema-less, allowing for flexible data
structures, but this can lead to inconsistencies if not managed
properly.
 Table
Feature BSON JSON
Binary-encoded format Text-based format (human-
Format
(not human-readable) readable)
Faster for machine
processing, optimized for Slower for machine
Performance
efficient storage and processing
traversal
Supports additional types Limited types (strings,
Data Types (e.g., Date, Binary, numbers, arrays, objects,
ObjectId, Decimal128) booleans)
More compact and
Larger due to text
Size optimized for storage
representation
efficiency
Commonly used for data
Internal storage format for
Usage interchange between web
MongoDB
applications and servers
Allows for a flexible
Schema-less, allowing for
Schema schema without strict
flexible data structures
requirements
Each data element has its Requires linear access,
Traversability size marked for efficient which can be slower for
traversal large datasets

o BSON Additional Data Types

BSON (Binary JSON) is the format MongoDB uses to store data. In addition
to standard JSON types, BSON supports:

Date
0.
 Stores dates as milliseconds since the Unix epoch.
1. Binary Data
 Stores binary content like images, videos, or files.
2. ObjectId
 Unique 12-byte identifier automatically generated for each
document.
3. Decimal128
 High-precision decimal type for monetary or scientific
calculations.
4. Code
 Stores JavaScript code within documents.
5. Regular Expression
 Stores regex patterns directly in documents.
6. MinKey/MaxKey
 Special types to compare values in a query.
 Embedded Documents and Arrays

Embedded Documents Arrays in MongoDB allow you to store nested documents


within a single document, making it easy to represent one-to-many relationships.
o Example:
o {
o "_id": 1,
o "name": "John Doe",
o "addresses": [
o {
o "street": "123 Main St",
o "city": "New York",
o "zipcode": "10001"
o },
o {
o "street": "456 Broadway",
o "city": "Los Angeles",
o "zipcode": "90001"
o }
o ]
o }
o Advantages:

 Performance: Faster read/write since related data is stored together.


 Consistency: Keeps related data in sync without needing joins.
 Flexibility: Easily nest arrays for complex data structures.
o When to Use:
 For one-to-many relationships.
 When the embedded data is not too large.
 When data is closely related to the parent document.
o Querying Embedded Documents Arrays:

You can easily query embedded arrays using MongoDB’s array query
operators, like $elemMatch, **$all**, and **$size**. You can also use dot
notation to search and update embedded documents.

 Example Query:

To find users by street address:

Embedded Documents Arrays allow you to efficiently store and


manage complex data in MongoDB while simplifying queries.

db.users.find({ 'addresses.street': '123 Main St' });

 Embedded vs Reference Document


o Embedded Documents
 Data is nested directly inside a document.
 Example:
 {
 "userId": 1,
 "name": "John",
 "address": {
 "street": "123 Main St",
 "city": "Springfield"
 }
 }
 Advantages:
 Faster reads (everything is in one document).
 Simpler queries.
 Disadvantages:
 Document size limits (16MB).
 Difficult to update embedded data separately.
o Reference Document
 Data is stored in separate collections and linked using IDs.
 Example:
 {
 "userId": 1,
 "name": "John",
 "addressId": 101
 }

And the addresses collection:

{
"addressId": 101,
"street": "123 Main St",
"city": "Springfield"
}

 Advantages:
 No size limits.
 Better for frequently updated data.
 Disadvantages:
 Slower reads (requires additional queries).
 More complex queries.
 Data Model and Data Types

In MongoDB, a data model defines the structure of data within a database.


MongoDB uses a flexible, schema-less data model that stores data in BSON (Binary
JSON) documents within collections.

In MongoDB, data is stored in BSON format, which supports various data types.
Knowing these types is important for effective schema design and query performance.

Key Data Types:

o Regular Expression: Stores regular expressions.


o { "pattern": /^mongodb/i }
o ObjectId: A unique 12-byte identifier for documents, used as the default
value for the _id field.
o { "_id": ObjectId("507f191e810c19729de860ea") }
o String: Used for text data; must be a valid UTF-8 string.
o { "name": "John Doe" }
o Boolean: Stores true or false values.
o { "isActive": true }
o Integer: Stores whole numbers; can be 32-bit (int) or 64-bit (long).
o { "age": 28 }
o Double: Used for floating-point numbers.
o { "price": 12.99 }
o Date: Stores date and time in Unix time format.
o { "createdAt": ISODate("2019-02-18T19:29:22.381Z") }
o Array: Stores a list of values, which can be of different types.
o { "tags": ["mongodb", "database", "noSQL"] }
o Object: Allows you to embed documents within documents.
o { "address": { "street": "123 Main St", "city": "San
Francisco", "state": "CA" } }
o **Null**: Represents the absence of a value.
o { "middleName": null }
o Binary Data: Stores binary data or byte arrays.
o { "data": BinData(0, "c3VyZS4=") }
o Code: Stores JavaScript code.
o { "script": Code("function() { return 'Hello, World!'; }") }
 Collections and Methods
o Counting Documents

MongoDB provides methods to count documents in a collection

 Use countDocuments when you need an exact count, especially if you


have filters.
 Use estimatedDocumentCount when you need a quick, approximate
count of the entire collection without filters.
 Avoid count if possible, as it is a legacy method and may not be as
reliable for large or sharded datasets.

0. countDocuments()

 Purpose: Provides an accurate count of documents based on a


specified filter.
 Usage: Use this method when you need an exact count with or
without a filter.
 Performance: It may take longer for large collections, as it
reads through the documents that match the filter.
 Syntax:
 db.collectionName.countDocuments({ <query> });
 Example:This will count all documents in the orders
collection with a status of 'completed'.
 db.orders.countDocuments({ status: 'completed' });

2. estimatedDocumentCount()

 Purpose: Provides an approximate count of all documents in


a collection.
 Usage: Use this method when you need a quick, approximate
count of the entire collection without any filters.
 Performance: Faster than countDocuments because it uses
metadata rather than scanning documents.
 Syntax:
 db.collectionName.estimatedDocumentCount();
 Example:This gives an approximate count of all documents in
the orders collection.
 db.orders.estimatedDocumentCount();

3. count()

 Purpose: An older method that can apply filters, similar to


countDocuments.
 Usage: While still available, count is considered less reliable
for large datasets. It’s more of a legacy option and should
generally be avoided in favor of countDocuments.
 Performance: May yield slightly inaccurate results on sharded
clusters or with very large datasets.
 Syntax:
 db.collectionName.count({ <query> });
 Example:
 db.orders.count({ status: 'completed' });
o validate()

The validate command checks if a MongoDB collection is correct and


provides details about its structure and indexes.

 How to Use It

To validate a collection, run:

db.runCommand({ validate: "<collection_name>" });

 Example: To validate a collection called products:


 db.runCommand({ validate: 'products' });
 Options

 full: Checks everything in detail (can slow down performance).

Example:

db.runCommand({ validate: 'products', full:


true });

 background: Runs the check in the background so other


operations can continue.

Output

It gives you information like:

Whether the collection is valid.


Any errors or warnings.
The number of indexes.
 When to Use It
Use validate if you think there's a problem with your data or if you
want to check the collection's structure.

 Performance Note

Using full can slow down your database, so use it carefully on large
collections.

o What is a Cursor?
 A cursor is like a pointer that allows you to go through the results of a
query one document at a time.
 In MongoDB, a cursor acts as a pointer to the documents in a
collection, and it allows you to retrieve documents one at a time.
 When you ask MongoDB for documents, it gives you a cursor to help
you retrieve them.
 Creating a Cursor: When you run a query (like finding documents),
MongoDB automatically creates a cursor.
 const cursor = db.collection('myCollection').find();
 Iterating Through Documents: You can loop through the results using
the forEach method:
 cursor.forEach((doc) => {
 console.log(doc); // This prints each document to the
console
 });
 Cursor Methods

 count(): Returns the total number of documents in the result


set.
 limit(n): Limits the number of documents retrieved to n.
 skip(n): Skips the first n documents in the result set.
 sort(field, order): Sorts the documents based on the
specified field and order (1 for ascending, -1 for descending).
 project(field): Specifies the fields to include or exclude
from the result documents.

 const cursor = db
 .collection('myCollection')
 .find({ age: { $gt: 25 } })
 .sort('name', 1)
 .limit(10)
 .skip(20)
 .project({ name: 1, _id: 0 });

 Closing Cursor

 Cursors close automatically when you finish retrieving all


documents or after 10 minutes of inactivity.
 If you want to close it manually, you can use:
 cursor.close();
o important comments

0. Start MongoDB Shell:


1. mongosh
2. Show Databases:
3. show dbs
4. Use a Database:
5. use databaseName
6. Show Collections (equivalent to tables):
7. show collections
8. Create a Collection:
9. db.createCollection("collectionName");
10. Drop a Collection:
11. db.collectionName.drop()
o insert()

In MongoDB, you use collections to store documents. To add data, you can
use two main methods: insertOne() and insertMany().

 1. insertOne() for adding a single document.

 Purpose: Inserts a single document into a collection.

 db.inventory.insertOne({ name: 'Ai', age: 14 });

 2. insertMany() for adding multiple documents at once

 Purpose: Inserts multiple documents into a collection at once.

 db.collection.insertMany([
 { name: 'Ai', age: 20 },
 { name: 'Ash', age: 21 },
 { name: 'Messi', age: 35 }
 ]);

o find()

The find() method is used to search for documents within a collection. You
can filter, sort, and limit the results based on your needs.

 1. Basic Find() Method

 Purpose: Fetch all documents in a collection.

 db.users.find(); // Fetches all users

 2. Query Filters

 Purpose: Search for specific documents using filters.

 db.users.find({ age: 25 }); // Finds users with age 25

 3. Logical Operators

Use logical operators like $and, $or, and $not for complex queries.
db.users.find({ $and: [{ age: 25 }, { first_name:
'John' }] }); // Finds users who are 25 and named John

 4. Projection

 Purpose: Control which fields are returned in the results.

 db.users.find({ age: 25 }, { first_name: 1, age: 1 }); //


Returns only first_name and age of matching users

 5. Sorting

Sort results using the sort() function.

db.users.find().sort({ age: 1 }); // Sorts users by age


in ascending order

 6. Limit and Skip

 Limit: Fetch a specific number of documents.

 db.users.find().limit(5); // Gets the first 5 users

 Skip: Skip a specific number of documents.


 db.users.find().skip(10); // Skips the first 10
users
o update()

MongoDB provides several methods to modify existing documents in a


collection. Here are the most commonly used update methods:

 1. updateOne() Updates the first document that matches a filter.


 db.users.updateOne({ name: 'John Doe' }, { $set: { age:
30 } });
 2. updateMany() Updates all documents that match a filter.
 db.users.updateMany({ status: 'new' }, { $inc: { views: 1
} });
 3. replaceOne() Replaces a document that matches a filter with a new
document.
 db.users.replaceOne({ name: 'John Doe' }, { name: 'John
Doe', age: 30 });
 Common Update Operators:

 **$set**: Update a specific field.


 db.users.updateOne({ name: 'Alice' }, { $set: { age:
25 } });
 $inc: Increment a field’s value.
 db.users.updateOne({ name: 'Bob' }, { $inc: { score:
10 } });
 $push: Add a value to an array.
 db.users.updateOne({ name: 'Carol' }, { $push:
{ hobbies: 'reading' } });
o delete()
 deleteOne(): Deletes the first document that matches a filter.
 db.users.deleteOne({ firstName: 'John' });
 deleteMany(): Deletes all documents that match a filter.
 db.users.deleteMany({ country: 'Australia' });
 drop(): Deletes the entire collection, including all documents and
metadata.
 db.collection.drop();
o bulkWrite()

The bulkWrite() method allows you to perform multiple operations (like


insert, update, or delete) at once, making your application faster.

 Ordered Bulk Write: Executes operations in order. If one fails, it


stops there.
 db.users.bulkWrite(
 [
 { insertOne: { document: { _id: 1, name:
'Alice' } } }, // Insert
 { updateOne: { filter: { _id: 2 }, update: { $set:
{ name: 'Bob' } } } }, // Update
 { deleteOne: { filter: { _id: 3 } } } // Delete
 ],
 { ordered: true } // Executes in order; stops if one
fails
 );
 Unordered Bulk Write: Executes operations in any order. If one fails,
it continues with the others.
 db.users.bulkWrite(
 [
 { insertOne: { document: { _id: 4, name:
'Charlie' } } }, // Insert
 { updateOne: { filter: { _id: 5 }, update: { $set:
{ name: 'Dave' } } } }, // Update
 { deleteOne: { filter: { _id: 6 } } } // Delete
 ],
 { ordered: false } // Executes in any order; continues
if one fails
 );
 Mongo Utility Commands
o Database Commands
 show dbs: Lists all databases.
 use <db_name>: Switches to a specific database.
 db.dropDatabase(): Drops the current database.
o Collection Commands
 show collections: Lists all collections in a database.
 db.createCollection("name"): Creates a new collection.
 db.<collection>.drop(): Drops a collection.
o Data Commands
 db.<collection>.insertOne({...}): Inserts a document.
 db.<collection>.find({...}): Queries a collection.
 db.<collection>.updateOne({...}, {...}): Updates a document.
o Index Commands
 db.<collection>.createIndex({ field: 1 }): Creates an index.
 db.<collection>.getIndexes(): Lists all indexes on a collection.
o Backup and Restore
 mongodump: Exports data to a backup.
 mongorestore: Restores data from a backup.
 Components of _id

The _id field is a unique identifier for each MongoDB document. Its default value is
an ObjectId, which has the following structure:

1. Timestamp (First 4 bytes)


 Represents the creation time in seconds since the Unix epoch.
2. Machine Identifier (Next 3 bytes)
 Ensures uniqueness across machines.
3. Process Identifier (Next 2 bytes)
 Ensures uniqueness across processes running on the same machine.
4. Counter (Last 3 bytes)
 A random value that ensures uniqueness within a single second.

Example of an ObjectId:

64d7f9e1d8347a12cfc1b032

 OPERATORS

o Comparison Operators
 $eq (Equal)

Checks if two values are equal; returns true if they are, otherwise
false.

 Query Example: Find products with a price of 100:


 db.products.find({ price: { $eq: 100 } });
 Aggregation Example: Add a “discounted” field if the price is
50:
 db.products.aggregate([
 { $addFields: { discounted: { $eq: ['$price',
50] } } },
 ]);
 $ne (Not Equal)

The $ne operator filters documents where a specified field's value is


not equal to a given value.

 Example: Find products where the category is not "Fruits":


 db.products.find({ category: { $ne: 'Fruits' } });
 $gt (Greater Than)

Filters documents where a field’s value is greater than a specified


value.

 Example: Find students older than 21:


 db.students.find({ age: { $gt: 21 } });
 $lt (Less Than)

Filters documents where a specified field’s value is less than a given


value.

 Example: Find products priced less than 500:


 db.products.find({ price: { $lt: 500 } });
 $lte (Less Than Equal)

Matches values that are less than or equal to a specified value.

 Example: Find products priced at 15 or less:


 db.products.find({ price: { $lte: 15 } });
 $gte (Greater Than Equal)

The Greater Than or Equal To Operator ($gte) checks if a value is


greater than or equal to another value.

 Example: Find products with a price greater than or equal to


20:
 db.products.find({ price: { $gte: 20 } });
 $nin

The $nin operator in MongoDB is used to exclude documents where a


field's value matches any value in a specified array. It returns
documents where:

0. The field’s value is not in the array.


1. The field does not exist in the document.

 Syntax
 { field: { $nin: [<value1>, <value2>, ..., <valueN>]
} }
 Example

Consider a books collection:

[
{ _id: 1, title: 'The Silent Patient', genre:
'Mystery' },
{ _id: 2, title: 'Dune', genre: 'Sci-Fi' },
{ _id: 3, title: 'Educated', genre:
'Biography' },
{ _id: 4, title: 'The Alchemist', genre:
'Adventure' },
{ _id: 5, title: 'The Great Gatsby' }
]

To find books that are not in the genres "Mystery", "Sci-Fi",


or "Thriller":

db.books.find({ genre: { $nin: ['Mystery', 'Sci-


Fi', 'Thriller'] } });
 Result:
 [
 { _id: 3, title: 'Educated', genre:
'Biography' },
 { _id: 4, title: 'The Alchemist', genre:
'Adventure' },
 { _id: 5, title: 'The Great Gatsby' }
 ]

Summary

The $nin operator helps you find documents where a field’s


value is not in a specific list, providing a way to exclude
unwanted matches.

 $in

The $in operator in MongoDB is used to find documents where a field


matches any value in a specified array.

 Syntax
 { field: { $in: [<value1>, <value2>, ...] } }
 Example

Consider an articles collection:

[
{ _id: 1, title: 'MongoDB', tags: ['database',
'NoSQL'] },
{ _id: 2, title: 'Node.js', tags: ['javascript',
'runtime'] },
{ _id: 3, title: 'React', tags: ['NoSQL',
'javascript'] },
{ _id: 4, title: 'React', tags: ['SQL',
'React'] },
{ _id: 5, title: 'React', tags: ['React',
'MySQL'] },
]

To find all articles with either the "NoSQL" or "javascript"


tag:

db.articles.find({ tags: { $in: ['NoSQL',


'javascript'] } });

 Result:
 [
 { _id: 1, title: 'MongoDB', tags:
['database', 'NoSQL'] },
 { _id: 2, title: 'Node.js', tags:
['javascript', 'runtime'] },
 { _id: 3, title: 'React', tags: ['NoSQL',
'javascript'] }
 ]
Summary

The $in operator lets you check if a field’s value exists within
a specified list of values, which is useful for filtering multiple
possible matches.

o Logical Operators
 $and

The $and operator combines multiple query conditions and returns


documents that meet all specified conditions.

 Syntax:
 { $and: [{ condition1 }, { condition2 }, ... ] }
 Example: Find orders with a price greater than 1 and quantity
less than 10:
 db.orders.find({ $and: [{ price: { $gt: 1 } },
{ quantity: { $lt: 10 } }] });
 Results:
 { "_id": 3, "item": "orange", "price": 2,
"quantity": 5 }

Note

You can also achieve the same result without using $and:

db.orders.find({ price: { $gt: 1 }, quantity:


{ $lt: 10 } });

 $or

The $or operator allows you to query multiple conditions and returns
documents that satisfy any of the specified conditions.

 Syntax:
 {
 $or: [
 { condition1 },
 { condition2 },
 // ...,
 { conditionN }
 ]
 }
 Example: Find products where the category is "Fruits" or the
price is less than or equal to 15:
 db.products.find({
 $or: [
 { category: 'Fruits' },
 { price: { $lte: 15 } }
 ]
 });
 Results:
 [
 { _id: 1, category: 'Fruits', price: 20 },
 { _id: 2, category: 'Fruits', price: 30 },
 { _id: 3, category: 'Vegetables', price: 10
},
 { _id: 4, category: 'Vegetables', price: 15
}
 ]
 To find documents where the category is "Fruits" and
the price is either less than 20 or greater than 25:
 db.products.find({
 $and: [
 { category: 'Fruits' },
 {
 $or: [
 { price: { $lt: 20 } },
 { price: { $gt: 25 } }
 ]
 }
 ]
 });
 Results:
 [{ _id: 2, category: 'Fruits', price: 30 }]
 $not

The $not operator negates a logical expression or condition in a query,


allowing you to find documents that do not match a given condition.

 Syntax:
 {
 field: { $not: { <operator-expression> } }
 }
 Examples

1. Simple Usage: Find all products with a price that is not


greater than 100:
2. db.products.find({ price: { $not: { $gt:
100 } } });
 This returns products with a price of 100 or less.
3. Combining with Other Operators: Find electronics
products whose price is not less than 50 and not greater
than 200:
4. db.products.find({
5. $and: [
6. { category: 'Electronics' },
7. { price: { $not: { $lt: 50 } } },
8. { price: { $not: { $gt: 200 } } }
9. ]
10. });
 This returns products with a price between 50
and 200.
11. Using Regular Expressions: Find all products whose
name does not start with "apple" (case-insensitive):
12. db.products.find({ name: { $not: /^apple/i }
});
 $nor
The $nor operator in MongoDB is used to find documents that do not
match any conditions listed.

 Syntax
 { $nor: [ { <condition1> },
{ <condition2> }, ... ] }
 Example

Given a students collection:

[
{ _id: 1, name: 'Alice', age: 30, subjects:
['math', 'science'] },
{ _id: 2, name: 'Bob', age: 25, subjects:
['history'] },
{ _id: 3, name: 'Cathy', age: 35, subjects:
['math', 'history'] },
{ _id: 4, name: 'David', age: 28, subjects:
['science'] },
]

Query

Find students who are not older than 30 and not studying math:

db.students.find({
$nor: [{ age: { $gt: 30 } }, { subjects:
'math' }],
});

 Result:
 [
 { _id: 2, name: 'Bob', age: 25, subjects:
['history'] },
 { _id: 4, name: 'David', age: 28, subjects:
['science'] }
 ]
o Array Operators
 $all

The $all operator in MongoDB is used to match documents where an


array field contains all specified elements. This allows you to filter for
documents based on multiple required values within a single array
field.

 Syntax
 {
 <field>: {
 $all: [<value1>, <value2>, ..., <valueN>]
 }
 }
 Example

Consider a movies collection:


[
{ _id: 1, title: "The Matrix", tags: ["action",
"sci-fi", "cyberpunk"] },
{ _id: 2, title: "Inception", tags: ["sci-fi",
"thriller", "action"] },
{ _id: 3, title: "Toy Story", tags: ["animation",
"family", "action"] },
{ _id: 4, title: "Evile", tags: ["horror",
"mystery"] }
]

To find all movies with both the "action" and "sci-fi" tags:

db.movies.find({ tags: { $all: ['action', 'sci-fi']


} });

 Result:
 [
 { _id: 1, title: "The Matrix", tags:
["action", "sci-fi", "cyberpunk"] },
 { _id: 2, title: "Inception", tags:
["action", "thriller", "sci-fi"] }
 ]

Summary

The $all operator checks if all specified values are present in


an array field, making it useful for queries that require multiple
elements in the same field.

 $elemMatch

The $elemMatch operator matches documents that contain an array


field with at least one element that matches all the specified query
criteria.

The $elemMatch operator checks which object in the array meets the
specified conditions in the query. dealing with arrays of objects

 Syntax
 {
 <field>: {
 $elemMatch: { <condition1>, <condition2>, ...,
<conditionN> }
 }
 }
 Example

Suppose we have a collection named courseRecords with the


following documents:

[
{ _id: 1, student: "Mary", grades: [{ subject:
"Math", score: 80 }, { subject: "English", score:
75 }] },
{ _id: 2, student: "Tom", grades: [{ subject:
"Math", score: 90 }, { subject: "English", score:
80 }] },
{ _id: 3, student: "John", grades: [{ subject:
"Math", score: 85 }, { subject: "English", score:
65 }] },
{ _id: 4, student: "Lucy", grades: [{ subject:
"Math", score: 70 }, { subject: "English", score:
85 }] }
]

Query

Let’s say we want to find all students who have a score of 80 or


above in Math.

db.courseRecords.find({
grades: {
$elemMatch: {
subject: "Math",
score: { $gte: 80 }
}
}
});

Explanation of Query

 grades: We are querying the grades array field.


 $elemMatch: Ensures that at least one element within
the grades array satisfies both conditions:
 subject is "Math".
 score is >= 80.
 Result

The query returns documents where at least one element


in the grades array meets both conditions. Based on the
data in courseRecords, the result will be:

[
{ _id: 1, student: "Mary", grades:
[{ subject: "Math", score: 80 }, { subject:
"English", score: 75 }] },
{ _id: 2, student: "Tom", grades:
[{ subject: "Math", score: 90 }, { subject:
"English", score: 80 }] },
{ _id: 3, student: "John", grades:
[{ subject: "Math", score: 85 }, { subject:
"English", score: 65 }] }
]
Result Explanation

1. Mary: Has Math with a score of 80 (matches


both conditions).
2. Tom: Has Math with a score of 90 (matches
both conditions).
3. John: Has Math with a score of 85 (matches
both conditions).
4. Lucy is not included because her Math score is
70, which does not satisfy score >= 80.
 $size

The $size operator is used to query documents based on the exact


number of elements in an array field. This operator is helpful when you
want to filter documents that contain an array with a specific length.

 Syntax
 { "<array_field>": { "$size": <number_of_elements> }
}
 <array_field>: The name of the array field you want
to check.
 <number_of_elements>: The exact number of
elements you want to match.
 Example

Assuming you have a collection named products with


documents like this:

[
{ "_id": 1, "name": "Shirt", "colors": ["red",
"blue", "green"] },
{ "_id": 2, "name": "Pants", "colors": ["black",
"white", "grey", "blue", "red"] },
{ "_id": 3, "name": "Hat", "colors": ["yellow"] }
]

To find all products that have exactly 5 colors, you would use
the following query:

db.products.find({ colors: { $size: 5 } });

 Result

This query will return:

[
{ "_id": 2, "name": "Pants", "colors":
["black", "white", "grey", "blue", "red"] }
]

Important Notes
 The $size operator matches documents that
have an exact number of elements in the
specified array field.
 For more flexible array length comparisons
(e.g., greater than or less than), you might need
to use $expr along with $size in the
aggregation framework.
 The $size operator does not require an
additional index for efficiency; it can utilize
existing indexes on the array field.

This summary provides a clear understanding of the


$size operator in MongoDB. If you have any further
questions or need additional examples, feel free to ask!

o Element Operators
 $exists

The $exists operator is used to filter documents based on the


presence or absence of a specified field. This operator is useful when
you want to check whether a field exists, regardless of its value.

 Syntax
 { "field": { "$exists": <boolean> } }
 Example
 [
 { "_id": 1, "title": "MongoDB Basics", "author":
"John Doe" },
 { "_id": 2, "title": "Advanced MongoDB",
"published": 2022 },
 { "_id": 3, "title": "JavaScript", "author": "Jan
Smit", "edition": null }
 ]

Queries Using $exists

1. Find documents where the author field exists:


2. db.books.find({ author: { $exists: true } });
 Result:
 [
 { "_id": 1, "title": "MongoDB
Basics", "author": "John Doe" },
 { "_id": 3, "title": "JavaScript",
"author": "Jan Smit", "edition": null }
 ]
3. Find documents where the published field does not
exist:
4. db.books.find({ published: { $exists: false }
});
 Result:
 [
 { "_id": 1, "title": "MongoDB
Basics", "author": "John Doe" }
 ]
Summary

 Use $exists: true to find documents that contain a


specific field.
 Use $exists: false to find documents that do not
have a specific field.
 $type

The $type operator is used to filter documents based on the data type
of a specified field. It allows you to query for documents that have
fields of a certain BSON data type.

 Syntax
 {
 fieldName: {
 $type: dataType
 }
 }
 fieldName: The name of the field you want to check.
 dataType: The BSON data type or its corresponding
alias.
 Common BSON Data Types and Aliases

Type Alias
Double 1 or "double"
String 2 or "string"
Object 3 or "object"
Array 4 or "array"
Binary 5 or "binData"
ObjectId 7 or "objectId"
Boolean 8 or "bool"
Date 9 or "date"
Null 10 or "null"
Int32 16 or "int"
Int64 18 or "long"
Decimal128 19 or "decimal"

 Example

Assume you have a products collection with the following


documents:

[
{ "_id": 1, "name": "Laptop", "price": 999.99,
"inStock": true },
{ "_id": 2, "name": "Smartphone", "price":
599.99, "inStock": true },
{ "_id": 3, "name": "Tablet", "price": "300",
"inStock": "yes" }, // price is a string
{ "_id": 4, "name": "Headphones", "price": null,
"inStock": false },
{ "_id": 5, "name": "Monitor", "price": 250.00,
"inStock": true }
]

1. Find Products with Price as Double

If you want to find all products where the price is a


Double, you can use the following query:

db.products.find({ price: { $type: "double" }


});

 Result:

This query will return documents with _id 1, 2,


and 5, since their price values are of type
Double.

[
{ "_id": 1, "name": "Laptop",
"price": 999.99, "inStock": true },
{ "_id": 2, "name": "Smartphone",
"price": 599.99, "inStock": true },
{ "_id": 5, "name": "Monitor",
"price": 250.00, "inStock": true }
]

2. Find Products with Price as String

If you want to find products where the price is stored


as a String, you can do:

db.products.find({ price: { $type: "string" }


});

 Result:

This query will return the document with _id 3,


since its price value is a String.

[
{ "_id": 3, "name": "Tablet",
"price": "300", "inStock": "yes" }
]

3. Find Products with Price as Null


To find products where the price is explicitly set to
null, you can use:

db.products.find({ price: { $type:


"null" } });

 Result:

This query will return the document with _id 4,


since its price value is null.

[
{ "_id": 4, "name": "Headphones",
"price": null, "inStock": false }
]

Summary

 The $type operator allows you to filter documents


based on the specific BSON data type of a field.
 It's useful for ensuring that you are only working with
documents that have the expected data types.
 The examples above illustrate how to find documents
based on the type of the price field, whether it's a
Double, String, or Null.
o Evaluation Operator
 $expr

The $expr operator allows the use of aggregation expressions within


the query. It enables comparisons and logical operations on fields
within the same document.

 Syntax:
 db.collection.find({
 $expr: { $gt: ["$field1", "$field2"] }
 });
 Example Find documents where field1 is greater than field2
 [
 { "_id": 1, "field1": 10, "field2": 5 },
 { "_id": 2, "field1": 3, "field2": 8 },
 { "_id": 3, "field1": 15, "field2": 15 }
 ]

Query:

db.collection.find({
$expr: { $gt: ["$field1", "$field2"] }
});

 Result:
 [
 { "_id": 1, "field1": 10, "field2": 5 }
 ]
Conclusion

The $expr operator is useful for comparing fields


within the same document, allowing complex queries
that leverage aggregation expressions.

 $regex

The $regex operator in MongoDB is used to search for documents that


contain a specific pattern in a text field using Regular Expressions
(regex). It allows for flexible string matching based on defined
patterns.

 Syntax

0. /al/:

 Matches: Any name containing the sequence


"al" anywhere in the name.
 Example matches: "Pal", "Sally", "Jamal",
"Alice", "Kal".
1. /^al/:

 Matches: Names that start with "al".


 Example matches: "Alice", "Albert", "Alina",
"Alfred".
 Does not match: "Pal", "Sally", "Kal".
2. /al$/:

 Matches: Names that end with "al".


 Example matches: "Pal", "Jamal", "Kal".
 Does not match: "Alice", "Albert", "Sally".
3. /^al$/:

 Matches: Names that are exactly "al".


 Example matches: Only the exact name "al".
 Does not match: "Alice", "Jamal", "Kal", etc.

 // Matches these letter conining name anyware


 db.collection.find({ fieldName: { $regex: /al/ }})
 // Matches the first letters
 db.collection.find({ fieldName: { $regex: /^Al/ }})
 // Matches the last letters
 db.collection.find({ fieldName: { $regex: /al$/ }})
 // Matching Exact Name
 db.collection.find({ fieldName: { $regex:
/^Al$/ } });

 fieldName: The field you want to search.


 your-pattern: The regex pattern to match.
 Examples
 [
 { "_id": 1, "name": "Alice Johnson" },
 { "_id": 2, "name": "Bob Smith" },
 { "_id": 3, "name": "Charlie Brown" },
 { "_id": 4, "name": "David Wilson" }
 ]

 Basic Example:
 db.users.find({ name: { $regex: 'Alice' } });

 Result
 [
 { "_id": 1, "name": "Alice Johnson" }
 ]

This finds documents where the name field contains


"Alice".

 Case-Insensitive Search:
 db.users.find({ name: { $regex: 'alice',
$options: 'i' } });

 Result
 [
 { "_id": 1, "name": "Alice Johnson" }
 ]

This finds documents where the name contains "alice"


regardless of case.

 Special Characters: To search for a special character


like +, escape it:
 db.users.find({ name: { $regex: '\\\\+' } });

 Result
 []

(Assuming none of the names contain the +


character in this case.)

 Summary
 The $regex operator allows you to search for patterns in text
fields, making it a useful tool for flexible querying in
MongoDB.

 $jsonSchema

The $jsonSchema operator is used to enforce schema validation on


documents in a collection. You can specify the required structure of
documents, such as data types and required fields.

 Syntax:
 db.collection.find({
 $jsonSchema: {
 bsonType: "object",
 required: ["name", "age"],
 properties: {
 name: { bsonType: "string" },
 age: { bsonType: "int", minimum: 18 }
 }
 }
 });
 Example
 [
 { "_id": 1, "name": "Alice", "age": 30 },
 { "_id": 2, "name": "Bob", "age": 17 },
 { "_id": 3, "name": "Charlie", "age": 22 }
 ]

Query

0. Find documents that conform to the schema:


1. db.collection.find({
2. $jsonSchema: {
3. bsonType: "object",
4. required: ["name", "age"],
5. properties: {
6. name: { bsonType: "string" },
7. age: { bsonType: "int", minimum: 18 }
8. }
9. }
10. });

 Result:
 [
 { "_id": 1, "name": "Alice", "age": 30 },
 { "_id": 3, "name": "Charlie", "age": 22 }
 ]

Conclusion

The $jsonSchema operator ensures that documents adhere to a


defined structure, helping maintain data integrity within a
collection.

 $mod

The $mod operator checks if a field’s value divided by a divisor has a


specific remainder. It’s helpful for finding values that meet specific
modulo conditions.

 Syntax:
 db.collection.find({
 field: { $mod: [divisor, remainder] }
 });
 Example
 [
 { "_id": 1, "number": 10 },
 { "_id": 2, "number": 15 },
 { "_id": 3, "number": 20 }
 ]

Query

0. Find documents where number is divisible by 5:


1. db.collection.find({
2. number: { $mod: [5, 0] }
3. });

 Result:
 [
 { "_id": 1, "number": 10 },
 { "_id": 3, "number": 20 }
 ]

Conclusion

The $mod operator is useful for identifying documents based on


remainder conditions, enabling queries based on divisibility.

 $text

The $text operator is used to perform text searches on a collection


with a text index. It allows you to find documents based on keywords
or phrases.

 Syntax:
 db.collection.find({
 $text: { $search: "some text" }
 });
 Example
 [
 { "_id": 1, "content": "Learn MongoDB basics" },
 { "_id": 2, "content": "Advanced MongoDB
techniques" },
 { "_id": 3, "content": "JavaScript programming" }
 ]

Query

0. Find documents containing the phrase "MongoDB":


1. db.collection.find({
2. $text: { $search: "MongoDB" }
3. });

 Result:
 [
 { "_id": 1, "content": "Learn MongoDB
basics" },
 { "_id": 2, "content": "Advanced MongoDB
techniques" }
 ]

Conclusion

The $text operator facilitates efficient keyword searches


within documents, enhancing the ability to find relevant
information quickly.

 $where

The $where operator allows the use of JavaScript expressions to query


documents. It can be more flexible but is generally slower and should
be used with caution.

 Syntax:
 db.collection.find({
 $where: "this.field > value"
 });
 Example
 [
 { "_id": 1, "value": 10 },
 { "_id": 2, "value": 20 },
 { "_id": 3, "value": 30 }
 ]

Query

0. Find documents where value is greater than 15:


1. db.collection.find({
2. $where: "this.value > 15"
3. });

 Result:
 [
 { "_id": 2, "value": 20 },
 { "_id": 3, "value": 30 }
 ]

Conclusion

The $where operator offers great flexibility for querying, but it


should be used sparingly due to potential performance issues.

o Update Operators
 $set: Sets the value of a field in a document.
 Example Document:
 { "_id": 1, "name": "Alice", "age": 25 }
 Query:
 db.users.updateOne(
 { _id: 1 },
 { $set: { age: 30 }}
 );
 Result:
 { "_id": 1, "name": "Alice", "age": 30 }
 $unset: Removes a field from a document.
 Example Document:
 { "_id": 1, "name": "Alice", "email":
"[email protected]" }
 Query:
 db.users.updateOne(
 { _id: 1 },
 { $unset: { email: "" }}
 );
 Result:
 { "_id": 1, "name": "Alice" }
 $inc: Increments the value of a field by a specified amount.
 Example Document:
 { "_id": 1, "name": "Alice", "age": 30 }
 Query:
 db.users.updateOne(
 { _id: 1 },
 { $inc: { age: 1 }}
 );
 Result:
 { "_id": 1, "name": "Alice", "age": 31 }
 **$push**: Adds an element to an array field.
 Example Document:
 { "_id": 1, "name": "Alice", "hobbies":
["Reading"] }
 Query:
 db.users.updateOne(
 { _id: 1 },
 { $push: { hobbies: "Cooking" }}
 );
 Result:
 { "_id": 1, "name": "Alice", "hobbies": ["Reading",
"Cooking"] }
 $addToSet: Adds a value to an array only if it doesn't already exist in
the array.
 Example Document:
 {
 "_id": 1,
 "name": "Alice",
 "hobbies": ["Reading", "Cycling", "Swimming"]
 }
 Query:
 Now, if we try to add "Swimming" again using $addToSet,
we would run:
 db.users.updateOne(
 { _id: 1 }, // Find the
document with _id equal to 1
 { $addToSet: { hobbies: "Swimming" }} // Try to
add "Swimming" to the hobbies array
 );
 Result:
 {
 "_id": 1,
 "name": "Alice",
 "hobbies": ["Reading", "Cycling", "Swimming"]
 }
 $pop: Removes the first or last element of an array.
 Example Document:
 { "_id": 1, "name": "Alice", "hobbies": ["Reading",
"Cooking"] }
 Query:
 db.users.updateOne(
 { _id: 1 },
 { $pop: { hobbies: 1 }}
 );
 Result:
 { "_id": 1, "name": "Alice", "hobbies":
["Reading"] }
 $pull: Removes all instances of a value from an array.
 Example Document:
 { "_id": 1, "name": "Alice", "hobbies": ["Reading",
"Cooking"] }
 Query:
 db.users.updateOne(
 { _id: 1 },
 { $pull: { hobbies: "Reading" }}
 );
 Result:
 { "_id": 1, "name": "Alice", "hobbies":
["Cooking"] }
 $rename: Renames a field.
 Example Document:
 { "_id": 1, "name": "Alice", "age": 25 }
 Query:
 db.users.updateOne(
 { _id: 1 },
 { $rename: { name: "fullName" }}
 );
 Result:
 { "_id": 1, "fullName": "Alice", "age": 25 }
o Projection Operators
 $project

The $project operator in MongoDB is used in the aggregation


framework to specify which fields to include or exclude in the output
documents. It reshapes the data returned from the database.

 Syntax
 { $project: { field1: <value>, field2:
<value>, ... } }
 field1, field2: Names of the fields to include (1) or
exclude (0).
 Example

Assuming a collection named users with documents like:

{
"_id": 1,
"name": "John Doe",
"age": 30,
"email": "[email protected]"
}
Query:

To include only the name and exclude the email, use:

db.users.aggregate([
{
$project: {
name: 1,
email: 0 // Exclude email
}
}
]);

 Result
 {
 "_id": 1,
 "name": "John Doe"
 }

This query returns documents that only include the name


field and exclude the email field.

 $include

The $include projection operator in MongoDB is used to specify


which fields to return in the result documents. By including only the
fields of interest, you can make your queries more efficient by
reducing the amount of data retrieved.

 Syntax
 { field: 1 }
 field: The name of the field to include.
 1: Indicates that the field should be included in the
results.

You can include multiple fields like this:

{ field1: 1, field2: 1, field3: 1 }

 Example

Assume you have a collection called books with the following


documents:

[
{
"title": "The Catcher in the Rye",
"author": "J.D. Salinger",
"year": 1951,
"genre": "Literary fiction"
},
{
"title": "To Kill a Mockingbird",
"author": "Harper Lee",
"year": 1960,
"genre": "Southern Gothic"
},
{
"title": "Of Mice and Men",
"author": "John Steinbeck",
"year": 1937,
"genre": "Novella"
}
]

Query:

To retrieve only the title and author fields, excluding the


_id field, use:

db.books.find({}, { title: 1, author: 1, _id: 0 });

 Result
 [
 {
 "title": "The Catcher in the Rye",
 "author": "J.D. Salinger"
 },
 {
 "title": "To Kill a Mockingbird",
 "author": "Harper Lee"
 },
 {
 "title": "Of Mice and Men",
 "author": "John Steinbeck"
 }
 ]

Note

 You cannot combine $include (1) and $exclude (0)


for the same field, except for the _id field, which can
be excluded while including other fields.
 $exclude

The $exclude operator in MongoDB is used to exclude specific fields


from the result of a query. To exclude a field, you set its value to 0 in
the projection document.

 Syntax
 {
 $project: {
 field1: 0,
 field2: 0,
 ...
 }
 }
 Example
Suppose we have a collection called students with the
following documents:

[
{ "_id": 1, "name": "John Doe", "age": 20,
"course": "Software Engineering" },
{ "_id": 2, "name": "Jane Smith", "age": 22,
"course": "Computer Science" },
{ "_id": 3, "name": "Richard Roe", "age": 21,
"course": "Information Technology" }
]

If you want to fetch all the students but exclude the age field
from the result, you can use the following command:

db.students.aggregate([
{
$project: {
age: 0 // Exclude the age field
}
}
]);

 Result

The result will be:

[
{ "_id": 1, "name": "John Doe", "course":
"Software Engineering" },
{ "_id": 2, "name": "Jane Smith", "course":
"Computer Science" },
{ "_id": 3, "name": "Richard Roe",
"course": "Information Technology" }
]

In this output, the age field is successfully excluded


from the results.

Important Note

You cannot exclude a field (set to 0) that is also included (set to


1) in the same projection document, except for the _id field,
which can be both included and excluded in the same
document.

 $slice

The $slice operator in MongoDB limits the number of items returned


from an array in a document. It helps fetch only the needed data,
improving query speed and reducing memory use.

 Syntax
0. Limit number of elements:
1. { field: { $slice: <number> } }
2. Limit from a specific position:
3. { field: { $slice: [<skip>, <limit>] } }
 Examples

0. Limit Number of Elements

Document:

{
"_id": 1,
"title": "My First Post",
"tags": ["mongodb", "database", "nosql",
"backend"]
}

Query:

db.posts.find({}, { tags: { $slice: 3 } });

 Result:
 {
 "_id": 1,
 "tags": ["mongodb", "database",
"nosql"]
 }

2. Limit from a Specific Position

Document:

{
"_id": 2,
"title": "My Second Post",
"tags": ["mongodb", "database", "nosql",
"backend", "development", "coding"]
}

Query:

db.posts.find({}, { tags: { $slice: [4,


3] } });

 Result:
 {
 "_id": 2,
 "tags": ["development", "coding"]
 }

Conclusion
The $slice operator is a simple way to limit the number of
array items returned in MongoDB queries.

o Bulk Write Operation

A bulk write operation allows you to perform multiple write operations (insert,
update, replace, or delete) in a single command for better performance and
reduced network overhead.

 Syntax:
 db.collection.bulkWrite([
 { insertOne: { document: { /* document fields
*/ } } },
 { updateOne: { filter: { /* filter criteria */ },
update: { /* update fields */ } } },
 { deleteOne: { filter: { /* filter criteria */ } } },
 // Additional operations...
 ])
 Example:

Collection: products

[
{ "_id": 1, "name": "Laptop", "price": 1200 },
{ "_id": 2, "name": "Phone", "price": 800 },
{ "_id": 3, "name": "Tablet", "price": 400 }
]

 Operation: Perform multiple operations: insert a new product,


update the price of an existing product, and delete another
product.
 Query:
 db.products.bulkWrite([
 { insertOne: { document: { _id: 4, name:
"Monitor", price: 300 } } },
 { updateOne: { filter: { _id: 2 }, update:
{ $set: { price: 750 } } } },
 { deleteOne: { filter: { _id: 3 } } }
 ])
 Result:
 A new product "Monitor" with a price of 300 is
inserted.
 The product "Phone" price is updated to 750.
 The product "Tablet" is deleted from the collection.
 [
 { "_id": 1, "name": "Laptop", "price": 1200 },
 { "_id": 2, "name": "Phone", "price":
750 }, // Price updated
 { "_id": 4, "name": "Monitor", "price":
300 } // New product added
 // "Tablet" has been removed
 ]

Summary:
The bulk write operation efficiently performs multiple write operations
in a single call, improving performance and reducing the number of
network requests.

o Accumulator Operators

Accumulators are operators in MongoDB that perform calculations on groups


of data during an aggregation operation.

 $sum: Adds up the numeric values for documents in a group.


 Syntax: { $sum: "<field>" }
 Example Collection: transactions
 [
 { "_id": 1, "item": "Book", "amount": 15,
"category": "Stationery" },
 { "_id": 2, "item": "Pen", "amount": 2,
"category": "Stationery" },
 { "_id": 3, "item": "Notebook", "amount": 5,
"category": "Stationery" },
 { "_id": 4, "item": "Apple", "amount": 1,
"category": "Fruit" }
 ]

Query:

db.transactions.aggregate([
{ $group: { _id: "$category", totalAmount:
{ $sum: "$amount" } } }
])

 Result:
 [
 { "_id": "Stationery", "totalAmount":
22 },
 { "_id": "Fruit", "totalAmount": 1 }
 ]
 $avg: Calculates the average of numeric values in a group.
 Syntax: { $avg: "<field>" }
 Example Collection: sales
 [
 { "_id": 1, "product": "Laptop", "price": 1200,
"category": "Electronics" },
 { "_id": 2, "product": "Phone", "price": 800,
"category": "Electronics" },
 { "_id": 3, "product": "Tablet", "price": 600,
"category": "Electronics" }
 ]

Query:

db.sales.aggregate([
{ $group: { _id: "$category", averagePrice:
{ $avg: "$price" } } }
])
 Result:
 [
 { "_id": "Electronics", "averagePrice":
866.67 }
 ]
 $min: Finds the minimum value in a group.
 Syntax: { $min: "<field>" }
 Example Collection: grades
 [
 { "_id": 1, "student": "Alice", "score": 85,
"subject": "Math" },
 { "_id": 2, "student": "Bob", "score": 75,
"subject": "Math" },
 { "_id": 3, "student": "Charlie", "score": 90,
"subject": "Math" }
 ]

Query:

db.grades.aggregate([
{ $group: { _id: "$subject", minScore: { $min:
"$score" } } }
])

 Result:
 [
 { "_id": "Math", "minScore": 75 }
 ]
 $max: Finds the maximum value in a group.
 Syntax: { $max: "<field>" }
 Example Collection: products
 [
 { "_id": 1, "name": "TV", "price": 500 },
 { "_id": 2, "name": "Laptop", "price": 1200 },
 { "_id": 3, "name": "Phone", "price": 800 }
 ]

Query:

db.products.aggregate([
{ $group: { _id: null, maxPrice: { $max:
"$price" } } }
])

 Result:
 [
 { "_id": null, "maxPrice": 1200 }
 ]
 $first:Returns the first value in a group based on the order of
documents.
 Syntax: { $first: "<field>" }
 Example Collection: orders
 [
 { "_id": 1, "customer": "Alice", "amount": 200,
"date": "2024-01-01" },
 { "_id": 2, "customer": "Bob", "amount": 150,
"date": "2024-02-01" },
 { "_id": 3, "customer": "Charlie", "amount":
300, "date": "2024-03-01" }
 ]

Query:

db.orders.aggregate([
{ $group: { _id: null, firstOrder: { $first:
"$customer" } } }
])

 Result:
 [
 { "_id": null, "firstOrder": "Alice" }
 ]
 $last:Returns the last value in a group based on the order of
documents.
 Syntax: { $last: "<field>" }
 Example Collection: employees
 [
 { "_id": 1, "name": "Alice", "department": "HR",
"joined": "2023-01-01" },
 { "_id": 2, "name": "Bob", "department": "HR",
"joined": "2023-02-01" },
 { "_id": 3, "name": "Charlie", "department":
"HR", "joined": "2023-03-01" }
 ]

Query:

db.employees.aggregate([
{ $group: { _id: "$department", lastJoined:
{ $last: "$name" } } }
])

 Result:
 [
 { "_id": "HR", "lastJoined": "Charlie" }
 ]
 $push: Creates an array with all values from a specified field in a
group.
 Syntax: { $push: "<field>" }
 Example Collection: inventory
 [
 { "_id": 1, "product": "Apple", "category":
"Fruit" },
 { "_id": 2, "product": "Orange", "category":
"Fruit" },
 { "_id": 3, "product": "Banana", "category":
"Fruit" }
 ]

Query:
db.inventory.aggregate([
{ $group: { _id: "$category", allProducts:
{ $push: "$product" } } }
])

 Result:
 [
 { "_id": "Fruit", "allProducts":
["Apple", "Orange", "Banana"] }
 ]
 $addToSet: Creates an array of unique values from a specified field in
a group.
 Syntax: { $addToSet: "<field>" }
 Example Collection: purchases
 [
 { "_id": 1, "product": "Apple", "category":
"Fruit" },
 { "_id": 2, "product": "Apple", "category":
"Fruit" },
 { "_id": 3, "product": "Banana", "category":
"Fruit" }
 ]

Query:

db.purchases.aggregate([
{ $group: { _id: "$category", uniqueProducts: {
$addToSet: "$product" } } }
])

 Result:
 [
 { "_id": "Fruit", "uniqueProducts":
["Apple", "Banana"] }
 ]
o Upsert Option

In MongoDB, the upsert option is used in update operations to insert a new


document if no matching document is found. It combines the functionality of
"update" and "insert" within a single command.

 Syntax
 db.collection.updateOne(
 { filterCriteria },
 { updateOperations },
 { upsert: true }
 )
 Example

Suppose we have a products collection:

[
{ "_id": 1, "name": "Laptop", "price": 1200 }
]
If we try to update a document with a filter that doesn’t match any
existing documents, upsert will insert a new document.

 Query:
 db.products.updateOne(
 { name: "Phone" },
 { $set: { price: 800 } },
 { upsert: true }
 )
 Result: Since no document with name: "Phone" exists,
MongoDB inserts a new document:
 { "_id": ObjectId(...), "name": "Phone", "price":
800 }

Summary

The upsert option is useful when you want to either update existing
documents or insert a new one if no matches are found in the
collection.

 MongoDB Aggregation Operators


o $match Filters documents by conditions.

The **$match** operator filters documents in a MongoDB aggregation


pipeline based on specified conditions, passing only the documents that meet
these conditions to the next stage.

 Syntax
 { $match: { <query> } }

Where <query> contains the conditions to filter documents.

 Example

Suppose we have a collection named employees with the following


documents:

[
{ "_id": 1, "firstName": "John", "age": 25,
"department": "HR" },
{ "_id": 2, "firstName": "Jane", "age": 35,
"department": "Finance" },
{ "_id": 3, "firstName": "Mike", "age": 28,
"department": "HR" }
]

Query:

To find employees aged above 30, you would use:

db.employees.aggregate([
{ $match: { age: { $gt: 30 } } }
])
 Result
 [
 { "_id": 2, "firstName": "Jane", "age": 35,
"department": "Finance" }
 ]

This result shows only the employee who is older than 30.

o $group Groups documents

The $group operator in MongoDB is used to aggregate data by categorizing


documents based on specified fields and performing calculations on each
group, such as counting, summing, or averaging values.

 Syntax
 {
 $group: {
 _id: <expression>,
 <field1>: { <accumulator1>: <expression1> },
 ...
 }
 }
_id: The field or expression used to group the documents.
<field1>: The name of the new field in the result that will hold
computed values.
 <accumulator1>: An accumulator operator (e.g., $sum, $avg,
$max, $min) that defines the calculation to perform.
 <expression1>: The field or expression to which the
accumulator applies.
 Example

Suppose we have a collection called orders with the following


documents:

{ "_id": 1, "customer_id": "C1", "amount": 110 },


{ "_id": 2, "customer_id": "C2", "amount": 150 },
{ "_id": 3, "customer_id": "C1", "amount": 90 },
{ "_id": 4, "customer_id": "C3", "amount": 200 },
{ "_id": 5, "customer_id": "C2", "amount": 50 }
]

To group the orders by customer_id and calculate the total amount


spent by each customer, you can use the following aggregation query:

db.orders.aggregate([
{
$group: {
_id: "$customer_id",
total_spent: { $sum: "$amount" }
}
}
])
 Result
 [
 { "_id": "C1", "total_spent": 200 },
 { "_id": "C2", "total_spent": 200 },
 { "_id": "C3", "total_spent": 200 }
 ]

This result shows the total amount spent by each customer,


aggregated based on their customer_id.

o $sortSorts documents by specified fields.

The $sort operator in MongoDB is used to sort documents that pass through
the aggregation pipeline based on specified field(s) in either ascending or
descending order.

 Syntax
 { $sort: { field1: <sort order>, field2: <sort
order>, ... } }
 field1, field2: The fields by which you want to sort the
documents.
<sort order>: Use 1 for ascending order and 1 for descending
order.
 Examples

Assume you have a collection called students with the following


documents:

[
{ "_id": 1, "name": "Alice", "age": 22 },
{ "_id": 2, "name": "Bob", "age": 25 },
{ "_id": 3, "name": "Charlie", "age": 20 }
]

 1: Sorting by Age in Ascending Order

To sort the students by their age in ascending order:

db.students.aggregate([{ $sort: { age: 1 } }])

 Result:
 [
 { "_id": 3, "name": "Charlie", "age": 20 },
 { "_id": 1, "name": "Alice", "age": 22 },
 { "_id": 2, "name": "Bob", "age": 25 }
 ]
 2: Sorting by Age in Descending Order

To sort the students by their age in descending order:

db.students.aggregate([{ $sort: { age: -1 } }])

 Result:
 [
 { "_id": 2, "name": "Bob", "age": 25 },
 { "_id": 1, "name": "Alice", "age": 22 },
 { "_id": 3, "name": "Charlie", "age": 20 }
 ]
 3: Sorting by Multiple Fields

If you want to sort the documents first by age in descending


order and then by name in ascending order, you can do:

db.students.aggregate([{ $sort: { age: -1, name:


1 } }])

Assuming the same documents, this will sort the students by


age first, and if there are any ties, it will sort those by name.

 Result:
 [
 { "_id": 2, "name": "Bob", "age": 25 },
 { "_id": 1, "name": "Alice", "age": 22 },
 { "_id": 3, "name": "Charlie", "age": 20 }
 ]

Important Note

The $sort operator can be resource-intensive, especially with


large datasets. It's best to place it towards the end of your
aggregation pipeline to improve performance.
o $project Chooses fields to show.

The $project operator in MongoDB is used in the aggregation framework to


specify which fields to include or exclude in the output documents. It reshapes
the data returned from the database.

 Syntax
 { $project: { field1: <value>, field2: <value>, ... } }
 field1, field2: Names of the fields to include (1) or exclude (0).
 Example

Assuming a collection named users with documents like:

{
"_id": 1,
"name": "John Doe",
"age": 30,
"email": "[email protected]"
}

Query:

To include only the name and exclude the email, use:

db.users.aggregate([
{
$project: {
name: 1,
email: 0 // Exclude email
}
}
]);

 Result
 {
 "_id": 1,
 "name": "John Doe"
 }

This query returns documents that only include the name field
and exclude the email field.

o $skip Skips specified documents.

The $skip operator is used to skip a specified number of documents in an


aggregation pipeline. It is commonly used for pagination in query results.

 Syntax
 { $skip: <number> }
 <number>: The number of documents to skip.
 Example

Assume you have a collection called employees with the following


documents:

[
{ "_id": 1, "name": "John", "age": 28 },
{ "_id": 2, "name": "Jane", "age": 32 },
{ "_id": 3, "name": "Bob", "age": 25 },
{ "_id": 4, "name": "Alice", "age": 30 },
{ "_id": 5, "name": "Charlie", "age": 27 },
{ "_id": 6, "name": "Diana", "age": 29 }
]

Example: Skipping Documents

If you want to skip the first 3 documents in the employees collection:

db.employees.aggregate([
{ $skip: 3 }
])

 Result:
 [
 { "_id": 4, "name": "Alice", "age": 30 },
 { "_id": 5, "name": "Charlie", "age": 27 },
 { "_id": 6, "name": "Diana", "age": 29 }
 ]

Important Notes
Order Matters: The $skip operator does not guarantee the
order of documents passed through it, so it's recommended to
use $sort before $skip if the order is important.
 Performance Consideration: For better performance, consider
combining $skip with additional filters and placing it later in
the aggregation pipeline.
o $limit Limits result count.

The $limit operator restricts the number of documents that are passed to the
next stage in the aggregation pipeline. It is particularly useful for debugging
and controlling the output of queries.

 Syntax
 { $limit: <number> }
 <number>: The maximum number of documents to return.
 Example

Assume you have a collection called employees with the following


documents:

[
{ "_id": 1, "name": "John", "age": 28 },
{ "_id": 2, "name": "Jane", "age": 32 },
{ "_id": 3, "name": "Bob", "age": 25 },
{ "_id": 4, "name": "Alice", "age": 30 },
{ "_id": 5, "name": "Charlie", "age": 27 },
{ "_id": 6, "name": "Diana", "age": 29 }
]

Example: Limiting Documents

If you want to limit the number of documents returned to 3 from the


employees collection:

db.employees.aggregate([
{ $limit: 3 }
])

 Result:
 [
 { "_id": 1, "name": "John", "age": 28 },
 { "_id": 2, "name": "Jane", "age": 32 },
 { "_id": 3, "name": "Bob", "age": 25 }
 ]

Important Notes

The $limit operator is often used at the end of an aggregation


pipeline to restrict the output size.
 It can be combined with other operators (like $sort) to control
which documents are returned based on specific criteria. For
example, you might sort by age first and then limit the results.
o $sum Adds up values in a group.
The $sum operator in MongoDB is a powerful tool used in the aggregation
pipeline to calculate the total sum of numeric values. It can be applied to fields
in documents or used with expressions that evaluate to numeric values.

 Syntax

The basic syntax for using the $sum operator is:

{ $sum: <expression> }

 Example

Assume you have a collection named orders with the following


documents:

[
{ "_id": 1, "total": 50 },
{ "_id": 2, "total": 100 },
{ "_id": 3, "total": 150 }
]

Objective

You want to calculate the total sum of the total field across all
documents in the orders collection.

Aggregation Query
db.orders.aggregate([
{
$group: {
_id: null, // No specific grouping, we want the
total for all documents
totalSum: { $sum: "$total" } // Sum the 'total'
field
}
}
]);

 Result

The output of the query would be:

[
{
"_id": null,
"totalSum": 300
}
]

Explanation
 $group: Combines the documents into a single output
document.
 totalSum: The new field in the output that contains the
sum of the total field from all documents.
 $sum: "$total": Calculates the sum of the total
field.

This example clearly demonstrates how to use the $sum


operator in a straightforward manner!

o $unwind Expands array elements into documents.

The $unwind operator deconstructs an array field from a document, creating a


separate document for each element in that array.

When you use the $unwind operator in MongoDB on an array that contains
multiple objects, it will return each object in the array as a separate document.
This allows you to work with each object individually, which is useful for
analysis or manipulation of the data.

 Syntax
 {
 $unwind: {
 path: "<arrayField>",
 includeArrayIndex: "<string>", // Optional
 preserveNullAndEmptyArrays: <boolean> // Optional
 }
 }
path: The field path of the array you want to unwind (prefixed
with $).
 includeArrayIndex: (Optional) Adds the index of the array
element.
 preserveNullAndEmptyArrays: (Optional) If true, includes
documents with no array.
 Example

Sample Document in sales Collection:

{
"_id": 1,
"item": "itemA",
"orders": [
{ "quantity": 2, "unitPrice": 10 },
{ "quantity": 3, "unitPrice": 20 }
]
}

Using $unwind:

db.sales.aggregate([
{ $unwind: { path: "$orders" } }
])
 Result:
 [
 { "_id": 1, "item": "itemA", "orders":
{ "quantity": 2, "unitPrice": 10 } },
 { "_id": 1, "item": "itemA", "orders":
{ "quantity": 3, "unitPrice": 20 } }
 ]

This output creates separate documents for each order in the


orders array.

o $lookup Joins documents from other collections.

The $lookup operator is used to perform a left outer join between two
collections in MongoDB, allowing you to combine documents from one
collection with documents from another based on a shared field.

In MongoDB, you can join up to 60 collections in a single aggregation


pipeline using the $lookup stage.

 Syntax
 {
 "$lookup": {
 "from": "<collection_name>",
 "localField": "<field_from_input_documents>",
 "foreignField":
"<field_from_documents_of_the_from_collection>",
 "as": "<output_array_field>"
 }
 }

Parameters

 from: The target collection to join with.


 localField: The field from the input collection (the one on
which $lookup is applied).
 foreignField: The field from the target collection that
corresponds to the local field.
as: The name of the output array field that will hold the joined
documents.
 Example

In simple terms, the $lookup operator connects two collections in


MongoDB:

 One collection has documents (like orders) that need more


information.
 The other collection has related documents (like products) that
provide that extra information.
So, it’s like looking at a list of orders and finding the matching product
details for each order from another list of products. This way, you can
see both the orders and their related product information together.

1. orders Collection

[
{ "orderId": 1, "productId": "A", "quantity": 2 },
{ "orderId": 2, "productId": "B", "quantity": 3 }
]

2. products Collection

[
{ "productId": "A", "productName": "Widget", "price":
10 },
{ "productId": "B", "productName": "Gadget", "price":
20 }
]

Aggregation Query

To join the orders collection with the products collection based on


productId and calculate the total amount for each order:

javascript
Copy code
db.orders.aggregate([
{
$lookup: {
from: 'products',
localField: 'productId',
foreignField: 'productId',
as: 'productDetails'
}
},
{
$unwind: '$productDetails' // Flatten the array of
product details
},
{
$project: {
orderId: 1,
totalAmount: {
$multiply: ['$quantity', '$productDetails.price']
// Calculate total amount
}
}
}
]);

 Result

The output will look like this:


[
{ "orderId": 1, "totalAmount": 20 }, // 2 * 10
{ "orderId": 2, "totalAmount": 60 } // 3 * 20
]

Explanation

0. $lookup: Joins the orders collection with the


products collection where productId matches. The
matched documents are stored in the productDetails
array.
1. $unwind:Deconstructs the productDetails array into
individual documents.
2. $project: Selects the orderId and calculates
totalAmount by multiplying quantity with the price
from productDetails.

Conclusion

The $lookup operator is essential for combining related data


across multiple collections in MongoDB, making it a powerful
tool for complex queries.

o $bucket Groups into value ranges.

The $bucket operator takes a set of documents and divides them into discrete
groups, called buckets, based on a specified field. Each bucket can represent a
range of values, and you can perform aggregate calculations on the documents
in each bucket.

 Syntax

Here’s the basic syntax for the $bucket operator:

{
$bucket: {
groupBy: <expression>, // The field to group
by (e.g., a number or date).
boundaries: [ <boundary1>, <boundary2>, ... ], // An
array of boundary values that define the ranges for the
buckets.
default: <defaultBucket>, // (Optional) Name of
the bucket for values outside the specified boundaries.
output: { // An object defining
what to return for each bucket.
<outputField1>: { <accumulator1>: <expression1> },
...
}
}
}

 Example
Imagine you have a collection called scores that contains students'
exam scores:

[
{ "student": "Alice", "score": 85 },
{ "student": "Bob", "score": 92 },
{ "student": "Charlie", "score": 75 },
{ "student": "David", "score": 65 },
{ "student": "Eve", "score": 89 },
{ "student": "Frank", "score": 55 }
]

Using $bucket

You want to group these scores into ranges (buckets) to see how many
students fall into each score range.

Aggregation Query
javascript
Copy code
db.scores.aggregate([
{
$bucket: {
groupBy: "$score", // Group by
the 'score' field.
boundaries: [0, 60, 75, 85, 100], // Define the
boundaries for the buckets.
default: "Other", // Optional:
Name for scores outside the defined ranges.
output: {
count: { $sum: 1 }, // Count the
number of students in each bucket.
totalScore: { $sum: "$score" } // Sum the
scores in each bucket.
}
}
}
]);

 Result

The output will look something like this:

[
{ "range": "[0, 60)", "count": 1, "totalScore":
55 }, // 1 student with a score < 60
{ "range": "[60, 75)", "count": 1, "totalScore":
65 }, // 1 student with a score between 60 and 75
{ "range": "[75, 85)", "count": 2, "totalScore":
164 }, // 2 students with scores between 75 and 85
{ "range": "[85, 100)", "count": 2, "totalScore":
177 } // 2 students with scores between 85 and 100
]
Explanation of the Result

 The first bucket contains students who scored between 0


and 60. There is 1 student (Frank), and the total score
is 55.
 The second bucket is for scores 60 to 75 with 1 student
(David), and the total score is 65.
 The third bucket contains scores 75 to 85, with 2
students (Charlie and Alice), totaling 164.
 The last bucket is for scores 85 to 100, with 2 students
(Bob and Eve), totaling 177.

Conclusion

The $bucket operator helps you categorize and analyze data by


creating ranges, making it easier to see how many entries fall into each
category.

o $out Saves output to a collection.

Writes the results of an aggregation pipeline into a specified collection,


replacing the existing collection if it exists.

 Syntax:
 { $out: "outputCollection" }
 Example:

Example Collection: sales

[
{ "_id": 1, "product": "Laptop", "amount": 1200,
"status": "completed" },
{ "_id": 2, "product": "Phone", "amount": 800,
"status": "completed" },
{ "_id": 3, "product": "Tablet", "amount": 400,
"status": "pending" },
{ "_id": 4, "product": "Monitor", "amount": 300,
"status": "completed" },
{ "_id": 5, "product": "Keyboard", "amount": 150,
"status": "completed" }
]

 Write all completed sales to a new collection.


 Query:
 db.sales.aggregate([
 { $match: { status: "completed" } },
 { $out: "completed_sales" }
 ])
 Result in completed_sales:
 [
 { "_id": 1, "product": "Laptop", "amount": 1200,
"status": "completed" },
 { "_id": 2, "product": "Phone", "amount": 800,
"status": "completed" },
 { "_id": 4, "product": "Monitor", "amount": 300,
"status": "completed" },
 { "_id": 5, "product": "Keyboard", "amount":
150, "status": "completed" }
 ]
 Summary: $out saves documents that match a query
(completed sales) into a new collection completed_sales.
o $facet Runs multiple pipelines together.

Processes multiple aggregation pipelines within a single stage, outputting the


results of each pipeline as subfields.

 Syntax:
 { $facet: { stage1: [ <pipeline1> ], stage2:
[ <pipeline2> ] } }
 Example:

Example Collection: sales

[
{ "_id": 1, "product": "Laptop", "amount": 1200,
"status": "completed" },
{ "_id": 2, "product": "Phone", "amount": 800,
"status": "completed" },
{ "_id": 3, "product": "Tablet", "amount": 400,
"status": "pending" },
{ "_id": 4, "product": "Monitor", "amount": 300,
"status": "completed" },
{ "_id": 5, "product": "Keyboard", "amount": 150,
"status": "completed" }
]

 Analyze sales into high-value and low-value categories based


on the amount.
 Query:
 db.sales.aggregate([
 { $facet: {
 "highValueSales": [
 { $match: { amount: { $gt: 500 } } },
 { $count: "count" }
 ],
 "lowValueSales": [
 { $match: { amount: { $lte: 500 } } },
 { $count: "count" }
 ]
 } }
 ])
 Result:
 {
 "highValueSales": [ { "count": 2 } ],
 "lowValueSales": [ { "count": 3 } ]
 }
 Summary: $facet runs multiple aggregations (high-value and
low-value sales) simultaneously within the same pipeline.
o $fill Fills gaps with default values.

The $fill operator in MongoDB is used to fill in gaps in time series data
during an aggregation operation. Specifically, it allows you to specify default
values for fields when there are missing documents for certain time intervals
in the output of an aggregation pipeline.

 Syntax
 {
 $fill: {
 output: {
 fieldName1: value1,
 fieldName2: value2,
 // ... additional fields as needed
 }
 }
 }

 Example:

Suppose you have a time series collection named


temperatureReadings with the following documents:

{ "date": ISODate("2024-11-01"), "temperature": 15 }


{ "date": ISODate("2024-11-02"), "temperature": 18 }
{ "date": ISODate("2024-11-04"), "temperature": 20 }

Notice that there is no reading for 2024-11-03.

Aggregation Query:

You want to get the average temperature for each day, filling missing
days with a default temperature of 0.

db.temperatureReadings.aggregate([
{
$group: {
_id: { $dateTrunc: { date: "$date", unit:
"day" } },
avgTemperature: { $avg: "$temperature" }
}
},
{
$fill: {
output: { avgTemperature: 0 }
}
}
]);

 Result:

The output will be:

{ "_id": ISODate("2024-11-01"), "avgTemperature":


15 }
{ "_id": ISODate("2024-11-02"), "avgTemperature":
18 }
{ "_id": ISODate("2024-11-03"), "avgTemperature": 0
} // Filled
{ "_id": ISODate("2024-11-04"), "avgTemperature":
20 }

In this result, the 2024-11-03 entry is filled with 0, indicating


that there was no temperature recorded on that day

 Aggregation & Pipeline

Aggregation in MongoDB is a framework that transforms collections into computed


results, enabling data analysis and reporting. It processes documents through a series
of stages, where each stage applies specific operations to produce refined outputs.

0. Pipeline:
 A pipeline is a series of stages executed in order to process the data.
 Each stage transforms the data and passes it to the next stage.
 The output of the last stage is the final result of the pipeline.
1. Stage:
 A stage is a single operation applied to the data.
 It can involve simple transformations or complex aggregations.
 Each stage has a specific purpose and is responsible for a single task.
2. Operator:
 An operator is a special symbol used to perform specific operations on
the data.
 Operators can be mathematical, logical, or comparison-based.

o Example of a Simple Aggregation Pipeline:


o db.collection.aggregate([
o { $match: { status: 'A' } },
o { $group: { _id: '$cust_id', total: { $sum: '$amount' } } },
o { $sort: { total: -1 } },
o ]);
 Explanation of the Pipeline:

0. $match: Filters the collection to include only documents where


the status is "A".
1. $group: Groups the filtered documents by cust_id and
calculates the total amount spent by each customer using $sum.
2. $sort: Sorts the results by the total amount in descending order.

Conclusion

The aggregation framework in MongoDB is a powerful tool for data processing and
analysis, allowing users to perform complex queries and calculations efficiently.
Understanding the concepts of pipelines, stages, and operators is essential for
leveraging this capability effectively.

 Single Purpose Aggregation


Single Purpose Aggregation in MongoDB refers to a set of aggregation operations
designed to perform specific tasks, returning a computed result based on a single
calculation. Unlike the more general aggregation framework, which allows for
complex transformations, single-purpose aggregation methods are optimized for
simpler, targeted operations.

Common Single-Purpose Aggregation Methods:

0. count(): Counts the number of documents that match a query.


1. db.collection.count({ status: "active" })

This counts all documents in the collection where the status field is "active".

2. distinct(): Returns an array of unique values for a given field.


3. db.collection.distinct("category")

This retrieves all distinct category values from the collection.

4. group(): Groups documents by a specified key and performs an operation like


sum or average. (Note: The group() command has been deprecated, and it's
better to use $group in aggregation pipelines.)
5. findAndModify(): Finds a document, modifies it, and returns the document in
a single atomic operation.
6. db.collection.findAndModify({
7. query: { name: "Alice" },
8. update: { $set: { score: 90 } },
9. new: true
10. })

Summary:

Single-purpose aggregation methods are used for tasks like counting, finding distinct
values, or basic group operations. They are ideal when you need quick, simple results,
without the need for the more extensive aggregation framework.

 ACID

o ACID (Atomicity, Consistency, Isolation, Durability)

properties ensure reliable transaction processing:

o Atomicity: Transactions are all-or-nothing.


o Consistency: Data must remain in a valid state before and after a transaction.
o Isolation: Transactions occur independently.
o Durability: Changes are permanent once a transaction is committed.
o Explained

0. Atomicity: This ensures that a transaction is treated as a single,


indivisible unit. Either all the steps in the transaction complete
successfully, or none do.
Example: In a bank transfer, if money is withdrawn from one account,
it must be deposited in another. If one part of this process fails, the
entire transaction is canceled, so no money is lost.

1. Consistency: This guarantees that a transaction brings the database


from one valid state to another, following all rules and constraints.

Example: Suppose there's a rule that account balances cannot be


negative. Any transaction that violates this rule will be rolled back,
preserving database integrity.

2. Isolation: This property ensures that multiple transactions can occur


simultaneously without affecting each other. It prevents one
transaction from reading data modified by another until the change is
complete.

Example: If two customers try to purchase the last item in stock at the
same time, isolation ensures that only one of them completes the
transaction to avoid double selling.

3. Durability: Once a transaction is committed, the changes are


permanent, even in the event of a system failure.

Example: After a successful transaction, such as a bank transfer, the


system will save this transaction, so the transferred amount is not
reversed, even if the server crashes right afterward.

 Transactions in MongoDB
o MongoDB generally follows the BASE principles for its consistency model,
especially in the context of its replication and sharding features, where
eventual consistency is prioritized for availability and scalability.
o However, MongoDB transactions, starting from version 4.0, follow ACID
principles for multi-document operations. This means MongoDB can support
ACID transactions (Atomicity, Consistency, Isolation, Durability) when
needed for complex operations like updates across multiple documents or
collections.
 Journaling in MongoDB

In MongoDB, journaling is a feature that helps protect your data. When you make
changes to the database, MongoDB first records these changes in a "journal" before
saving them. This journal acts like a backup, so if MongoDB unexpectedly shuts
down, it can use the journal to recover and keep your data safe.

Why Journaling is Important:

0.Data Protection: Journaling keeps a backup of changes before they're saved.


1.Recovery: If MongoDB crashes, it can use the journal to restore recent
changes.
 CAP Theorem
The CAP theorem states that in a distributed data store, you cannot guarantee all three
of the following at the same time:

0. Consistency: All nodes have the same data at the same time. Every read
returns the most recent write.
1. Availability: Every request gets a response, even if the data isn't up to date.
The system is always operational.
2. Partition Tolerance: The system continues to work despite network failures
between nodes.

Key Points:

o You can only achieve two out of the three properties:

 CP (Consistency and Partition Tolerance): The system ensures data


consistency but may become unavailable during a network partition.
 AP (Availability and Partition Tolerance): The system remains
available during a partition but may return stale data.
 CA (Consistency and Availability): This isn't possible in distributed
systems with potential network partitions.

Conclusion:

The CAP theorem helps developers understand the trade-offs in designing distributed
systems, guiding decisions based on specific needs.

 Replication and Replica Sets

Replication in MongoDB is the process of duplicating data across multiple servers to


ensure high availability and data redundancy. It allows data to be accessible even if
one server fails, providing fault tolerance and improving data reliability.

o malayalam

MongoDB ലെ Replication എന്നത്, ഡാറ്റയുടെ പകർപ്പുകൾ വിവിധ


സർവറുകളിൽ നിലനിർത്തുന്ന പ്രക്രിയയാണ്. ഇത് ഉയർന്ന
ലഭ്യതയും ഡാറ്റ പുനരാവൃത്തിയും ഉറപ്പാക്കുന്നു. ഒരു സർവർ
പരാജയപ്പെട്ടാലും ഡാറ്റക്ക് ലഭ്യമാകാനുള്ള സാഹചര്യം ഇത്
ഒരുക്കുന്നു,Fault tolerance-നെ ഉറപ്പാക്കുകയും ഡാറ്റയുടെ
വിശ്വസ്തത മെച്ചപ്പെടുത്തുകയും ചെയ്യുന്നു.

Replica Sets are groups of MongoDB servers that maintain the same dataset. A
replica set has:

o Primary Node: The main server where all write operations happen. It
replicates data to secondary nodes.
o Secondary Nodes: Servers that replicate data from the primary. They can
serve read requests if configured, and if the primary fails, one secondary
becomes the new primary.
o Arbiter: A member that does not hold data but helps with elections to decide
which secondary becomes the primary during failover.
 Types of Replication
o 1 . Master-Slave Replication (Old model, now deprecated)
 How it works: One server (master) handles all the writes, and other
servers (slaves) copy the data from the master.
 Problem: If the master goes down, no one can write to the database.
 Current Status: No longer used in recent MongoDB versions.
o

2. Replica Sets (Current model)

 How it works: A group of servers (nodes) work together. One is the


primary (handles writes), and others are secondaries (copy data from
the primary and can handle reads).
 Features:
 If the primary goes down, a secondary automatically becomes
the new primary.
 You can direct reads to secondaries for better performance.
 Current Status: The standard and recommended model for MongoDB
replication.
o

3. Sharded Clusters (For handling large data)

 How it works: MongoDB data is split into shards (parts), and each
shard is a replica set.
 Features:
 Used when you need to store huge amounts of data that can't fit
on a single server.
 Each shard can handle reads and writes, and data is
automatically distributed across servers.
 Current Status: Used for horizontal scaling when the database grows
too large for one replica set.
o

4. Delayed Replica Sets (For data recovery)

 How it works: One of the secondaries in a replica set has a delay in


replicating data.
 Features: This allows you to "go back in time" if data was deleted or
changed by mistake.
 Use Case: Helps recover lost data by reverting to an earlier state.

Summary
Type Description Failover Scalability
One master, one or more slaves (old
Master-Slave Manual Limited
model)
Primary and secondary servers
Replica Sets Automatic Can scale reads
(current model)
Data is split across multiple replica Handles huge
Sharded Clusters Automatic
sets data
Delayed Replica A secondary node has a delay for
Manual No impact
Sets recovery

 Indexes , TTL Index, The trade-off, B- Tree 🌲, Covered Query, GridFS


o Indexes

Indexes allow MongoDB to locate documents faster, making queries


(searches) more efficient. Without indexes, MongoDB has to check every
document in a collection to find the ones you need, which can be slow if
there’s a lot of data.

 Indexing

This is the process of creating these indexes, effectively creating


shortcuts for MongoDB to speed up data retrieval.

An index is like a "shortcut" that MongoDB can use to quickly locate


the desired data, instead of scanning the entire collection.

 Type of Indexing

0. Single Field Index (Default Index)

 An index created on a single field.


 It allows MongoDB to quickly find documents based on
the value of that field.

Example: Index on the name field:

db.collection.createIndex({ name: 1 })

 Here, 1 indicates an ascending order.


2. Compound Index

 An index on multiple fields.


 Useful when queries involve more than one field.

Example: Index on both class and score fields:

db.collection.createIndex({ class: 1, score: -1 })


 This creates an index on class in ascending order and
score in descending order.

3. Multikey Index

 An index on array fields.


 MongoDB indexes each element of the array
individually.

Example: Index on an array field tags:

db.collection.createIndex({ tags: 1 })

 This will index each element in the tags array.


4. Text Index

 Special index to support text search.


 It indexes string content in text fields.

Example: Create a text index on description:

db.collection.createIndex({ description: "text" })

5. Hashed Index

 Indexes the hash of a field’s value, used for sharding


purposes.
 It is used for evenly distributing documents across
shards.

Example: Hashed index on user_id:

db.collection.createIndex({ user_id: "hashed" })

6. Geospatial Index

 Index used for geospatial queries.


 MongoDB provides two types of geospatial indexes: 2d
and 2dsphere.
 2dsphere index: Helps you work with Earth-like
coordinates (like latitude and longitude) for location-
based queries.
 2d index: Used for flat, two-dimensional points (like on
a map) to run location queries.

Example: 2dsphere index for geographic coordinates:

db.collection.createIndex({ location: "2dsphere" })

7. Wildcard Index

 Indexes multiple fields without specifying the field


names.
 It is useful when the schema is flexible or fields are
unknown beforehand.

Example: Wildcard index on all fields:

db.collection.createIndex({ "$**": 1 })

 Types of Indexes (Simplified)

0. Single-Field Index: An index on one field only. For example,


an index on the name field.

db.students.createIndex({ age: 1})

1. Compound Index: Combines multiple fields, like name and


age, to help with queries that use both fields.
db.students.createIndex({ "name": 1 , "age": 1})
2. Text Index: Enables searches within text fields (e.g.,
keywords).
3. Multikey Index: Allows you to index array fields (an index
that can be created on an array field), meaning each element in
an array can be indexed.
4. Geospatial Index: For location-based data (e.g., finding places
near a specific point)
 Indexes: Structures that help speed up data retrieval in MongoDB.
 When not to use indexing in MongoDB
0. When collection is small
1. When the collection is frequently updated
2. When the queries are complex (multiple fields)
3. When the collection is large (make less indices)
 Example

Creating an Index: Suppose you create a compound index on the


namefield:

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

o Index vs Indexes vs Indexing


 Index is a single data structure.
 Indexes are multiple index structures.
 Indexing is the process of creating an index
o TTL Index

A TTL (Time-to-Live) Index is a special type of index that automatically


deletes documents after a specified period. This is useful for managing
temporary data.

db.logs.insertOne({
message: "Temporary log",
createdAt: new Date() // Current time
});
db.logs.createIndex(
{ "createdAt": 1 }, // Index on createdAt field
{ expireAfterSeconds: 30 } // Expire after 30 seconds
);

o The trade-off

0. Faster Queries: Indexes make queries run faster by allowing


MongoDB to locate data quickly, rather than scanning every document
in a collection.
1. Increased Storage: Indexes require extra storage. Each index takes up
space in the database, which grows with the amount of data in the
indexed field.
2. Slower Writes: Every time a document is inserted, updated, or
deleted, the indexes need to be updated too. This can make write
operations slower as MongoDB has to maintain these indexes.

 Example of Trade-offs

If you have a students collection with a million documents and


frequently search by name, creating an index on the name field will
speed up those searches. But:

 It will take extra storage to maintain that index.


 It may slow down when adding new students because
MongoDB must update the name index every time.
o B- Tree 🌲

What is a B-tree?

A B-tree is a type of tree data structure used to organize and store data
efficiently. It’s commonly used in databases and filesystems.

 Key Features:

0. Balanced: All leaf nodes (end points) are at the same level,
which keeps the tree balanced.
1. Multi-way Nodes: Each node can have multiple keys and
children, not just two like a regular binary tree.
2. Sorted Data: Keys within each node are kept in sorted order,
making searches faster.
3. Efficient Operations: B-trees allow for quick searching,
inserting, and deleting of keys.
 How It Works:

 Search: You can find a key by comparing it to the keys in a


node and moving to the correct child node, repeating this
process until you find the key or reach a leaf node.
 Insert: When adding a key:

If the node has space, just add it in order.



If it’s full, split the node and move the middle key up to

the parent.
 Delete: Removing a key might involve merging nodes or
borrowing keys from siblings if the node gets too small.
 Example:

Imagine a B-tree with a maximum of 3 keys per node:

css
Copy code
[10, 20]
/ | \\
/ | \\
[5] [15] [25, 30]

 The root node has two keys: 10 and 20.


 It has three children:

The first child has keys less than 10,



The second child has keys between 10 and 20,

The third child has keys greater than 20.

 Why Use B-trees?

B-trees are great for efficiently managing large amounts of sorted data,
making them ideal for databases where quick access is essential.

o Covered Query
 All the fields in the query are part of an index.
 All the fields returned in the query are in the same index
 Purpose: The primary benefit of a covered query is performance
improvement. Since MongoDB can retrieve the requested data directly
from the index, it avoids the overhead of fetching documents from the
data store, making the query faster and using less I/O.
 Requirements for a Covered Query:

0. The query must only reference fields that are part of an index.
1.All fields in the projection (the fields you want to return) must
also be included in the index.
2. The _id field is implicitly included in every index.
 Example

Covered Query: If you run a query like this:

db.collection.find({ name: "Alice" }, { name: 1, age:


1 });

 Covered Queries: Queries that can be entirely satisfied using an


index, allowing for more efficient data access
o Winning plan

MongoDB checks the performance of index on a sample document once the


queries is run and set it as winning plan.

Then for second query of similar type it doesn’t race them again.

It store that winning plan in cache

o GridFS

GridFS: A specification for storing and retrieving large files in MongoDB. It


divides large files into smaller chunks (usually 255 KB) and stores them in a
chunks collection, while the file's metadata is stored in a files collection.
This allows MongoDB to handle files larger than its BSON-document size
limit of 16 MB, making it suitable for storing large files like images, videos,
and audio.

 Database scaling
o Vertical Scaling (Scaling Up)
 What it Means: Increasing the capacity of a single MongoDB server
by adding more resources, like CPU, RAM, or storage.
 Benefits: Simple to implement, as you’re only working with one
server.
 Limitations: Limited by the maximum hardware capacity of that
single server, and costs can increase with higher specifications.
 Usage: Useful for small or medium-sized databases where the
workload can be handled by a single, more powerful server.
o Horizontal Scaling (Scaling Out)
 What it Means: Adding more servers to distribute the workload,
especially for large databases or high traffic.
 Key Methods in MongoDB:

 Sharding: MongoDB splits large collections across multiple


servers (shards). Each shard stores part of the data, distributing
storage and load.
 Replication: MongoDB duplicates data across multiple servers
in a replica set to increase availability and read capacity. This is
also a form of horizontal scaling as it spreads data across
multiple machines.
 Benefits: Virtually unlimited scaling by adding more servers, improves
read and write capacity, and provides data redundancy.
 Usage: Essential for applications with large datasets or high traffic, as
it spreads the data and requests across multiple machines.
o How MongoDB Scaling Helps:
 More Data Storage: Sharding increases storage space by adding more
servers.
 High Availability: Replication keeps data safe if a server fails.
 Load Balancing: Distributes requests across multiple servers,
improving speed and efficiency.

MongoDB uses these techniques to grow with your data and user base,
keeping the database fast and reliable.

o Sharding (Horizontal Scaling)


 MongoDB splits big collections across multiple servers, called shards.
 A shard key decides how data is spread out.
 This helps MongoDB store more data and handle more requests.
o Replication
 MongoDB copies data across several servers in a replica set.
 There’s one primary server (for writing) and multiple secondary
servers (for backups and extra reading).
 If the primary server goes down, a secondary server takes over.
 This keeps data available and spreads read requests.
o Database Profiler

The Database Profiler in MongoDB is a tool used to track and analyze


database operations, like queries and updates. It helps you find slow
operations and optimize your database's performance.

 Examples:
 Enable Profiler for Slow Queries (queries slower than
100ms):
 bb.setProfilingLevel(1, { slowms: 100 });
 Enable Profiler for All Operations:
 db.setProfilingLevel(2);
 View Collected Data:
 db.system.profile.find().sort({ ts: -1 });

 Disable the Profiler:


 db.setProfilingLevel(0);
 Why Use It?
 Find slow queries.
 Optimize performance by adjusting slow operations.
 Monitor your database for any issues.
 Capped collection

A capped collection is a fixed-size collection that supports high-throughput


operations by maintaining documents in a first-in, first-out (FIFO) order.
0. Fixed Size: It has a set storage limit. When it reaches this size, old documents
are automatically removed to make room for new ones.
1. Insertion Order: Data is stored and read in the order it was added, so it’s
great for logs or other ordered data.
2. Fast Performance: Optimized for quick inserts, making it good for tracking
events or real-time data.
3. No Deletion: You can’t delete or change the size of documents once they’re in
the collection.

o Syntax
o db.createColllection("cappedCollection", { capped: true, size:
100 })
 Explanation
 db.createCollection: This is the method used to create a
collection.
 "cappedCollection": This is the name of the new capped
collection you want to create.
 { capped: true, size: 100 }: This option specifies that the
collection should be capped, meaning it has a fixed size. In this
case, the size is set to 100 bytes.
o How to check the collection is capped or not
o // if the collection is capped it will return true else it will
return false
o db.collectionName.isCapped();
 Clustered collection

A clustered collection organizes data based on a cluster key, which helps store and
retrieve related data more efficiently.

0. Cluster Key:
 Documents are stored in a specific order based on this key. For
example, if you often query data by a user_id, setting user_id as the
cluster key will make those queries faster.
1. Efficient Storage:
 Clustered collections use less storage for large datasets because they
are ordered, which helps reduce index size.
2. Optimized Performance:
 Retrieving data by the cluster key is faster, which can improve
performance in large databases.

Note: As of now, MongoDB doesn’t natively support clustered collections the same
way as some relational databases do, but MongoDB’s indexing and sharding options
offer similar advantages.

 WiredTiger

WiredTiger is the default storage engine for MongoDB ,introduced to enhance


performance, scalability, and data handling compared to the previous engine

Advantages of WiredTiger:
o Improved performance and storage efficiency for databases with high read and
write demands.
o Lower disk space usage thanks to compression.
o Enhanced concurrency with document-level locking, making it ideal for
workloads with multiple users or applications.
 Write Concern: & Read Concern:
o Write Concern: MongoDB double-checks that data is saved.
o Read Concern: MongoDB makes sure data is recent when you read it.

o Write Concern:

Write concern is about making sure your data is saved safely when you add,
change, or delete something.

0. Basic Confirmation: MongoDB saves the data and confirms right


away. Fast but a little risky.
1. Extra Confirmation: MongoDB checks with more servers to make
sure the data is safe. Safer but slower.

So, write concern is just how much you want MongoDB to double-check
that your data is saved.

Write Concern എന്നത് MongoDB ഡാറ്റ സുരക്ഷിതമായി സേവ്


ചെയ്‌തിരിക്കുന്നു എന്ന് ഉറപ്പാക്കാൻ എത്ര ജാഗ്രതയോടെ
പരിശോധിക്കണം എന്നതാണ്.

o Read Concern:

Read concern is about how fresh or updated the data is when you read it.

0. Quick Read: You get the data fast, but it might not be the latest from
all servers.
1. Confirmed Read: You get data only after MongoDB checks with
multiple servers. More accurate, but takes a bit longer.

Read Concern എന്നത് നിങ്ങൾ വായിക്കുന്ന ഡാറ്റ എത്ര


പുതിയതായിരിക്കണം എന്ന് പറയുക എന്നതാണ്.

 allowDiskUse

In MongoDB, allowDiskUse is an option used in aggregation queries that allows


MongoDB to use disk space if the operation exceeds the default memory limit (100
MB).

The allowDiskUse option in MongoDB is like asking for permission to use disk
space when an aggregation result takes more than the default memory limit of 100
MB.
 Normalization

Normalization in MongoDB means organizing data to avoid repeating it in multiple


places. It helps keep data tidy and makes updates easier by storing information in
fewer places.

 Watch

In MongoDB, watch allows you to see changes in the database as they happen. It's
like keeping an eye on your data so you know when something is added, updated, or
deleted, and you can react to it right away.

 Profiler

Profiler helps you track and analyze the performance of your database by logging
information about operations, like queries and updates. It shows which operations are
slow, so you can identify and fix performance problems.

o How to Use:
0. Enable Profiling:
1. db.setProfilingLevel(1, { slowms: 100 });

This logs operations that take more than 100ms.

2. View Profiling Data:


3. db.system.profile.find().pretty();

This shows the logged operations.

Why Use It:

o Find Slow Queries: Helps you see which queries are slow.
o Improve Performance: Lets you optimize slow operations for faster
performance.
 Oplog

In MongoDB, the Oplog (short for operation log) is a special capped collection that
records all changes made to the data in the database. It is used primarily in replica
sets to synchronize data across different members (nodes) of the replica set.

You might also like