In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
In this issue, the editor will bring you about the aggregation commands in MongoDB. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
1. Polymerization pipeline method:
The pipeline aggregation method can be understood as the aggregate pipeline method, which classifies and statistics the values corresponding to the keys of several numerical documents in the set, which is somewhat similar to group by in SQL language.
The syntax is as follows:
Db.collection.agrregate (
[$match: {}}
{$group: {,}}
]
Description:
Field1 is a classified field; field2 is a numeric field with various statistical operators, such as $sum, $avg, $min,$max, etc.
> use test
> db.test.insert (
... [{id: "001", amount:2,price:15.2,ok:true}
... {id: "001", amount:3,price:14.8,ok:true}
... {id: "002", amount:4,price:40,ok:true}
... {id: "002", amount:2,price:10,ok:true}
... {id: "003", amount:3,price:20.3,ok:true}
...]
.)
BulkWriteResult ({
"writeErrors": []
"writeConcernErrors": []
"nInserted": 5
"nUpserted": 0
"nMatched": 0
"nModified": 0
"nRemoved": 0
"upserted": []
})
> db.test.aggregate ({$match: {ok:true}})
{"_ id": ObjectId ("5b50388dff7043cec86841af"), "id": "001", "amount": 2, "price": 15.2, "ok": true}
{"_ id": ObjectId ("5b50388dff7043cec86841b0"), "id": "001", "amount": 3, "price": 14.8, "ok": true}
{"_ id": ObjectId ("5b50388dff7043cec86841b1"), "id": "002", "amount": 4, "price": 40, "ok": true}
{"_ id": ObjectId ("5b50388dff7043cec86841b2"), "id": "002", "amount": 2, "price": 10, "ok": true}
{"_ id": ObjectId ("5b50388dff7043cec86841b3"), "id": "003", "amount": 3, "price": 20.3, "ok": true}
> db.test.aggregate (
... {
.. $group: {
... _ id:'$id'
... Total: {$sum: "$amount"}
...}
.)
{"_ id": "003", "total": 6}
{"_ id": "002", "total": 12}
{"_ id": "001", "total": 10}
>
Note: _ id:'$id',id is the category field name, total is the statistical result field name, $sum is the summation operation symbol, and $amount is the summation field.
2.map-reduce method:
> var chenfeng=db.test.mapReduce (
... Function () {
... Emit (this.id,this.amount)
...}
... Function (key,values) {
... Return Array.sum (values)
...}
... {query: {ok:true}, out: {replace: "result"}}
.)
> db[ chenfeng.result] .find ()
{"_ id": "001", "value": 5}
{"_ id": "002", "value": 6}
{"_ id": "003", "value": 3}
>
3. Single objective aggregation method:
Syntax:
Db.collection.count (query,options)
For example:
> db.test.distinct ("id")
["001", "002", "003"]
>
> db.test.find ({ok:true}) .count ()
five
These are the aggregate commands in the MongoDB shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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
CREATE OR REPLACE PACKAGE KMP_ALGOR_PKGASTYPE next_arr_tt IS TABLE OF INT INDEX BY PLS_INTEGER;PROCE
© 2024 shulou.com SLNews company. All rights reserved.