Introduction to MongoDB collection.aggregate() Method
MongoDB is a NoSQL database that stores data in the form of documents and provides many methods for data analysis and aggregation, including the aggregate()
method. This method can be used to perform aggregation operations on a MongoDB collection and supports SQL-like aggregation operations such as group
, sort
, project
, etc.
Syntax
The syntax of the aggregate()
method is as follows:
db.collection.aggregate( [ { <stage> }, ... ] )
Here, db.collection
is the name of the collection on which the aggregation operation is to be performed, and <stage>
is the aggregation stage, which can be one or more stages executed in sequence.
Use Cases
The aggregate()
method is typically used in the following scenarios:
- Data analysis and report generation: By using aggregation operations, data in a MongoDB collection can be analyzed and various reports can be generated.
- Data cleaning and transformation: By using aggregation operations, data in a MongoDB collection can be cleaned and transformed for subsequent analysis.
- Data mining and modeling: By using aggregation operations, specific data patterns and rules can be extracted from a MongoDB collection for data mining and modeling.
Examples
Below are two complete examples of using the aggregate()
method.
Example 1: Count the number of users and average age in each city
Assuming there is a user collection named users
where each user document contains the following fields: _id
, name
, age
, and city
. We need to count the number of users and their average age in each city. This can be achieved using the following code:
db.users.aggregate([
{
$group: {
_id: "$city",
total_users: { $sum: 1 },
avg_age: { $avg: "$age" }
}
},
{ $sort: { total_users: -1 } }
])
This code performs aggregation operations on the users
collection. It first uses the $group
stage to group users by city, and then uses the $sum
and $avg
aggregation operations to count the total number of users and their average age, respectively. Finally, it uses the $sort
stage to sort the result by total users in descending order. Assuming the data in the users
collection is as follows:
{ _id: 1, name: "Alice", age: 25, city: "Beijing" }
{ _id: 2, name: "Bob", age: 30, city: "Shanghai" }
{ _id: 3, name: "Charlie", age: 35, city: "Beijing" }
{ _id: 4, name: "David", age: 40, city: "Shenzhen" }
{ _id: 5, name: "Emily", age: 25, city: "Beijing" }
Then, the result of the aggregation operation is as follows:
{ "_id" : "Beijing", "total_users" : 3, "avg_age" : 28.333333333333332 }
{ "_id" : "Shanghai", "total_users" : 1, "avg_age" : 30 }
{ "_id" : "Shenzhen", "total_users" :1, "avg_age" : 40 }
This result indicates that there are three users in Beijing with an average age of about 28.33 years, one user in Shanghai with an average age of 30 years, and one user in Shenzhen with an average age of 40 years.
Conclusion
The MongoDB aggregate()
method can be used to perform aggregation operations on MongoDB collections, supporting aggregation operations in SQL such as group
, sort
, project
, and so on.