MongoDB aggregation function analysis

  • 2020-05-09 19:32:22
  • OfStack

MongoDB database powerful! In addition to the basic query capabilities, it provides powerful aggregation capabilities. Here is a brief introduction to count, distinct, and group.

1. count:
       


-- In the empty set, count The number returned is 0 . 
  > db.test.count()
  0
  -- Test insert 1 After a document count The return value of. 
  > db.test.insert({"test":1})
  > db.test.count()
  1
  > db.test.insert({"test":2})
  > db.test.count()
  2
  --count and find1 If so, accept the terms. As you can see from the results, only qualified documents were 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. 
  > db.test.remove()
  > db.test.count()
  0
  -- insert 4 Test data. Please pay attention to 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 The command must specify a collection name, such as test , and the fields to be distinguished, such as: age . 
  -- The following command will be based on test In the collection age Fields to perform distinct Command. 
  > 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 that       group does is a little bit more complicated. After selecting the key on which to group, MongoDB divides the collection into groups based on the selected key values. You can then aggregate the documents in each group to produce one 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 will be used day As a group Group key, and then pull out time The document whose key value is the latest timestamp is also retrieved price The key value. 
  > db.test.group( {
  ... "key" : {"day":true},      -- If multiple fields, can be {"f1":true,"f2":true}
  ... "initial" : {"time" : "0"},    --initial said $reduce Function parameters prev Initial value of. Each group has one 1 Copy the initial value. 
  ... "$reduce" : function(doc,prev) { --reduce The 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 1 One is modified by the finisher reduce Examples of results. 
  > db.test.group( {
  ... key: { day: true},
  ... initial: {count: 0},
  ... reduce: function(obj,prev){ prev.count++;},
  ... finalize: function(out){ out.scaledCount = out.count * 10 } -- New in the results document 1 A key. 
  ... } )
  [
    {
      "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 articles: