In MongoDB, the aggregate
operation is used to process data records and return computed results. This command allows you to perform complex data aggregation tasks, such as filtering, grouping, and sorting, within MongoDB collections.
db.collection.aggregate(pipeline, options)
The aggregate
method uses a pipeline
of stages to process documents and return aggregated results. The options
parameter can be used to customize the aggregation.
Let's look at some examples of how to use the aggregate
method in the programGuru
collection in MongoDB:
db.programGuru.aggregate([
{ $match: { age: { $gt: 25 } } },
{ $group: { _id: "$age", count: { $sum: 1 } } },
{ $sort: { _id: 1 } }
])
This command filters documents where the age
is greater than 25, groups them by age
, and sorts the results by age
in ascending order.
db.programGuru.aggregate([
{ $match: { age: { $gt: 25 } } },
{ $project: { name: 1, age: 1, _id: 0 } }
])
This command filters documents where the age
is greater than 25 and projects only the name
and age
fields.
db.programGuru.aggregate([
{ $match: { age: { $gt: 25 } } },
{ $group: { _id: "$age", averageAge: { $avg: "$age" } } },
{ $sort: { averageAge: 1 } }
])
This command filters documents where the age
is greater than 25, groups them by age
, calculates the average age
, and sorts the results by the average age
in ascending order.
Let's go through a complete example that includes switching to a database, creating a collection, inserting documents, and using the aggregate
method.
This step involves switching to a database named myDatabase
.
use myDatabase
In this example, we switch to the myDatabase
database.
This step involves creating a new collection named programGuru
in the myDatabase
database.
db.createCollection("programGuru")
Here, we create a collection named programGuru
.
This step involves inserting documents into the programGuru
collection.
db.programGuru.insertMany([
{ name: "John Doe", age: 30, email: "john.doe@example.com" },
{ name: "Jane Smith", age: 25, email: "jane.smith@example.com" },
{ name: "Jim Brown", age: 35, email: "jim.brown@example.com" }
])
We insert multiple documents into the programGuru
collection.
This step involves using the aggregate
method to process documents in the programGuru
collection.
db.programGuru.aggregate([
{ $match: { age: { $gt: 25 } } },
{ $group: { _id: "$age", count: { $sum: 1 } } },
{ $sort: { _id: 1 } }
]).pretty()
We filter documents where the age
is greater than 25, group them by age
, and sort the results by age
in ascending order.
db.programGuru.aggregate([
{ $match: { age: { $gt: 25 } } },
{ $project: { name: 1, age: 1, _id: 0 } }
]).pretty()
We filter documents where the age
is greater than 25 and project only the name
and age
fields.
db.programGuru.aggregate([
{ $match: { age: { $gt: 25 } } },
{ $group: { _id: "$age", averageAge: { $avg: "$age" } } },
{ $sort: { averageAge: 1 } }
]).pretty()
We filter documents where the age
is greater than 25, group them by age
, calculate the average age
, and sort the results by the average age
in ascending order.
The MongoDB aggregate
operation is crucial for performing complex data processing tasks. Understanding how to use the aggregation framework allows you to efficiently filter, group, and transform data within MongoDB collections.