MongoDB Database features powerful! In addition to the basic query functionality, it provides powerful aggregation capabilities. Here's a quick introduction to count, distinct, and group.
1.count:
--In an empty collection, Count returns a number of 0.
> Db.test.count ()
0
--tests 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 eligible documents are involved in the calculation.
> Db.test.count ({"Test": 1})
1
2.distinct:
distinct is used to find all the different values for a given key. You must also specify collections and keys when you use them.
--to facilitate subsequent testing, empty the test collection.
> Db.test.remove ()
> Db.test.count ()
0
--insert 4 test data. Please note the age field.
> Db.test.insert ({"Name": "Ada", "Age":]
> Db.test.insert ({"Name": "Fred", "Age":)
> Db.test.insert ({"Name": "Andy", "Age":})
> Db.test.insert ({"Name": "Susan", "Age":)
-- The distinct command must specify the collection name, such as test, and the fields you want to differentiate, 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": [A,
4,
],
"stats": {"
n": "4", "
nscanned":
"," Nscannedobjects ": 4,
" Timems ": 0,
" cursor ":" Basiccursor "
},
" OK ": 1
}
3.group:
the aggregation made by group is somewhat complex. Select the key on which the group is based, and then mongodb the collection according to the selected key values to several groups. You can then produce a result document by aggregating the documents within each group.
--Here is the test data > 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.ins ERT ({' 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 grouping key for group, and then remove 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 more than one field, can be {' 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 functions accept two parameters, doc represents the current document being iterated, 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-2
2 05:26:00 "," Price ": 4.3}]--The following example is to count the number of documents in each packet. > 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 reduce results through the 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 resulting document. ... } [{' Day ': ' 2012-08-20 ', ' Count ': 2, ' Scaledcount ':}, {' Day ': 2012-08-21
", ' Count ': 2, ' scaledcount ': ' {' Day ': ' 2012-08-22 ', ' Count ': 1, ' Scaledcount ": 10}]