MongoDB QuickStart (13-aggregates count, distinct, and group)

Source: Internet
Author: User
Tags prev

1. Count:

--In an empty collection, Count returns a quantity of 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 and find also accept the conditions. As you can see from the results, only qualified documents participate in the calculation.
> Db.test.count ({"Test": 1})
1


2. Distinct:

Distinct is used to find out all the different values for a given key. You must also specify the collection and key when you use it.

-In order to facilitate subsequent tests, empty the test set first.

> Db.test.remove ()
> Db.test.count ()
0
--Insert 4 test data. Be aware of 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 collection name, such as test, and the fields that need to be distinguished, such as: Age.
--The following command executes the distinct command based on the age field in the Test collection.
> 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 somewhat complex. The key that the group is based on is selected, and MongoDB then divides the collection into groups based on the selected key value. You can then generate a result document by aggregating the documents within each group.

--Here is the test data to be 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})
--This will use day as the group key for the group, and then take out the document with the time key value as the latest timestamp, and also remove the price key value for the document.
> Db.test.group ({
... "Key": {"Day": true},--if it is multiple fields, {"F1": True, "F2": true}
... "Initial": {"time": "0"},--initial represents the initial value of the $reduce function parameter prev. Each group has a copy of the initial value.
... "$reduce": function (doc,prev) {--reduce function accepts two parameters, doc represents the current document being iterated, and Prev represents an accumulator document.
... if (Doc.time > Prev.time) {
... prev.day = Doc.day
... prev.price = Doc.price;
... prev.time = doc.time;
...     }
... } } )
[
{
"Day": "2012-08-20",
"Time": "2012-08-20 05:00:00",
"Price": 4.1
},
{
"Day": "2012-08-21",
"Time": "2012-08-21 11:28:00",
"Price": 4.27
},
{
"Day": "2012-08-22",
"Time": "2012-08-22 05:26:00",
"Price": 4.3
}
]
--The following example is to count the number of documents within each grouping.
> 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 how to modify the reduce result with a completion device.
> Db.test.group ({
... key: {day:true},
... initial: {count:0},
... reduce:function (obj,prev) {prev.count++;},
... finalize:function (out) {out.scaledcount = Out.count * 10}--A new key is added to 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
}
]

Parameter explanation:

key:group FieldReduce: Aggregate functions, typically with sum and count operations, contain two parameters, current traversal object, and aggregate counter initial: Initialize counter value group cannot be used in sharding, result output cannot be more than 10,000 keys

MongoDB QuickStart (13-aggregates count, distinct, and group)

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.