MongoDB Study Notes (aggregation)

Source: Internet
Author: User

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
}
]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.