Related Topics
Overview Of MongoDB
MongoDB Page 1
MongoDB Page 2
MongoDB Page 3
No SQl Database
MongoDB Page 4
MongoDB Page 5
Advantages Over RDBMS
MongoDB Page 6
MongoDB Page 7
MongoDB Data Types
MongoDB Page 8
MongoDB Data Modeling
MongoDB Page 9
Query & Projection Operator
MongoDB Page 10
MongoDB Page 11
MongoDB Update Operator
MongoDB Page 12
AggregationPipeline Stages
MongoDB Page 13
MongoDB Page 14
MongoDB Limit()
MongoDB Page 15
MongoDB Sort()
MongoDB Page 16
Query Modifiers
MongoDB Page 17
Aggregation Commands
MongoDB Page 18
Geospatial Command
MongoDB Page 19
Query and Write Operation Commands
MongoDB Page 20
Query Plan Cache Commands
MongoDB Page 21
Authentication Commands
MongoDB Page 22
Role Management Commands
MongoDB Page 23
Replication Command
MongoDB Page 24
Shading Commands
MongoDB Page 25
Session Commands
MongoDB Page 26
Create Database
MongoDB Page 27
Drop Database
MongoDB Page 28
Create Collection
MongoDB Page 29
Drop Collection
MongoDB Page 30
Inset Documents
MongoDB Page 31
Update Documents
MongoDB Page 32
Delete Documents
MongoDB Page 33
SQL to MongoDB Mapping
MongoDB Page 34
Introduction to React.js
React JS Page 1
React JS Page 2
React JS Page 3
Components in React.js
React JS Page 4
React JS Page 5
Virtual DOM in React.js
React JS Page 6
React JS Page 7
State and Props in React.js
React JS Page 8
React JS Page 9
React Router
React JS Page 10
React JS Page 11
React Hooks
React JS Page 12
React JS Page 13
Redux in React.js
React JS Page 14
React JS Page 15
Context API in React.js
React JS Page 16
React JS Page 17
React with Webpack and Babel
React JS Page 18
React JS Page 19
Testing in React.js
React JS Page 20
React JS Page 21
Deployment and Optimization in React.js
React JS Page 22
React JS Page 23
Emerging Trends and Best Practices in React.js
React JS Page 24
React JS Page 25
Introduction
Node.js Page 1
Node.js Page 2
Node.js Architecture and Event-Driven Programming
Node.js Page 3
Node.js Page 4
Modules and Packages in Node.js
Node.js Page 5
Node.js Page 6
File System and Buffers in Node.js
Node.js Page 7
Node.js Page 8
HTTP and Networking in Node.js
Node.js Page 9
Node.js Page 10
Express.js and Web Applications
Node.js Page 11
Node.js Page 12
Databases and ORMs in Node.js
Node.js Page 13
Node.js Page 14
RESTful APIs in Node.js
Node.js Page 15
Node.js Page 16
Testing and Debugging in Node.js
Node.js Page 17
Deployment and Scalability in Node.js
Node.js Page 18
Node.js Page 19
Emerging Trends and Best Practices in Node.js
Node.js Page 20
Node.js Page 21
Performance Optimization in Node.js
Node.js Page 22
Node.js Page 23

MongoDB
[
{ "_id": 1, "name": "John", "age": 25 },
{ "_id": 2, "name": "Jane", "age": 30 },
{ "_id": 3, "name": "Bob", "age": 25 },
{ "_id": 4, "name": "Alice", "age": 35 }
]
To count the number of documents in the “users” collection, you can simply execute the count()
command without any parameters:
db.users.count()
The output will be:
4
Now, let’s say you want to count the number of users who are 25 years old. You can pass a query object to the count()
command to filter the documents based on certain criteria:
db.users.count({ age: 25 })
The output will be:
2
In this case, the count()
command returns the count of documents where the “age” field is equal to 25.
Note that starting from MongoDB version 4.0, the count()
command has been deprecated in favor of the countDocuments()
method. It provides similar functionality but is more versatile and offers additional options.
[
{ "_id": 1, "name": "John", "age": 25 },
{ "_id": 2, "name": "Jane", "age": 30 },
{ "_id": 3, "name": "Bob", "age": 25 },
{ "_id": 4, "name": "Alice", "age": 35 }
]
To retrieve the distinct values of the “age” field from the “users” collection, you can use the distinct()
command as follows:
db.users.distinct("age")
The output will be an array of distinct values:
[25, 30, 35]
In this example, the distinct()
command returns the unique values present in the “age” field of the “users” collection.
You can also specify a query object to filter the documents before retrieving the distinct values. For instance, to get the distinct names of users who are older than 25:
db.users.distinct("name", { age: { $gt: 25 } })
The output will be:
["Jane", "Alice"]
In this case, the distinct()
command returns the distinct names of users whose “age” field is greater than 25.
The distinct()
command can be helpful when you want to obtain unique values for a particular field in your collection, which can be useful for analysis or data exploration purposes.
[
{ "_id": 1, "product": "A", "quantity": 5, "price": 10 },
{ "_id": 2, "product": "B", "quantity": 3, "price": 15 },
{ "_id": 3, "product": "A", "quantity": 2, "price": 12 },
{ "_id": 4, "product": "C", "quantity": 7, "price": 8 },
{ "_id": 5, "product": "B", "quantity": 4, "price": 13 }
]
To calculate the total quantity and revenue for each product, you can use the $group
stage as follows:
db.sales.aggregate([
{
$group: {
_id: "$product",
totalQuantity: { $sum: "$quantity" },
totalRevenue: { $sum: { $multiply: ["$quantity", "$price"] } }
}
}
])
The output will be:
[
{ "_id": "A", "totalQuantity": 7, "totalRevenue": 74 },
{ "_id": "B", "totalQuantity": 7, "totalRevenue": 107 },
{ "_id": "C", "totalQuantity": 7, "totalRevenue": 56 }
]
In this example, the $group
stage groups the documents based on the “product” field and calculates the total quantity and revenue for each product using the $sum
and $multiply
aggregation operators.
The aggregation framework provides a wide range of stages and operators that allow you to perform complex transformations, aggregations, and analyses on your data. It is a powerful tool for data manipulation and analysis in MongoDB.
[
{ "_id": 1, "product": "A", "quantity": 5, "price": 10 },
{ "_id": 2, "product": "B", "quantity": 3, "price": 15 },
{ "_id": 3, "product": "A", "quantity": 2, "price": 12 },
{ "_id": 4, "product": "C", "quantity": 7, "price": 8 },
{ "_id": 5, "product": "B", "quantity": 4, "price": 13 }
]
Let’s use mapReduce()
to calculate the total revenue for each product:
var mapFunction = function() {
emit(this.product, this.quantity * this.price);
};
var reduceFunction = function(key, values) {
return Array.sum(values);
};
db.sales.mapReduce(
mapFunction,
reduceFunction,
{ out: "product_revenue" }
)
In this example, the mapFunction
calculates the revenue for each document by multiplying the “quantity” and “price” fields and emits the product as the key. The reduceFunction
receives the key (product) and the array of revenue values for that product and sums them using Array.sum()
.
The result is stored in a new collection called “product_revenue”. To retrieve the result, you can query the “product_revenue” collection:
db.product_revenue.find()
The output will be:
[
{ "_id": "A", "value": 74 },
{ "_id": "B", "value": 107 },
{ "_id": "C", "value": 56 }
]
MapReduce is a powerful tool for complex data processing and analysis in MongoDB. However, it is important to note that the mapReduce()
command has some limitations, such as slower performance compared to the aggregation framework and requiring JavaScript execution. Therefore, for most scenarios, it is recommended to use the aggregation framework, which provides a more efficient and flexible way to perform data processing and analysis in MongoDB.




Popular Category
Topics for You
Introduction to React.js
React JS Page 1
React JS Page 2
React JS Page 3
Components in React.js
React JS Page 4
React JS Page 5
Virtual DOM in React.js
React JS Page 6
React JS Page 7
State and Props in React.js
React JS Page 8
React JS Page 9
React Router
React JS Page 10
React JS Page 11
React Hooks
React JS Page 12
React JS Page 13
Redux in React.js
React JS Page 14
React JS Page 15
Context API in React.js
React JS Page 16
React JS Page 17
React with Webpack and Babel
React JS Page 18
React JS Page 19
Testing in React.js
React JS Page 20
React JS Page 21
Deployment and Optimization in React.js
React JS Page 22
React JS Page 23
Emerging Trends and Best Practices in React.js
React JS Page 24
React JS Page 25
Introduction
Node.js Page 1
Node.js Page 2
Node.js Architecture and Event-Driven Programming
Node.js Page 3
Node.js Page 4
Modules and Packages in Node.js
Node.js Page 5
Node.js Page 6
File System and Buffers in Node.js
Node.js Page 7
Node.js Page 8
HTTP and Networking in Node.js
Node.js Page 9
Node.js Page 10
Express.js and Web Applications
Node.js Page 11
Node.js Page 12
Databases and ORMs in Node.js
Node.js Page 13
Node.js Page 14
RESTful APIs in Node.js
Node.js Page 15
Node.js Page 16
Testing and Debugging in Node.js
Node.js Page 17
Deployment and Scalability in Node.js
Node.js Page 18
Node.js Page 19
Emerging Trends and Best Practices in Node.js
Node.js Page 20
Node.js Page 21
Performance Optimization in Node.js
Node.js Page 22
Node.js Page 23
Go through our study material. Your Job is awaiting.