$addToSet
返回一个无重复元素的数组,元素值是对每个分组文档执行表达式的结果。数组元素顺序未指定。
$addToSet
可以用于下列聚合阶段:
$bucket
$bucketAuto
$group
$setWindowFeilds
{ $addToSet:
$addToSet
会把整个数组当成一个元素添加到返回的数组。$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:12:00Z") }
根据date
字段按照日和年对文档进行分组,下面的操作使用$addToSet
将分组内不重复的销售物品作为列表返回:
db.sales.aggregate(
[
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
itemsSold: { $addToSet: "$item" }
}
}
]
)
操作返回下面的结果:
{ "_id" : { "day" : 46, "year" : 2014 }, "itemsSold" : [ "xyz", "abc" ] }
{ "_id" : { "day" : 34, "year" : 2014 }, "itemsSold" : [ "xyz", "jkl" ] }
{ "_id" : { "day" : 1, "year" : 2014 }, "itemsSold" : [ "abc" ] }
$setWindowFields
中使用创建一个cakeSales
集合,包含蛋糕在California (CA)
和Washington (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
集合中使用$addToSet
,输出在每个州销售的不重复的蛋糕类型:
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
cakeTypesForState: {
$addToSet: "$type",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )
本例中:
partitionBy: "$state"
根据state
对集合文档进行分区,分为CA
和WA
两个分区。sortBy: { orderDate: 1 }
将每个分区中的文件按orderDate
升序排序(1)
,最早的orderDate
排在前面。output
使用运行在文档窗口的$addToSet
把每个唯一的蛋糕类型type
添加到cakeTypesForState
数组字段。unbounded
和当前current
文档之间的文档,这意味着$addToSet
将返回一个数组,该数组包含分区开头和当前文档之间文档的唯一蛋糕类型type
。本例在CA
和WA
的cakeTypesForState
字段输出蛋糕类型type
数组:
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
"state" : "CA", "price" : 41, "quantity" : 162,
"cakeTypesForState" : [ "strawberry" ] }
{ "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
"state" : "CA", "price" : 13, "quantity" : 120,
"cakeTypesForState" : [ "strawberry", "chocolate" ] }
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
"state" : "CA", "price" : 12, "quantity" : 145,
"cakeTypesForState" : [ "strawberry", "vanilla", "chocolate" ] }
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
"state" : "WA", "price" : 43, "quantity" : 134,
"cakeTypesForState" : [ "strawberry" ] }
{ "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
"state" : "WA", "price" : 13, "quantity" : 104,
"cakeTypesForState" : [ "vanilla", "strawberry" ] }
{ "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
"state" : "WA", "price" : 14, "quantity" : 140,
"cakeTypesForState" : [ "vanilla", "chocolate", "strawberry" ] }