In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Mongodb implements the summation of array objects? Many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can gain something.
/ * 1 * / {"_ id": ObjectId ("5c414a6a0847e00385143003"), "date": "2019-01-18 09", "data": [{"app_platform": "ios", "user": 3028}, {"app_platform": "android", "user": 4472},]}.
Now we need to calculate the total number of user whose date date is "2019-01-18 09" and the type of app_platform is "ios"
If you can, think about how to implement the mongodb statement first.
There is a very important implementation in the implementation process, that is, $unwind, which is officially explained:
Deconstructs an array field from the input documents to output a document for each element. Each output document is the input document with the value of the array field replaced by the element.
Deconstruct an array field from the input document and output a document for each element. Each output document is an input document, and the value of the array field is replaced by an element.
So we came up with the idea of breaking up data array objects and simplifying them. The mongodb statement is as follows:
Db.getCollection ('user') .aggregate ([{$project: {_ id: 1, data: 1, date: 1}}, {$match: {"date": "2019-01-18 09"}}, {$unwind: "$data"},])
The results are as follows:
/ * 1 * /
{
"_ id": ObjectId ("5c414a6a0847e00385143003")
"date": "2019-01-18 09"
"data": {
"app_platform": "ios"
"user": 3028
}
}
/ * 2 * /
{
"_ id": ObjectId ("5c414a6a0847e00385143003")
"date": "2019-01-18 09"
"data": {
"app_platform": "android"
"user": 4472
}
}
You can see that the data has changed from an array to multiple document data, so the question is changed into the total number of user of the result. Do you think the problem has become easier, and we can continue to use $match to filter app_platform data, as shown in the mongodb statement:
Db.getCollection ('user') .aggregate ([{$project: {_ id: 1, data: 1, date: 1}}, {$match: {"date": "2019-01-18 09"}}, {$unwind: "$data"}, {$match: {$in: ["ios"]}},}])
The implementation results are as follows:
/ * 1 * /
{
"_ id": ObjectId ("5c414a6a0847e00385143003")
"date": "2019-01-18 09"
"data": {
"app_platform": "ios"
"user": 3028
}
}
You can see that the data has been filtered. If you confidently observe the role of the two $match, you can find that the mongodb is executed sequentially, that is, $match acts on the set of operation results in front of it.
Let's continue with the calculation. At this point, we only need to use group and sum to sum the user fields in data. The mongodb statement is as follows:
Db.getCollection ('user') .aggregate ([{$project: {_ id: 1, data: 1, date: 1}}, {$match: {"date": "2019-01-18 09"}}, {$unwind: "$data"}, {$match: {$in: ["ios"]}}}, {$group: {_ id: null "user": {$sum: "$data.user"}])
The results are as follows:
/ * 1 * /
{
"_ id": null
"user": 7500
}
The calculated user is the data we need.
In fact, all the difficulties are as follows:
When calculating array object data, convert it to several simple data formats, and the $unwind instruction makes the problem easier.
The execution order of mongodb and $project,$match are all sequentially executed and acted on the results of previous operations.
After understanding these two points, I believe that no matter how difficult the mongodb sentence is, you can achieve it.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.