MongoDB Aggregation Pipeline

索引请点击↑↑↑

管道操作符

速查

名称描述
$project数据投影,主要用于重命名、增加和删除字段
$match滤波操作,筛选符合条件文档,作为下一阶段的输入
$limit限制经过管道的文档数量
$skip从待操作集合开始的位置跳过文档的数目
$unwind将数组元素拆分为独立字段
$group对数据进行分组
$sort对文档按照指定字段排序

$project

数据投影,主要用于重命名、增加和删除字段

删除 _id 字段
_id: 0
显示本来就有的字段
City: 1
重命名字段
MyCity: "$City"
新建子文档
子文档: {
    AzGroup: "$AzGroup",
    InnerMark: "$InnerMark"
}
例子如下

这里写图片描述

$match

$match的语法和查询表达式db.collection.find()的语法相同,用于获取分数大于70小于或等于90记录,然后将符合条件的记录送到下一阶段$group管道操作符进行处理。

db.articles.aggregate([
    { $match : 
        { score : 
            { $gt : 70, $lte : 90 } 
        } 
    },
    { $group: 
        { 
            _id: null, 
            count: { $sum: 1 } 
        } 
    }
] );
注意
  1. 不能在$match操作符中使用$where表达式操作符。

  2. $match尽量出现在管道的前面,这样可以提早过滤文档,加快聚合速度。

  3. 如果$match出现在最前面的话,可以使用索引来加快查询。

$limit

$limit的参数只能是一个正整数

db.article.aggregate(
    { 
        $limit : 5 
    }
);

这样处理后,管道内就只剩 5个文档了

$skip

$skip参数也只能为一个正整数

db.article.aggregate(
    { 
        $skip : 5 
    }
);

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

$unwind

例子:

article文档中有一个名字为tags数组字段:

{ 
    "_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
}
注意:
  1. {$unwind:"$tags"})不要忘了$符号

  2. 如果$unwind目标字段不存在的话,那么该文档将被忽略过滤掉,例如:

    db.article.aggregate(
        {
            $project:{
                author:1, 
                title:1, 
                tags:1 
            } 
        },
        { 
            $unwind:"$tag" 
        } 
    )

    $tags改为$tag因不存在该字段,该文档被忽略,输出的结果为空

  3. 如果$unwind目标字段不是一个数组的话,将会产生错误,例如:

    db.article.aggregate(
        { 
            $project:{author:1,title:1,tags:1} 
        },
        {
            $unwind:"$title" 
        } 
    )
    
    // Error
    Error: Printing Stack Trace
    // 略
  4. 如果$unwind目标字段数组为空的话,该文档也将会被忽略。

$group

$group的时候必须要指定一个_id域,同时也可以包含一些算术类型的表达式操作符:

例子:
db.article.aggregate(
    { 
        $group : {
            _id : "$author",
            docsPerAuthor : { $sum : 1 },
            viewsPerAuthor : { $sum : "$pageViews" }
        }
    }
);
注意:
  1. $group的输出是无序的。
  2. $group操作目前是在内存中进行的,所以不能用它来对大量个数的文档进行分组。

$sort

使用方式
db.users.aggregate( 
    { 
        $sort : { 
            age : -1, 
            posts: 1 
        } 
    }
); 

注:按照年龄进行降序操作,按照posts进行升序操作

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

$geoNear

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

管道表达式

组聚合操作符

名称描述
$addToSetReturns an array of all the unique values for the selected field among for each document in that group.
$firstReturns the first value in a group.
$lastReturns the last value in a group.
$maxReturns the highest value in a group.
$minReturns the lowest value in a group.
$avgReturns an average of all the values in a group.
$pushReturns an array of all values for the selected field among for each document in that group.
$sumReturns the sum of all the values in a group.

Bool类型聚合操作符

名称描述
$andReturns true only when all values in its input array are true.
$orReturns true when any value in its input array are true.
$notReturns the boolean value that is the opposite of the input value.

比较类型聚合操作符

名称描述
$cmpCompares two values and returns the result of the comparison as an integer.
$eqTakes two values and returns true if the values are equivalent.
$gtTakes two values and returns true if the first is larger than the second.
$gteTakes two values and returns true if the first is larger than or equal to the second.
$ltTakes two values and returns true if the second value is larger than the first.
$lteTakes two values and returns true if the second value is larger than or equal to the first.
$neTakes two values and returns true if the values are not equivalent.

算术类型聚合操作符

名称描述
$addComputes the sum of an array of numbers.
$divideTakes two numbers and divides the first number by the second.
$modComputes the product of an array of numbers.
$multiplyTakes two values and returns true if the first is larger than or equal to the second.
$subtractTakes two numbers and subtracts the second number from the first.

字符串类型聚合操作符

名称描述
$concatConcatenates two strings.
$strcasecmpCompares two strings and returns an integer that reflects the comparison.
$substrTakes a string and returns portion of that string.
$toLowerConverts a string to lowercase.
$toUpperConverts a string to uppercase.

日期类型聚合操作符

名称描述
$dayOfYearConverts a date to a number between 1 and 366.
$dayOfMonthConverts a date to a number between 1 and 31.
$dayOfWeekConverts a date to a number between 1 and 7.
$yearConverts a date to the full year.
$monthConverts a date into a number between 1 and 12.
$weekConverts a date into a number between 0 and 53
$hourConverts a date into a number between 0 and 23.
$minuteConverts a date into a number between 0 and 59.
$secondConverts a date into a number between 0 and 59. May be 60 to account for leap seconds.
$millisecondReturns the millisecond portion of a date as an integer between 0 and 999.

条件类型聚合操作符

名称描述
$condA ternary operator that evaluates one expression, and depending on the result returns the value of one following expressions.
$ifNullEvaluates an expression and returns a value.

有点多,改天(不可能)翻译

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值