banner
Xiao4-800K

Xiao4-800K

技术分享、闲聊、玩闹

Overview of MongoDB Aggregation Query#

MongoDB is an efficient document database that can be used to store similar data.

When using the find query to retrieve data, we can concatenate filtering conditions to return data that meets the criteria. However, usually this data cannot be used or passed directly. It needs to be processed by the backend and returned to the frontend application. For example, querying all blog posts of a user is a similar operation. Usually, personal information and blog post information are stored in separate tables. If you want to know the blog posts of a specific user, you need to perform a table join query. If you are familiar with relational databases, you will better understand this operation because this kind of table join query is very common in daily development.

In MongoDB, the Aggregate command is used for aggregation queries. Without adding any parameters, it is equivalent to the find method. Its format is as follows:

collection.aggregate([stage1, stage2, ..., stageN])

The stage instructions include $match for data filtering, $project for field-related operations, $group for grouping, $unwind for data splitting, and $lookup for table joins.

Data Filtering#

db.getCollection('example_user').aggregate([
	{"$match":{
		"name":"Jiang Daniu",
		"id":{"$lt":2}
	}}
])

The parameters of $match are the same as the first parameter of find. You can use field matching or range matching.

Adjusting Fields#

db.getCollection('example_user').aggregate([
	{"$project":{
		"_id":0,						// Hide _id
		"id":"$id",						// Reference custom id
		"Name":"$address",				// Modify field name
		"work":"Writer",				// Append field
		"isLogin":{"$literal":1},		// Default value of 1 or 0
		"hello":{"$literal":"$dollar"}	// Default value with $
	}}
])

Directory

The parameters of $project are similar to the second parameter of find, but with more available operations. You can append or modify existing fields, and you can also append fields of sub-documents. When you need to use '$' or 1, 0 as default values in the result, you can use $literal to annotate them to prevent conflicts with MongoDB syntax.

Data Grouping#

Grouping function

db.getCollection('example_data').aggregate([
	{"$group":{
		"_id":"$name"
	}}
])

Directory

Distinct function

db.getCollection('example_data').distinct("name")

Directory

First, let's compare the distinct function and the grouping function. We can see that the return values have obvious differences. The grouping function returns one row per data, while the distinct function returns the data in an array. If we need to return more fields, such as score information, average value, etc., for statistical purposes, using the grouping information would be more suitable. Let's enrich the grouping information a bit.

db.getCollection('example_data').aggregate([{
    "$group":{
        "_id":"$name",
        "max_score":{"$max":"$score"},		// Get the maximum value
        "min_score":{"$min":"$score"},		// Get the minimum value
        "avg_score":{"$avg":"$score"},		// Get the average value
        "sum_score":{"$sum":"$score"},		// Get the sum
        "first_score":{"$first":"$score"},	// Get the first value
        "last_score":{"$last":"$score"},	// Get the last value
    }
}])

Directory

Splitting Data#

When the data contains arrays, we need to split the data before processing.

db.getCollection('example_tshirt').aggregate([
	{"$unwind":"$size"}
])

Directory

We can also split multiple fields.

db.getCollection('example_tshirt').aggregate([
	{"$unwind":"$size"},
	{"$unwind":"$color"}
])

Joining Collections#

Joining collections is equivalent to table joins in SQL. By integrating data from multiple collections, we can obtain the data we need. The format is as follows:

MainCollection.aggregate([
	{"$lookup":{
		"from":"RelatedCollection",
		"localField":"MainCollectionField",
		"foreignField":"RelatedCollectionField",
		"as":"RelatedCollectionAlias"
	}}
])

For example, if we want to query which blog posts each user has written, we can write it in the following format:

Directory

Of course, this data format is not enough for us. We can optimize the data.

Directory

Additional Information#

After dealing with MongoDB, we definitely need to use it. In Python, there is a library called pymongo, which can almost directly use MongoDB commands, reducing our learning costs. It's really powerful.

More content click here

Directory

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.