$addToSet
返回一个无重复元素的数组,元素值是对每个分组文档执行表达式的结果。数组元素顺序未指定。
$addToSet
可以用于下列聚合阶段:
$bucket
$bucketAuto
$group
$setWindowFeilds
语法
{ $addToSet: <expression> }
用法
- 如果表达式的值是个数组,
$addToSet
会把整个数组当成一个元素添加到返回的数组。 - 如果表达式的值是一个文档,那么如果数组中的另一个文档与要添加的文档完全匹配,MongoDB 就会判定该文档为重复文档。具体来说,现有文档具有完全相同的字段和值,且顺序完全相同。
举例
在$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" ]
}
}
}
}
}
] )
本例中:文章来源:https://www.toymoban.com/news/detail-830199.html
-
partitionBy: "$state"
根据state
对集合文档进行分区,分为CA
和WA
两个分区。 -
sortBy: { orderDate: 1 }
将每个分区中的文件按orderDate
升序排序(1)
,最早的orderDate
排在前面。 -
output
使用运行在文档窗口的$addToSet
把每个唯一的蛋糕类型type
添加到cakeTypesForState
数组字段。
该窗口包含介于无限制下限unbounded
和当前current
文档之间的文档,这意味着$addToSet
将返回一个数组,该数组包含分区开头和当前文档之间文档的唯一蛋糕类型type
。
本例在CA
和WA
的cakeTypesForState
字段输出蛋糕类型type
数组:文章来源地址https://www.toymoban.com/news/detail-830199.html
{ "_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" ] }
到了这里,关于MongoDB聚合操作符:$addToSet的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!