@font-face {font-family: "Times New Roman";} @font-face {font-family: "Song Body";} P.msonormal {margin:0pt 0pt 0.0001pt; text-align:justify; font-family: "Times New Roman"; font-size:10.5pt;} P.P {margin:5pt 0pt; text-align:left; font-family: "Times New Roman"; font-size:12pt;} span.msoins {text-decoration:underline; color:blue;} Span.msodel {text-decoration:line-through; color:red;} Div. Section0 {}
Mongodb provides a powerful aggregation tool in addition to the basic query functionality. This chapter looks at how to use aggregations for querying
Count
Count is the most basic aggregation tool that returns the number of documents in the collection:
> Db.student_infor.count ()
10
You can also set criteria for querying
> Db.student_infor.count ({"Age": {$gt: 19}})
6
Distinct
Distinct is used to find out all the different values of the key, and you must specify the set and key when you use it .
Distinct corresponds to the set name, key corresponding to the keys. The output is All the values of age in student_infor
> Db.runcommand ({"distinct": "Student_infor", "Key": "Age"})
{"Values": [+, +, +, +], "OK": 1}
Group
The Group takes approximately a few parameters.
1.key: The field used to group the document. and theKeyfboth must have a
2.keyf: You can accept aJavaScriptfunction. The field used to dynamically determine the grouped document. and theKeyboth must have a
3.initial:Reduceinitialization of using variables in
4.reduce: Execution ofReducefunction. The function needs to return a value.
5.cond: The criteria for performing the filter.
6.finallize: InReduceexecution completes, and the result set returns the function that was last executed on the result set. Optional.
Take a look at a specific example:
>db.student_infor.group ({"key": {age:true}, "initial": {num:0}, $reduce: function (Doc,prev) {prev.num++}})
Key:age represents using age to do the grouping. Specifies true so that the same age will be divided into groups.
Initial is an initial value specified and will be used in reduce
Reduce: Each document corresponds to this call, and the system passes two parameters: the current document and the accumulator document. Doc represents the document that is currently being iterated, andprev is an accumulator document that is used to accumulate various statistics. num used in Prev is the initial value in intial .
The above results are as follows: statistics on the number of students per age
[
{
"Age": 21,
"Num": 2
},
{
"Age": 19,
"Num": 1
},
{
"Age": 20,
"Num": 2
},
{
"Age": 22,
"Num": 2
},
{
"Age": 18,
"Num": 3
}
]
For queries we can also use conditional judgment to filter by using the condtion
> Db.student_infor.group ({"key": {age:true}, "initial": {num:0}, $reduce: function (Doc,prev) {prev.num++}, condition:{"Age": {$gt: 20}})
Get The group query case with age greater than .
[{"Age": $, "num": 2}, {"Age": $, "num": 2}]
There is another way to do this:
> Db.runcommand ({group:{ns: "Student_infor", key:{age:true},initial:{num:0}, $reduce: function (Doc,prev) { prev.num++}})
An ns parameter is added here followed by the name of the collection.
Aggregate
MongoDB aggregation (aggregate) is primarily used to process data ( such as statistical averages , sums, etc. ) , and returns the computed data result. A bit like the count (*) in the SQL statement.
The following table shows some of the aggregated expressions :
An expression |
Describe |
Instance |
$sum |
Calculates the sum. |
Db.mycol.aggregate ([{$group: {_id: "$by _user", num_tutorial: {$sum: "$likes"}}]) |
$avg |
Calculate average |
Db.mycol.aggregate ([{$group: {_id: "$by _user", num_tutorial: {$avg: "$likes"}}]) |
$min |
Gets the minimum value that corresponds to all documents in the collection. |
Db.mycol.aggregate ([{$group: {_id: "$by _user", num_tutorial: {$min: "$likes"}}]) |
$max |
Gets the maximum value that corresponds to all documents in the collection. |
Db.mycol.aggregate ([{$group: {_id: "$by _user", num_tutorial: {$max: "$likes"}}]) |
$push |
Inserts a value into an array in the resulting document. |
Db.mycol.aggregate ([{$group: {_id: "$by _user", url: {$push: "$url"}}]) |
$addToSet |
Inserts a value into an array in the resulting document, but does not create a copy. |
Db.mycol.aggregate ([{$group: {_id: "$by _user", url: {$addToSet: "$url"}}]) |
$first |
Gets the first document data based on the ordering of the resource documents. |
Db.mycol.aggregate ([{$group: {_id: "$by _user", First_url: {$first: "$url"}}]) |
$last |
Get the last document data based on the ordering of the resource documents |
Db.mycol.aggregate ([{$group: {_id: "$by _user", Last_url: {$last: "$url"}}]) |
Consider a specific example: Group represents a group,theID represents the basis for grouping , and average is a newly generated field. $avg is the average function,the"$age" represents the average
>db.student_infor.aggregate ({$group: {_id: "student_id", average:{$avg: "$age"}})
{"_id": "student_id", "average": 19.9}
Find the oldest
>db.student_infor.aggregate ({$group: {_id: "Age", average:{$max: "$age"}})
{"_id": "Age", "average": 22}
Find the Little
> db.student_infor.aggregate ({$group: {_id: "Age", average:{$min: "$age"}})
{"_id": "Age", "average": 18}
Insert The age field into the array
> db.student_infor.aggregate ({$group: {_id: "Age", average:{$push: "$age"}})
{"_id": "Age", "average": [21, 19, 20, 22, 18, 18, 22, 21, 20, 18]}
sum the age
> db.student_infor.aggregate ({$group: {_id: "Age", average:{$sum: "$age"}})
{"_id": "Age", "average": 199}
MongoDB Learning: Aggregation