MongoDB Quick Start Tutorial (3.3)

3.4. Polymerization

3.4.1. What is polymerization?

MongoDB polymerized (Aggregate) is mainly used for processing data (such as statistical averages, sums, etc.), and returns the data calculated results. Somewhat similar sql statement count (*)

For example, a set of nine documents in the figure above, after the count operation by outputting a 9, this operation is a single operation of polymerization

3.4.2. What is Polymer pipes?

Mentioned above, a single aggregate operations, Polymer pipes that what is it? You can put the pipe understood as a line, or just water, water is the data as we call data streams, pipes, installed with all data, the data flow in the pipeline, the data can be passed after treatment in a pipe next to a pipeline, and ultimately get the data you want

Tell me what your net example:

There are four in the document data set, $ match after the match operation status is the data A, the data processing at this time only 3, this status is D, the data is filtered out, followed by the data into the next pipe to $ group to deal with this case, the role of $ group is a document group, then the packet according to the amount field summation, id is the final result of A123 750 (500 + 250)

3.4.3 How to operate using a polymerization pipe

1.aggregate () method

This method is used to perform the specific operation of the pipeline, the specific syntax is as follows:

db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)  

Examples official website:

db.orders.aggregate([
  {$match: {status: "A"}},
  {$group: {_id: "$cust_id", total: {$sum: "$amount"}}}
])

The method aggregate passing an array, each element of the data is one polymerization operation, the operation performed after the first element of the element will be passed to the next execution, each element is an object, the object key is pipeline operator, for example: {$ match: {status: "a"}}, which is the inside of the pipeline operator $ match, the target value is the value duct expression, for example: {$ match: {status: "a "}}, there is the {status:" a "} is the expression pipe

2. Common pipeline operator

3. Pipeline expression

前面我们已经说过什么是管道表达式,例如:{_id: "$cust_id", total: {$sum: "$amount"}},每一个管道表达式都是对象的形式,里面也包含了key和value,key通常都是字段名,value就是字段值, 字段值里面可以是由表达式操作符组成的对象,例如:{$sum: "$amount"},这个对象就是由表达式操作符组成的,其中,$sum就是一个表达式操作符,用于求和运算

常见的表达式操作符如下:

4.实际操作

准备数据,你可以将下列代码直接拷贝到mongodb中执行:

db.order.insert({"order_id":"1","uid":10001,"trade_no":"nd001","all_price":10,"all_num":9}) 
db.order.insert({"order_id":"2","uid":10002,"trade_no":"nd002","all_price":20,"all_num":8}) 
db.order.insert({"order_id":"3","uid":10003,"trade_no":"nd003","all_price":30,"all_num":7})
db.order_item.insert({"order_id":"1","title":"无线鼠标 1","price":500,num:10}) 
db.order_item.insert({"order_id":"1","title":"无线键盘 2","price":600,num:11}) 
db.order_item.insert({"order_id":"1","title":"有线键盘 3","price":700,num:12})
db.order_item.insert({"order_id":"2","title":"苹果","price":50,num:2}) 
db.order_item.insert({"order_id":"2","title":"香蕉","price":40,num:3})
db.order_item.insert({"order_id":"3","title":"牛奶","price":20,num:9}) 
db.order_item.insert({"order_id":"3","title":"面包","price":10,num:4})

1.$project

通俗的说这个管道操作符可以用来筛选指定的字段

举例:查询出订单信息,只显示订单号

db.order.aggregate([{$project:{_id: 0, order_id: 1}}])

2.$match

通俗的说$match操作的作用就是查找满足条件的数据

db.order.aggregate([{$project: {order_id: 1, all_price: 1}}, {$match: {"all_price": {$gt: 20}}}])

3.$group

$group主要是用来进行分组操作的

db.order_item.aggregate([{$group: {_id: "$order_id", total: {$sum: "$num"}}}])

注意:在使用$group的时候必须有一个_id作为分组的条件

4.$sort

$sort的作用是用来进行排序

db.order_item.aggregate([{$sort: {"price": -1}}])

注意: -1 表示降序, 1表示升序

5.$limit

$limit的作用是限制显示条数

db.order_item.aggregate([{$sort: {"price": -1}}, {$limit: 1}])

6.$skip

$skip的作用是可以跳过的条数

db.order_item.aggregate([{$sort: {"price": -1}}, {$skip: 3}])

7.$lookup

$lookup用来做关联查询,举个例子:查询order表中,order_id对应的商品信息

db.order.aggregate([{$lookup:{from: "order_item", localField: "order_id", foreignField: "order_id"

螺钉课堂视频课程地址:http://edu.nodeing.com

Guess you like

Origin www.cnblogs.com/dadifeihong/p/12048380.html
Recommended