索引请点击↑↑↑
管道操作符
速查
名称 | 描述 |
---|---|
$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 }
}
}
] );
注意
不能在
$match
操作符中使用$where
表达式操作符。$match
尽量出现在管道的前面,这样可以提早过滤文档,加快聚合速度。如果
$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
}
注意:
{$unwind:"$tags"})
不要忘了$
符号如果$unwind目标字段不存在的话,那么该文档将被忽略过滤掉,例如:
db.article.aggregate( { $project:{ author:1, title:1, tags:1 } }, { $unwind:"$tag" } )
将
$tags
改为$tag
因不存在该字段,该文档被忽略,输出的结果为空如果
$unwind
目标字段不是一个数组的话,将会产生错误,例如:db.article.aggregate( { $project:{author:1,title:1,tags:1} }, { $unwind:"$title" } ) // Error Error: Printing Stack Trace // 略
如果
$unwind
目标字段数组为空的话,该文档也将会被忽略。
$group
$group
的时候必须要指定一个_id
域,同时也可以包含一些算术类型的表达式操作符:
例子:
db.article.aggregate(
{
$group : {
_id : "$author",
docsPerAuthor : { $sum : 1 },
viewsPerAuthor : { $sum : "$pageViews" }
}
}
);
注意:
$group
的输出是无序的。$group
操作目前是在内存中进行的,所以不能用它来对大量个数的文档进行分组。
$sort
使用方式
db.users.aggregate(
{
$sort : {
age : -1,
posts: 1
}
}
);
注:按照年龄进行降序操作,按照posts进行升序操作
注意:
- 如果将
$sort
放到管道前面的话可以利用索引,提高效率 - MongoDB 2.4对内存做了优化,在管道中如果
$sort
出现在$limit
之前的话,$sort
只会对前$limit
个文档进行操作,这样在内存中也只会保留前$limit
个文档,从而可以极大的节省内存 $sort
操作是在内存中进行的,如果其占有的内存超过物理内存的10%,程序会产生错误
$geoNear
略
$geoNear
会返回一些坐标值,这些值以按照距离指定点距离由近到远进行排序
管道表达式
组聚合操作符
名称 | 描述 |
---|---|
$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类型聚合操作符
名称 | 描述 |
---|---|
$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. |
比较类型聚合操作符
名称 | 描述 |
---|---|
$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. |
算术类型聚合操作符
名称 | 描述 |
---|---|
$add | Computes the sum of an array of numbers. |
$divide | Takes two numbers and divides the first number by the second. |
$mod | Computes the product of an array of numbers. |
$multiply | Takes two values and returns true if the first is larger than or equal to the second. |
$subtract | Takes two numbers and subtracts the second number from the first. |
字符串类型聚合操作符
名称 | 描述 |
---|---|
$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. |
日期类型聚合操作符
名称 | 描述 |
---|---|
$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. |
条件类型聚合操作符
名称 | 描述 |
---|---|
$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. |
有点多,改天(不可能)翻译