In addition to basic query functions, MongoDB also provides powerful aggregation functions. Here we mainly introduce count, distinct, and group.
1. Count:
-- In an empty set, Count returns 0.
> DB. Test. Count ()
0
-- Test the return value of count after inserting a document.
> DB. Test. insert ({"test": 1 })
> DB. Test. Count ()
1
> DB. Test. insert ({"test": 2 })
> DB. Test. Count ()
2
-- Count, like find, also accepts conditions. The results show that only qualified documents are involved in the calculation.
> DB. Test. Count ({"test": 1 })
1
2. distinct:
Distinct is used to find all the different values of a given key. The Set and key must also be specified during use.
-- To facilitate subsequent tests, empty the test set first.
> DB. Test. Remove ()
> DB. Test. Count ()
0
-- Insert four test data records. Pay attention to the age field.
> DB. Test. insert ({"name": "ADA", "Age": 20 })
> DB. Test. insert ({"name": "Fred", "Age": 35 })
> DB. Test. insert ({"name": "Andy", "Age": 35 })
> DB. Test. insert ({"name": "Susan", "Age": 60 })
The -- distinct command must specify the set name, such as test, and fields to be distinguished, such as age.
-- The following command executes the distinct command based on the age field in the test set.
> DB. runcommand ({"distinct": "test", "key": "Age "})
{
"Values ":[
20,
35,
60
],
"Stats ":{
"N": 4,
"Nscanned": 4,
"Nscannedobjects": 4,
"Timems": 0,
"Cursor": "basiccursor"
},
"OK": 1
}
3. GROUP:
Group aggregation is complicated. Select the keys for the group. MongoDB then divides the set into several groups based on different key values. Then, you can aggregate the documents in each group to generate a result document.
-- The test data is prepared.
> DB. Test. Remove ()
> DB. Test. insert ({"day": "2012-08-20", "Time": "2012-08-20 03:20:40", "price": 4.23 })
> DB. Test. insert ({"day": "2012-08-21", "Time": "2012-08-21 11:28:00", "price": 4.27 })
> DB. Test. insert ({"day": "2012-08-20", "Time": "2012-08-20 05:00:00", "price": 4.10 })
> DB. Test. insert ({"day": "2012-08-22", "Time": "2012-08-22 05:26:00", "price": 4.30 })
> DB. Test. insert ({"day": "2012-08-21", "Time": "2012-08-21 08:34:00", "price": 4.01 })
-- Here, Day is used as the group key, and the document whose time key value is the latest timestamp is also taken out.
> DB. Test. Group ({
... "Key": {"day": true }, -- For multiple fields, it can be {"f1": True, "F2": true}
... "Initial": {"time": "0 "}, -- Initial indicates the initial value of the $ reduce function parameter Prev. Each group has an initial value.
... "$ Reduce": function (Doc, Prev ){ -- The reduce function accepts two parameters. Doc indicates the current document being iterated, and Prev indicates the document of the accumulators.
... If (Doc. Time> Prev. Time ){
... Prev. Day = Doc. Day
... Prev. Price = Doc. price;
... Prev. Time = Doc. time;
...}
...}})
[
{
"Day": "2012-08-20 ",
"Time": "05:00:00 ",
"Price": 4.1
},
{
"Day": "2012-08-21 ",
"Time": "11:28:00 ",
"Price": 4.27
},
{
"Day": "2012-08-22 ",
"Time": "05:26:00 ",
"Price": 4.3
}
]
-- The following example is to count the number of documents in each group.
> DB. Test. Group ({
... Key: {day: true },
... Initial: {count: 0 },
... Reduce: function (OBJ, Prev) {Prev. Count ++ ;},
...})
[
{
"Day": "2012-08-20 ",
"Count": 2
},
{
"Day": "2012-08-21 ",
"Count": 2
},
{
"Day": "2012-08-22 ",
"Count": 1
}
]
-- The last one is an example of modifying the reduce result through the worker.
> DB. Test. Group ({
... Key: {day: true },
... Initial: {count: 0 },
... Reduce: function (OBJ, Prev) {Prev. Count ++ ;},
... Finalize: function (out) {out. scaledcount = out. Count * 10} -- Add a new key in the result document.
...})
[
{
"Day": "2012-08-20 ",
"Count": 2,
"Scaledcount": 20
},
{
"Day": "2012-08-21 ",
"Count": 2,
"Scaledcount": 20
},
{
"Day": "2012-08-22 ",
"Count": 1,
"Scaledcount": 10
}
]