赞
踩
$sum
聚合运算符返回数值的合计值,计算式
$sum
会忽略非数值的值。
$sum
适用于以下阶段:
$addFields
阶段(从MongoDB3.4开始支持)$bucket
阶段$budketAuto
阶段$group
阶段$match
阶段的$expr
表达式$replaceRoot
阶段(从MongoDB3.4开始支持)$replaceWith
阶段(从MongoDB4.2开始支持)$set
阶段(从MongoDB4.2开始支持)$setWindowFields
阶段(从MongoDB5.0开始支持)当$sum
用作累加器时,语法为:
{ $sum: <expression> }
当$sum
用于非累加器时,语法为:
{ $sum: [ <expression1>, <expression2> ... ] }
结果将与输入具有相同的类型,除非无法用该类型准确表示,在这些情况下:
$sum
忽略非数字值并返回数字值的总和。$sum
会为该字段返回0
。$sum
返回0
。在$group
阶段,如果表达式解析为数组,$sum
会将操作数视为非数字值,对于其他支持的阶段:
$sum
会遍历数组,对数字元素进行操作,返回累加值。$sum
不会遍历数组,而是将数组视为非数字值。$group
阶段sales
集合有以下文档:
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-02-03T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-03T09:05:00Z") }
{ "_id" : 4, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-02-15T08:00:00Z") }
{ "_id" : 5, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T09:05:00Z") }
下面的聚合根据date
字段的日和年对文档进行分组,使用$sum
累加器计算每组文档的总金额和计数:
db.sales.aggregate(
[
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
totalAmount: { $sum: { $multiply: [ "$price", "$quantity" ] } },
count: { $sum: 1 }
}
}
]
)
操作返回下面的结果:
{ "_id" : { "day" : 46, "year" : 2014 }, "totalAmount" : 150, "count" : 2 }
{ "_id" : { "day" : 34, "year" : 2014 }, "totalAmount" : 45, "count" : 2 }
{ "_id" : { "day" : 1, "year" : 2014 }, "totalAmount" : 20, "count" : 1 }
对不存在的字段使用$sum
将返回0
。以下操作对qty
进行$sum
:
db.sales.aggregate(
[
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
totalAmount: { $sum: "$qty" },
count: { $sum: 1 }
}
}
]
)
操作返回下面的结果:
{ "_id" : { "day" : 46, "year" : 2014 }, "totalAmount" : 0, "count" : 2 }
{ "_id" : { "day" : 34, "year" : 2014 }, "totalAmount" : 0, "count" : 2 }
{ "_id" : { "day" : 1, "year" : 2014 }, "totalAmount" : 0, "count" : 1 }
在$group
阶段可以使用{$sum:1}
平替$count
。
$project
阶段students
聚合有下面的文档:
{ "_id": 1, "quizzes": [ 10, 6, 7 ], "labs": [ 5, 8 ], "final": 80, "midterm": 75 }
{ "_id": 2, "quizzes": [ 9, 10 ], "labs": [ 8, 8 ], "final": 95, "midterm": 80 }
{ "_id": 3, "quizzes": [ 4, 5, 5 ], "labs": [ 6, 5 ], "final": 78, "midterm": 70 }
下面的聚合操作在$project
阶段使用$sum
运算符计算测验总分、实验室总分以及期末和期中考试的总分:
db.students.aggregate([
{
$project: {
quizTotal: { $sum: "$quizzes"},
labTotal: { $sum: "$labs" },
examTotal: { $sum: [ "$final", "$midterm" ] }
}
}
])
操作返回下面的结果:
{ "_id" : 1, "quizTotal" : 23, "labTotal" : 13, "examTotal" : 155 }
{ "_id" : 2, "quizTotal" : 19, "labTotal" : 16, "examTotal" : 175 }
{ "_id" : 3, "quizTotal" : 14, "labTotal" : 11, "examTotal" : 148 }
$setWindowFields
阶段使用下面的脚本创建cakeSales
集合,包含了在加利福尼亚州(CA)和华盛顿州(WA)的蛋糕销售记录:
db.cakeSales.insertMany( [
{ _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
state: "CA", price: 13, quantity: 120 },
{ _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
state: "WA", price: 14, quantity: 140 },
{ _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
state: "CA", price: 12, quantity: 145 },
{ _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
state: "WA", price: 13, quantity: 104 },
{ _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
state: "CA", price: 41, quantity: 162 },
{ _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
state: "WA", price: 43, quantity: 134 }
] )
下面的示例在 $setWindowFields
阶段使用 $sum
输出每个州蛋糕销售数量的合计:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { orderDate: 1 }, output: { sumQuantityForState: { $sum: "$quantity", window: { documents: [ "unbounded", "current" ] } } } } } ] )
在本例中:
partitionBy: "$state"
按照州state
对集合的文档进行分区,有CA
和WA
两个区。sortBy: { orderDate: 1 }
根据orderDate
对分区内文档进行由小到大的排序,orderDate
最早的排在第一个。output
使用$sum
将窗口内文档的quantity
合计值赋予sumQuantityForState
字段。窗口包含无界下限和输出中的当前文档之间的文档,这意味着$sum
返回分区开头和当前文档之间文档的quantity
合计。在此示例输出中,CA
和WA
的quantity
合计值放在sumQuantityForState
字段:
操作结果如下:
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
"state" : "CA", "price" : 41, "quantity" : 162, "sumQuantityForState" : 162 }
{ "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
"state" : "CA", "price" : 13, "quantity" : 120, "sumQuantityForState" : 282 }
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
"state" : "CA", "price" : 12, "quantity" : 145, "sumQuantityForState" : 427 }
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
"state" : "WA", "price" : 43, "quantity" : 134, "sumQuantityForState" : 134 }
{ "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
"state" : "WA", "price" : 13, "quantity" : 104, "sumQuantityForState" : 238 }
{ "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
"state" : "WA", "price" : 14, "quantity" : 140, "sumQuantityForState" : 378 }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。