MongoDB 聚合管道(一)(Aggregation Pipeline)

MongoDB系列--深入理解MongoDB聚合(Aggregation )   MongoDB中聚合(aggregate) 操作将来自多个document的value组合在一起,并通过对分组数据进行各种操作处理,并返回计算后的数据结果,主要用于处理数据(诸如统计平均值,求和等)。MongoDB提供三种方式去执行聚合操作:聚合管道(aggregation pipeline)、Map-Reduce函数以及单一的聚合命令(count、distinct、group)。 1. 聚合... 阅读详情
一 管道的概念

        管道是MongoDB2.2版本引入新的功能 ,它是数据聚合的一个新框架,其概念类似于数据处理的管道。管道的工作方式类似于UNIX-like的shell ps aux | grep mong*  (下面的补充会详细介绍管道原理)。

       每个文档通过一个由多个节点组成的管道,每个节点有自己特殊的功能(分组、过滤等),文档经过管道处理后,最后输出相应的结果。管道基本的功能有两个:一是对文档进行“过滤”,也就是筛选出符合条件的文档;二是对文档进行“变换”,也就是改变文档的输出形式。其他的一些功能还包括按照某个指定的字段分组和排序等。而且在每个阶段还可以使用表达式操作符计算平均值和拼接字符串等相关操作。管道提供了一个MapReduce 的替代方案,MapReduce使用相对来说比较复杂,而管道拥有固定的接口(操作符表达),使用比较简单,对于大多数的聚合任务管道一般来说是首选方法。
     
二 管道操作符

        管道是由一个个功能节点组成的,这些节点用管道操作符来进行表示。聚合管道以一个集合中的所有文档作为开始,然后这些文档从一个操作节点流向下一个节点 ,每个操作节点对文档做相应的操作。这些操作可能会创建新的文档或者过滤掉一些不符合条件的文档,在管道中可以对文档进行重复操作。
  
     先看一个管道聚合的例子:

                     

    
 
管道操作符的种类:
Name Description
$project Reshapes a document stream. $project can rename, add, or remove fields as well as create computed values and sub-documents.
$match Filters the document stream, and only allows matching documents to pass into the next pipeline stage.$match uses standard MongoDB queries.
$limit Restricts the number of documents in an aggregation pipeline.
$skip Skips over a specified number of documents from the pipeline and returns the rest.
$unwind Takes an array of documents and returns them as a stream of documents.
$group Groups documents together for the purpose of calculating aggregate values based on a collection of documents.
$sort Takes all input documents and returns them in a stream of sorted documents.
$geoNear Returns an ordered stream of documents based on proximity to a geospatial point.
管道操作符详细使用说明
  1.  $project: 数据投影,主要用于重命名、增加和删除字段,例如:

db.article.aggregate(
    { $project : {
        title : 1 ,
        author : 1 ,
    }}
 );
       这样的话结果中就只还有_id,tilte和author三个字段了,默认情况下_id字段是被包含的,如果要想不包含_id话可以这样:
db.article.aggregate(
    { $project : {
        _id : 0 ,
        title : 1 ,
        author : 1
    }});
     也可以在$project内使用算术类型表达式操作符 ,例如:
db.article.aggregate(
    { $project : {
        title : 1,
        doctoredPageViews : { $add:["$pageViews", 10] }
    }});
    通过使用$add给pageViews字段的值加10,然后将结果赋值给一个新的字段:doctoredPageViews 
    注:必须将$add计算表达式放到中括号里面
    除此之外使用$project还可以重命名字段名和子文档的字段名:
db.article.aggregate(
    { $project : {
        title : 1 ,
        page_views : "$pageViews" ,
        bar : "$other.foo"
    }});
 也可以添加子文档:
db.article.aggregate(
    { $project : {
        title : 1 ,
        stats : {
            pv : "$pageViews",
            foo : "$other.foo",
            dpv : { $add:["$pageViews", 10] }
        }
    }});
产生了一个子文档stats,里面包含pv,foo,dpv三个字段。

 2.  $match: 滤波操作,筛选符合条件文档,作为下一阶段的输入
     $match的语法和查询表达式(db.collection.find())的语法相同
db.articles.aggregate( [
                        { $match : { score : { $gt : 70, $lte : 90 } } },
                        { $group: { _id: null, count: { $sum: 1 } } }
                       ] );
   $match用于获取分数大于70小于或等于90记录,然后将符合条件的记录送到下一阶段$group管道操作符进行处理。
注意: 1. 不能在$match操作符中使用$where表达式操作符。
            2. $match尽量出现在管道的前面,这样可以提早过滤文档,加快聚合速度。
            3. 如果$match出现在最前面的话,可以使用索引来加快查询。

 3.  $limit:  限制经过管道的文档数量
     $limit的参数只能是一个正整数

db.article.aggregate(
    { $limit : 5 });
这样的话经过$limit管道操作符处理后,管道内就只剩下前5个文档了

 4.  $skip: 从待操作集合开始的位置跳过文档的数目
    $skip参数也只能为一个正整数
db.article.aggregate(
    { $skip : 5 });

经过$skip管道操作符处理后,前五个文档被“过滤”掉

 5.  $unwind:将数组元素拆分为独立字段
例如:article文档中有一个名字为tags数组字段:
> db.article.find()
  { "_id" : ObjectId("528751b0e7f3eea3d1412ce2"),
 "author" : "Jone", "title" : "Abook", 
 "tags" : [  "good",  "fun",  "good" ] }
  
使用$unwind操作符后:
> db.article.aggregate({$project:{author:1,title:1,tags:1}},{$unwind:"$tags"})
{
        "result" : [
                {
                        "_id" : ObjectId("528751b0e7f3eea3d1412ce2"),
                        "author" : "Jone",
                        "title" : "A book",
                        "tags" : "good"
                },
                {
                        "_id" : ObjectId("528751b0e7f3eea3d1412ce2"),
                        "author" : "Jone",
                        "title" : "A book",
                        "tags" : "fun"
                },
                {
                        "_id" : ObjectId("528751b0e7f3eea3d1412ce2"),
                        "author" : "Jone",
                        "title" : "A book",
                        "tags" : "good"
                }
        ],
        "ok" : 1
}
注意:a. {$unwind:"$tags"})不要忘了$符号
            b. 如果$unwind目标字段不存在的话,那么该文档将被忽略过滤掉,例如:
               > db.article.aggregate({$project:{author:1,title:1,tags:1}},{$unwind:"$tag"})
                    { "result" : [ ], "ok" : 1 }
              将$tags改为$tag因不存在该字段,该文档被忽略,输出的结果为空
            c. 如果$unwind目标字段不是一个数组的话,将会产生错误,例如:
            > db.article.aggregate({$project:{author:1,title:1,tags:1}},{$unwind:"$title"})
                Error: Printing Stack Trace
               at printStackTrace (src/mongo/shell/utils.js:37:15)
               at DBCollection.aggregate (src/mongo/shell/collection.js:897:9)
               at (shell):1:12
               Sat Nov 16 19:16:54.488 JavaScript execution failed: aggregate failed: {
                    "errmsg" : "exception: $unwind:  value at end of field path must be an array",
                    "code" : 15978,
                    "ok" : 0
               } at src/mongo/shell/collection.js:L898
            d. 如果$unwind目标字段数组为空的话,该文档也将会被忽略。

     6. $group 对数据进行分组
        $group的时候必须要指定一个_id域,同时也可以包含一些算术类型的表达式操作符:
db.article.aggregate(
    { $group : {
        _id : "$author",
        docsPerAuthor : { $sum : 1 },
        viewsPerAuthor : { $sum : "$pageViews" }
    }});
注意:  1.$group的输出是无序的。
             2.$group操作目前是在内存中进行的,所以不能用它来对大量个数的文档进行分组。

  7.  $sort : 对文档按照指定字段排序
  使用方式如下:

    db.users.aggregate( { $sort : { age : -1, posts: 1 } }); 
    按照年龄进行降序操作,按照posts进行升序操作
注意:1. 如果将$sort放到管道前面的话可以利用索引,提高效率
           2. MongoDB 24.对内存做了优化,在管道中如果$sort出现在$limit之前的话,$sort只会对前$limit个文档进行操作,这样在内存中也只会保留前$limit个文档,从而可以极大的节省内存
           3. $sort操作是在内存中进行的,如果其占有的内存超过物理内存的10%,程序会产生错误

 8.   $goNear
       $goNear会返回一些坐标值,这些值以按照距离指定点距离由近到远进行排序

具体使用参数见下表:


Field Type Description
near GeoJSON point orlegacy coordinate pairs The point for which to find the closest documents.
distanceField string The output field that contains the calculated distance. To specify a field within a subdocument, use dot notation.
limit number Optional. The maximum number of documents to return. The default value is 100. See also the num option.
num number Optional. The num option provides the same function as the limitoption. Both define the maximum number of documents to return. If both options are included, the num value overrides the limit value.
maxDistance number Optional. A distance from the center point. Specify the distance in radians. MongoDB limits the results to those documents that fall within the specified distance from the center point.
query document Optional. Limits the results to the documents that match the query. The query syntax is the usual MongoDB read operation query syntax.
spherical Boolean Optional. If true, MongoDB references points using a spherical surface. The default value is false.
distanceMultiplier number Optional. The factor to multiply all distances returned by the query. For example, use the distanceMultiplier to convert radians, as returned by a spherical query, to kilometers by multiplying by the radius of the Earth.
includeLocs string Optional. This specifies the output field that identifies the location used to calculate the distance. This option is useful when a location field contains multiple locations. To specify a field within a subdocument, usedot notation.
uniqueDocs Boolean Optional. If this value is true, the query returns a matching document once, even if more than one of the document’s location fields match the query. If this value is false, the query returns a document multiple times if the document has multiple matching location fields. See $uniqueDocsfor more information.
  
例如:
db.places.aggregate([
                      {
                        $geoNear: {
                                    near: [40.724, -73.997],
                                    distanceField: "dist.calculated",
                                    maxDistance: 0.008,
                                    query: { type: "public" },
                                    includeLocs: "dist.location",
                                    uniqueDocs: true,
                                    num: 5
                                  }
                      }
                   ])
 其结果为:
{
  "result" : [
               { "_id" : 7,
                 "name" : "Washington Square",
                 "type" : "public",
                 "location" : [
                                [ 40.731, -73.999 ],
                                [ 40.732, -73.998 ],
                                [ 40.730, -73.995 ],
                                [ 40.729, -73.996 ]
                              ],
                 "dist" : {
                            "calculated" : 0.0050990195135962296,
                            "location" : [ 40.729, -73.996 ]
                          }
               },
               { "_id" : 8,
                 "name" : "Sara D. Roosevelt Park",
                 "type" : "public",
                 "location" : [
                                [ 40.723, -73.991 ],
                                [ 40.723, -73.990 ],
                                [ 40.715, -73.994 ],
                                [ 40.715, -73.994 ]
                              ],
                 "dist" : {
                            "calculated" : 0.006082762530298062,
                            "location" : [ 40.723, -73.991 ]
                          }
               }
             ],
  "ok" : 1}
其中,dist.calculated中包含了计算的结果,而dist.location中包含了计算距离时实际用到的坐标

注意: 1.使用$goNear只能在管道处理的开始第一个阶段进行
             2.必须指定distanceField,该字段用来决定是否包含距离字段
             3.$gonNear和geoNear命令比较相似,但是也有一些不同:distanceField在$geoNear中是必选的,而在geoNear中是可选的;includeLocs在$geoNear中是string类型,而在geoNear中是boolen类型。

三 管道表达式
     
     
  管道操作符作为“键”,所对应的“值”叫做管道表达式。例如上面例子中{$match:{status:"A"}},$match称为管道操作符,而{s tatus:"A"}称为管道表达式,它可以看做是管道操作符的操作数(Operand),每个管道表达式是一个文档结构,它是由字段名、字段值、和一些表达式操作符组成的,例如上面例子中管道表达式就包含了一个表达式操作符$sum进行累加求和。

      每个管道表达式只能作用于处理当前正在处理的文档,而不能进行跨文档的操作。管道表达式对文档的处理都是在内存中进行的。除了能够进行累加计算的管道表达式外,其他的表达式都是无状态的,也就是不会保留上下文的信息。累加性质的表达式操作符通常和$group操作符一起使用,来统计该组内最大值、最小值等,例如上面的例子中我们在$group管道操作符中使用了具有累加的$sum来计算总和。

     除了$sum以为,还有以下性质的表达式操作符:

组聚合操作符
Name Description
$addToSet Returns an array of all the unique values for the selected field among for each document in that group.
$first Returns the first value in a group.
$last Returns the last value in a group.
$max Returns the highest value in a group.
$min Returns the lowest value in a group.
$avg Returns an average of all the values in a group.
$push Returns an array of all values for the selected field among for each document in that group.
$sum Returns the sum of all the values in a group.
Bool类型聚合操作符
Name Description
$and Returns true only when all values in its input array are true.
$or Returns true when any value in its input array are true.
$not Returns the boolean value that is the opposite of the input value.
比较类型聚合操作符
Name Description
$cmp Compares two values and returns the result of the comparison as an integer.
$eq Takes two values and returns true if the values are equivalent.
$gt Takes two values and returns true if the first is larger than the second.
$gte Takes two values and returns true if the first is larger than or equal to the second.
$lt Takes two values and returns true if the second value is larger than the first.
$lte Takes two values and returns true if the second value is larger than or equal to the first.
$ne Takes two values and returns true if the values are not equivalent.

算术类型聚合操作符
Name Description
$add Computes the sum of an array of numbers.
$divide Takes two numbers and divides the first number by the second.
$mod Takes two numbers and calcualtes the modulo of the first number divided by the second.
$multiply Computes the product of an array of numbers.
$subtract Takes two numbers and subtracts the second number from the first.

字符串类型聚合操作符
Name Description
$concat Concatenates two strings.
$strcasecmp Compares two strings and returns an integer that reflects the comparison.
$substr Takes a string and returns portion of that string.
$toLower Converts a string to lowercase.
$toUpper Converts a string to uppercase.
日期类型聚合操作符
Name Description
$dayOfYear Converts a date to a number between 1 and 366.
$dayOfMonth Converts a date to a number between 1 and 31.
$dayOfWeek Converts a date to a number between 1 and 7.
$year Converts a date to the full year.
$month Converts a date into a number between 1 and 12.
$week Converts a date into a number between 0 and 53
$hour Converts a date into a number between 0 and 23.
$minute Converts a date into a number between 0 and 59.
$second Converts a date into a number between 0 and 59. May be 60 to account for leap seconds.
$millisecond Returns the millisecond portion of a date as an integer between 0 and 999.
条件类型聚合操作符
Name Description
$cond A ternary operator that evaluates one expression, and depending on the result returns the value of one following expressions.
$ifNull Evaluates an expression and returns a value.
注:以上操作符都必须在管道操作符的表达式内来使用。

各个表达式操作符的具体使用方式参见:


四 总结
       管道是MongoDB新引入的聚合框架,它使用了大量的管道操作符来固定聚合的接口,每种管道操作符中又可以包含丰富的表达式操作符,每种操作符都有其特定的功能,只有在理解这些操作符使用方法的基础上,才可以写出正确的管道聚合程序。

      下一节,将详细介绍聚合管道的代码编写。


补充  Linux shell中管道概念
      
      POSIX多线程的使用方式中, 有一种很重要的方式-----流水线(亦称为“管道”)方式,“数据元素”流串行地被一组线程按顺序执行。它的使用架构可参考下图:

                                              

      

 以面向对象的思想去理解,整个流水线,可以理解为一个数据传输的管道;该管道中的每一个工作线程,可以理解为一个整个流水线的一个工作阶段stage,这些工作线程之间的合作是一环扣一环的。靠输入口越近的工作线程,是时序较早的工作阶段stage,它的工作成果会影响下一个工作线程阶段(stage)的工作结果,即 下个阶段依赖于上一个阶段的输出,上一个阶段的输出成为本阶段的输入。这也是pipeline的一个共有特点!


MongoDB之聚合操作 MongoDB之聚合操作 文章目录MongoDB之聚合操作一、聚合简介二、aggregation pipeline分析2.1 聚合操作语法格式2.2 聚合指令的字段解释:2.3 管道操作符可选指令分析2.4 read(write) Concern 和 read(write)Preference三、 参考文档 一、聚合简介 MongoDB中的聚合操作将多个文档中的值组合在一起,并可对分组数据执行... 阅读详情

相关推荐

【MongoDB 实战】 06 MongoDB 聚合框架(Aggregation Framework)

在说 MongoDB 聚合框架之前,我们先了解下关系型数据库中的聚集函数和分组统计,我们常见的SQL五大的聚集函数有如下所示: 可见SQL 中的聚集函数是用来对数据表中的数据进行筛选过滤,然后对过滤后的数据进行一系列的函数运算。 MongoDB 中的聚合框架(Aggregation Framework)和SQL 中的聚集函数一样,它是一个计算框架,作用在一个或几个集合上面,对集合中的数据进行...

有道无术,术尚可求,有术无道,止于术。 776

MongoDB学习(五)mongodb终极查询-Aggregate

MongoDB学习(五)mongodb终极查询-Aggregate

weixin_44185213的博客 3815

Anaconda启动失败,一直在loading application的问题

实战基础新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本

Tristan的博客 2771

Aggregation-聚合查询

Aggregation: 参数说明:sql(Operators)      where ($match) 、group by ($group) 、having($match)、select($project)、order by($sort)、limit($limit)      sum($sum)、count($sum)、join($lookup) MongoVUE操作: S...

记录学习生活的点滴(Linux运维、java) 2万+

数据库存储分层架构搭建:热数据 SSD 存储、冷数据归档的配置操作及性能对比分析

本文探讨了数据库存储分层架构的优化策略。随着数据量激增,传统单一存储模式面临成本与性能的平衡难题。文章提出通过热数据SSD加速和冷数据归档降本的分层方案,系统分析了热数据SSD配置、冷数据归档操作及性能对比。分层架构基于数据活跃度匹配存储介质,使热数据保持高性能访问,冷数据低成本存储,实现总体成本降低60%-80%的同时确保核心查询性能。文中详细介绍了硬件选型、数据库配置、数据迁移等技术细节,并强调了动态调整分层策略的重要性,为构建高效经济的数据库存储体系提供了完整解决方案。

jingjing45678的博客 1291

排列显示阿拉伯语、数字及英文时的处理方法

这段时间参与开发沙特阿拉伯的网站模板,模板使用的语言是阿拉伯语,其显示顺序为从右到左,而中文、数字及英文的显示顺序为从左到右 ,所以当遇到一行文字内既有阿拉伯语又有数字和英文时会出现语序混乱的问题,如 期待显示的文案为: 按顺序写入,对应的代码如以下,其中currency_pay为变量,值为356SAR <span>الدفع الفعلي:{{currency_pay}}</span> 显示为 الدفع الفعلي : 356SAR 金额356和货币符号SAR分开了,这样显

llq886的博客 5805

java mongodb Aggregation的一些常用方法

java mongodb Aggregation的一些常用方法

我是小白程序员 5504

Joern实战:从代码导入到Python分析的全流程避坑指南(附常见错误解决方案)

本文详细介绍了Joern静态分析工具从代码导入到Python分析的全流程实践指南,包括环境配置、代码导入技巧、数据库服务优化、Python交互分析及性能调优等关键步骤。特别针对常见错误提供了实用解决方案,帮助开发者高效进行代码安全分析和漏洞挖掘。

weixin_28309571的博客 140

Aggregation MongoDB踩坑记录

对某些筛选条件进行分页查询,开始每一页的有效data都不足pageSize,最后发现,aggregation 的pipeline是有先后顺序的。 错误: agg = Aggregation.newAggregation(Aggregation.skip((curPage - 1) * pageSize), Aggregation.limit(pageSize), Aggregation.match(criteria), project); 这样是先找出每一页的data,然后再进行筛选 正确: agg =

weixin_44117394的博客 525

uniapp - 解决真机调试报错 request:fail abort statusCode:-1 java.security.cert.CertPathValidatorException: Tr

uni-app,真机调试报错,app端平台,安卓Android,安卓App发布线上报错,打包后接口请求失效了,api接口报错,真机运行安卓app时接口不通且报错,request:failabortstatusCode:-1java.security.cert.CertPathValidatorException:Trustanchorforcertificationpathnotfound.,真机调试,苹果ios真机报错,Uniapp打包到app后报错,uni-app真机调试报错,服务器

🏆 前端领域优质创作者 1万+

Unity之ASE+ShaderGraph+VFX入门到精通 目录

这里是ASE+ShaderGraph+VFX从入门到精通专栏的目录,不停更新中,有问题随时留言或者私信。

TxNet 2841

MongoDB 学习笔记之 Aggregation Pipeline实战实现inner join

Aggregation Pipeline实战实现inner join: leftT集合: comments集合: 现在我们要用aggregation实现inner join db.comments.aggregate([{ $lookup: { from:"leftT", localField:"timestamp", foreignFie...

baguashenp74070的博客 251

超简单五步实现VOS3000客户端呼入配置

1.查看SIP代理  在注册管理中查看自己线路注册的SIP代理IP地址,记录下来。 2.创建普通账户  创建一个呼入使用的普通账号,选择“计费费率”,根据自己情况选择费率,这里做测试就配置了免费的费率,如果费率不是免费,需要给这个普通账户充值。 3.新建对接网关 新建一个对接网关,点击该网关的“补充设置”,选择“网关类型”为静态IP,然后在IP地址栏填上刚才在注册管理里面记录的SIP代...

非著名运维的博客 1万+

MongoDB聚合管道(Aggregation Pipeline)

聚合管道(Aggregation Pipeline)是MongoDB中用于对数据进行处理和分析的一种强大机制。它由一系列的阶段(Stage)组成,每个阶段对输入的数据进行一种特定的操作,然后将结果传递给下一个阶段,就像在一个管道中对数据进行逐步处理一样。

莫凡的博客 1359

C#与倍福Plc通信——使用仿真软件模拟倍福PLC运行

安装这个软件之前先安装VisualStudio,本文安装这个软件时已经安装了VisualStudio2019。1、下载倍福PLC编程软件TwinCAT。

qq_34059233的博客 3664

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

MongoDB 聚合管道(Aggregation Pipeline)

MongoDB 聚合管道(Aggregation Pipeline)

minihuabei的博客 1826

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

聚合管道(Aggregation Pipeline)

目录1 管道2 管道表达式3 聚合管道行为3.1 管道操作符和索引3.2 早期过滤(Early Filtering)4 注意事项4.1 分片集合4.2 聚合 vs Map-Reduce4.3 限制4.4 管道优化 进入MongoDB中文手册(4.2版本)专栏 聚合管道是一个基于数据处理管道概念建模的数据聚合框架。文档进入一个多阶段的管道,该管道将文档转换成聚合的结果。例如: db.orders.aggregate([ { $match: { status: "A" } }, { $group:

zxwjx的专栏 1832

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

MongoDB 聚合管道(二)(Aggregation Pipeline)

Mongo系列优秀文章链接点击打开链接 聚合管道的优化    1.$sort  +  $skip  +  $limit顺序优化      如果在执行管道聚合时,如果$sort、$skip、$limit依次出现的话,例如:      { $sort: { age : -1 } },      { $skip: 10 },      { $limit: 5 }

haozi3156666的专栏 841

MongoDB聚合(Aggregation Pipeline基础篇上

学习MongoDB 十一: MongoDB聚合(Aggregation Pipeline基础篇上)(三) 2016年06月09日 10:47:10 阅读数:15320 一、Aggregate简介 db.collection.aggregate()是基于数据处理的聚合管道,每个文档通过一个由多个阶段(stage)组成的管道,可以对每个阶段的管道进行分组、过滤等功能,...

aaywod4483的博客 505

“Run Aggregation Pipelines” 通常指在 MongoDB 中执行聚合管道(Aggregation Pipeline)操作

“Run Aggregation Pipelines” 通常指在 MongoDB 中执行聚合管道(Aggregation Pipeline)操作,用于对集合中的文档进行一系列数据处理(如筛选、分组、排序、投影、连接等),最终返回转换后的结果。 MongoDB 聚合管道由多个阶段(stages)组成,每个阶段以 `$` 开头(如 `$match`, `$group`, `$sort`, `$project`, `$lookup` 等),按顺序执行,前一阶段的输出作为后一阶段的输入。

[Blog][Domain] programb.blog.youkuaiyun.com 423
上一篇: MongoDB 聚合管道(二)(Aggregation Pipeline)
下一篇: MongoDB 自动分片 (一) (Auto-Sharding)
张哲BJUT
博客等级 码龄15年 151粉丝 111原创
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值