Home>

1. count:

-In the empty collection,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.
As can be seen 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.Collections and keys must also be specified when used.

-To facilitate later testing,Empty the test set first.
 >db.test.remove ()
 >db.test.count ()
 0
 -Insert 4 test data.
Note 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})
 --distinct command must specify the collection name,Such as test, and the fields that need to be distinguished,For example:age.
 --The following command will execute 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:

The aggregation made by group is a bit complicated.First select the key on which the grouping is based.Afterwards mongodb will divide the collection into groups based on the selected key value.You can then aggregate the documents within each group,Generate a result document.

--Here is the prepared 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.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 we will use day as the grouping key for group,Then take out the document whose time key value is the latest timestamp,At the same time, the price key value of the document is also taken out.
 >db.test.group ({
 ... "key":{"day":true}, --if multiple fields,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 this initial value.
 ... "$reduce":function (doc, prev) {--reduce function takes two arguments,doc represents the current document being iterated,prev represents the 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 counts 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 by the finisher.
 >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 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
 }
 ]
</p>
<p>
  • Previous MongoDB tutorial query operation example
  • Next JS method to achieve random random color ball effects