(14) mongodb aggregate frame aggregation

Compared with the group, aggregate built many methods commonly used are as follows:

  $ match the equivalent relational database the WHERE
  $ Group equivalent to a relational database by Group
  $ Project is equivalent to a relational database in the SELECT
  $ equivalent to the Sort relational database by the Order
  $ limit is equivalent to a relational database the limit
  $ sum equivalent to the sum relational database
  $ sum equivalent to the relational database count

db.collection.aggregate (document) This is a usage, document is a json array ado, directly on the case, data spend data in an essay.

1, how many commodity goods under inquiry

  [

    {$group:{_id:null,total:{$sum:1}}}

  ]

  id is null, the packet is actually not, can easily write the _id also a constant, such as write: [{$ group: {_ id: 'cat_id', total: {$ sum: 1}}}] 

2, the number of items under each query column

  [

    {$group:{_id:'$cat_id',total:{$sum:1}}}

  ]  

  For cat_id group, total any name, $ sum: 1, for a sum, the actual number of rows of statistics

3, each inquiry under section price is greater than the number of commodities $ 50

  [
    {$match:{shop_price:{$gt:50}}},
    {$group:{_id:'$cat_id',total:{$sum:1}}}
  ]

4, each of the query column number of the item price is greater than $ 50, and selected "commodity number satisfying the condition of" greater than or equal to 3 column

  [
    {$match:{shop_price:{$gt:50}}},
    {$group:{_id:'$cat_id',total:{$sum:1}}},
    {$match:{total:{$gte:3}}}
  ]

5, to check stock in each section

  [

    {$group:{_id:'$cat_id',total:{$sum:'$goods_number'}}}
  ]

6, to check stock in each section, and sorted inventory

  [

    {$group:{_id:'$cat_id',total:{$sum:'$goods_number'}}},
    {$sort:{total:1}}
  ]

7, to check stock in each section, and sorted inventory, take the high number of top 3

  [
    {$group:{_id:'$cat_id',total:{$sum:'$goods_number'}}},
    {$sort:{total:-1}},
    {$limit:3}
  ]

8, query each column average commodity prices and average case price high to low Sort

  [
    {$group:{_id:'$cat_id',avg:{$avg:'$shop_price'}}},
    {$sort:{avg:-1}}
  ]

After the query output styles are the same, only (8), for example, as follows:

{
    "result" : [
        {
            "_id" : 5,
            "avg" : 3700
        },
        {
            "_id" : 4,
            "avg" : 2297
        },
        {
            "_id" : 3,
            "avg" : 1746.0666666666666
        },
        {
            "_id" : 2,
            "avg" : 823.33
        },
        {
            "_id" : 8,
            "avg" : 75.33333333333333
        },
        {
            "_id" : 15,
            "avg" : 70
        },
        {
            "_id" : 14,
            "avg" : 54
        },
        {
            "_id" : 13,
            "avg" : 33.5
        },
        {
            "_id" : 11,
            "avg" : 31
        }
    ],
    "ok" : 1
}
View Code

The output is json, a Result, a ok; Result in json array, store the result, the value 1 is ok

Guess you like

Origin www.cnblogs.com/javasl/p/11331879.html