mongodb command (b)

mongodb command (b)
polymerizing the conduit
polymerization aggregate concept

  • Polymerization (Aggregate) Polymer pipes are based on data processing, each document through a conduit by a plurality of stages (Stage) composition, can be grouped, filtering, each stage of the pipeline, and then through a series of processing, the output the corresponding results.
  • Syntax:. Db collection name .aggregate ({pipeline: Expression {}})


Frequently used pipe

  • In mongodb, the file after the file has been processed, the intake pipe by ⾏ treatments ⼀
  • Common duct follows:

 

  • $ Group: grouping the set of documentation that can be Use the statistical results
  • $ Match: data filtering, output only qualified documentation that
  • $ Project: Review Entering text file structures, such as renaming, add, delete field, create a calculated result
  • $ Sort: a sort file after Entering text output
  • $ Limit: limit of polymerization documentation that return conduit
  • $ Skip: skip a specified number of Documents, and return the rest of the documentation that
  • $ Unwind: the array type field split into ⾏


Common expression (data aggregate function in a similar relationship)

  • Entering text file processing and outputs
  • Syntax: expression: '$ Column Name'
  • Frequently used expressions:

 

  • $ Sum: calculating the sum, $ sum: 1 represents the count it at an times
  • $ Avg: the average calculated
  • $ Min: get the minimum values ​​for
  • $ Max: get the most value zoomed
  • $ Push: Insert the value document are the results of the array in ⼀
  • $ First: Get the first previous file data file according to the documentation that sort of resources
  • $ Last: get the last file file data according to the documentation that sort of resources


Format:   . DB set .aggregate ({conduit 1: {}}, {conduit 2: {}}) 3 Pipe command of $ group 3.1 are grouped by a field $ group all commands polymerization with up to one th command packet is used to set a document, can be used for statistics using the following exemplary db.stu.aggregate ({$ group: {_id : "$ gender", counter: {$ sum: 1}}}) wherein Precautions :
  





  • db.db_name.aggregate grammar, all piping commands require written therein
  • _id expressed in terms of packets, which are grouped by field, use this field to select $ gender representation group
  • $ Sum: 1 1 as represented by the each of the data statistics, statistics is the number of the next data packet


3.2 group by null
when we need to count the entire document when another use $ group is to put the entire document is divided into a set of statistics
using the examples are as follows:
db.stu.aggregate ({$ group: {_id: null, counter : {$ sum: 1}} })
wherein Precautions:

  • _id: null field indicating packet does not specify that the entire document statistics, acquired at this time counter indicates the number of the entire document


3.3 Pivot
data gender statistical normal conditions, when the need to know the name of all, the need to observe one by one, if some way to put together all the name, then the data at this time can be understood as a perspective view
the use of the following examples :

  • Statistics different gender students


db.stu.aggregate(     {$group:         {            _id:"$gender",             name:{$push:"$name"}         }     } )

  • Use $$ ROOT entire document can be placed in an array


db.stu.aggregate(     {$group:         {             _id:null,             name:{$push:"$$ROOT"}         }     } )
3.4 动手
对于如下数据,需要统计出每个country/province下的userid的数量(同一个userid只统计一次)
{ "country" : "china", "province" : "sh", "userid" : "a" }  {  "country" : "china", "province" : "sh", "userid" : "b" }  {  "country" : "china", "province" : "sh", "userid" : "a" }  {  "country" : "china", "province" : "sh", "userid" : "c" }  {  "country" : "china", "province" : "bj", "userid" : "da" }  {  "country" : "china", "province" : "bj", "userid" : "fa" }
参考答案
db.tv3.aggregate(  {$group:{_id:{country:'$country',province:'$province',userid:'$userid'}}},  {$group:{_id:{country:'$_id.country',province:'$_id.province'},count:{$sum:1}}}
4 管道命令之$match
$ match for filtering data is a command that can be used in the polymerization operation, the difference is $ match and find the results to the next operation may be a pipe handling, while not find
use example is as follows:

  • Queries older than 20 students


db.stu.aggregate(     {$match:{age:{$gt:20}}     )

  • The number of male and female students older than 20 queries


db.stu.aggregate ({$ match: {Age: {$ gt: $ 20 is {Group}}: {_ ID: "$ Gender", counter: $ SUM {:}}}. 1)
. 5 of conduit command $ Project
$ input output structure, project to modify a document, such as renaming, add, delete fields
using the example below:

  • Query student's age, name, age, name only output


db.stu.aggregate(     {$project:{_id:0,name:1,age:1}}     )

  • Queries boys and girls life, the number of output


db.stu.aggregate ({$ group: {_ id: "$ gender", counter: {$ sum: 1}}} {$ project: {_ id: 0, counter: 1}})
hands-on experience
for the following data: Statistics userid of the number (counted only once with a userid) under each country / province, results Country field {: " ", Province: " ", counter: "*"}
{ "Country": "China" , "province": "sh" , "userid": "a"} { "country": "china", "province": "sh", "userid": "b"} { "country": "china" , "province": "sh" , "userid": "a"} { "country": "china", "province": "sh", "userid": "c"} { "country": "china" , "province":"bj", "userid" : "da" }  {  "country" : "china", "province" : "bj", "userid" : "fa" }
参考答案
db.tv3.aggregate ({$ group: {_ id: {country: '$ country', province: '$ province', userid: '$ userid'}}}, {$ group: {_ id: {country: '$ _id.country ', province:' $ _ id.province '}, count: {$ sum: 1}}}, {$ project: {_ id: 0, country:' $ _ id.country ', province:' $ _ id. Province ', counter:' $ COUNT '}})
. 6 pipe command of $ Sort
$ Sort documents sorted for the output of the input
using the example below:

  • Student information queries, by age ascending


db.stu.aggregate({$sort:{age:1}})

  • Query number of men and women, according to the number descending


db.stu.aggregate ({$ Group: ID {_: "$ Gender", counter: $ SUM {:}}}. 1, $ {Sort: {counter: -1}})
. 7 Pipe command sum $ Skip  and  $ limit

  • $ Limit limit the number of returned data
  • $ Skip to skip a specified number of documents and returns the remaining number of documents
  • While the first use skip using limit the use of


Use example:

  • Query 2 Student Information


db.stu.aggregate(     {$limit:2} )

  • Student information queries from the start of the third


db.stu.aggregate(     {$skip:3} )

  • People Counting boys and girls, according to the number in ascending order, the second return data


db.stu.aggregate(     {$group:{_id:"$gender",counter:{$sum:1}}},     {$sort:{counter:-1}},     {$skip:1},     {$limit:1} )

More technical information may concern: gzitcast

Guess you like

Origin www.cnblogs.com/heimaguangzhou/p/11542058.html