In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to aggregate statistical calculations in MongoDB. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
We usually use the expression $sum to calculate the sum. Because MongoDB documents have array fields, you can simply divide the calculation sum into two types:
1. Count the sum of a certain field of all documents that meet the criteria
2, count the sum of each data value in the array field of each document. Both of these cases can be done through the $sum expression.
The aggregate statistics of the above two cases correspond to the $group operation step and the $project operation step in the aggregation framework, respectively.
1.$group
Let's just look at the example.
Case 1
The data in the test collection mycol is as follows:
{title: 'MongoDB Overview', description:' MongoDB is no sql database', by_user: 'runoob.com', url:' http://www.runoob.com', tags: ['mongodb',' database', 'NoSQL'], likes: 100}, {title:' NoSQL Overview', description:'No sql database is very fast', by_user: 'runoob.com', url:' http://www.runoob.com', tags: ['mongodb',' database' 'NoSQL'], likes: 10}, {title:' Neo4j Overview', description: 'Neo4j is no sql database', by_user:' Neo4j', url: 'http://www.neo4j.com', tags: [' neo4j', 'database',' NoSQL'], likes: 750}
Now we use the above collection to calculate the number of articles written by each author, using aggregate ()
Db.mycol.aggregate ([{$group: {_ id: "$by_user", num_tutorial: {$sum: 1}])
The query results are as follows:
/ * 1 * / {"_ id": "Neo4j", "num_tutorial": 1}, / * 2 * / {"_ id": "runoob.com", "num_tutorial": 2}
Case 2
Count the sum of each author's like and calculate the expression:
Db.mycol.aggregate ([{$group: {_ id: "$by_user", num_tutorial: {$sum: "$likes"}])
The query results are as follows
/ * 1 * / {"_ id": "Neo4j", "num_tutorial": 750}, / * 2 * / {"_ id": "runoob.com", "num_tutorial": 110}
Case 3
The above example is a bit simple. Let's enrich it a little bit. The data of the test set sales is as follows:
{"_ id": 1, "item": "abc", "price": 10, "quantity": 2, "date": ISODate ("2014-01-01T08:00:00Z")} {"_ id": 2, "item": "jkl", "price": 20, "quantity": 1, "date": ISODate ("2014-02-03T09:00:00Z")} {"_ id": 3 "item": "xyz", "price": 5, "quantity": 5, "date": ISODate ("2014-02-03T09:05:00Z")} {"_ id": 4, "item": "abc", "price": 10, "quantity": 10, "date": ISODate ("2014-02-15T08:00:00Z")} {"_ id": 5, "item": "xyz" "price": 5, "quantity": 10, "date": ISODate ("2014-02-15T09:05:00Z")}
The goal that needs to be accomplished is to count the daily sales based on the date grouping, and the aggregate formula is:
Db.sales.aggregate ([{$group: {_ id: {day: {$dayOfYear: "$date"}, year: {$year: "$date"}}, totalAmount: {$sum: {$multiply: ["$price", "$quantity"]}}, count: {$sum: 1}}])
The query results are as follows:
{"_ id": {"day": 46, "year": 2014}, "totalAmount": 150," count ": 2} {" _ id ": {" day ": 34," year ": 2014}," totalAmount ": 45," count ": 2} {" _ id ": {" day ": 1," year ": 2014}," totalAmount ": 20," count ": 1}
2.$project stage
Case 4
Suppose there is a students collection with the following data structure:
{"_ id": 1, "quizzes": [10,6,7], "labs": [5,8], "final": 80, "midterm": 75} {"_ id": 2, "quizzes": [9,10], "labs": [8,8], "final": 95, "midterm": 80} {"_ id": 3, "quizzes": [4,5,5], "labs": [6,5] "final": 78, "midterm": 70}
The demand now is to count the sum of each student's usual test scores, the sum of the experiment scores, and the sum of the final scores.
Db.students.aggregate ([{$project: {quizTotal: {$sum: "$quizzes"}, labTotal: {$sum: "$labs"}, examTotal: {$sum: ["$final", "$midterm"]}])
The query output is as follows:
{"_ id": 1, "quizTotal": 23, "labTotal": 13, "examTotal": 155} {"_ id": 2, "quizTotal": 19, "labTotal": 16, "examTotal": 175} {"_ id": 3, "quizTotal": 14, "labTotal": 11, "examTotal": 148} on "how to aggregate statistical calculations in MongoDB" Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.