$addToSet (aggregation)
On this page本页内容
Definition定义
Changed in version 5.0.5.0版更改。
$addToSet
$addToSet
returns an array of all unique values that results from applying an expression to each document in a group.返回将表达式应用于组中的每个文档所产生的所有唯一值的数组。
The order of the elements in the returned array is unspecified.返回数组中元素的顺序未指定。
$addToSet
is available in these stages:可在以下阶段使用:
$bucket
$bucketAuto
$group
$setWindowFields
(Available starting in MongoDB 5.0)(从MongoDB 5.0开始提供)
Syntax语法
$addToSet
syntax:语法:
{ $addToSet: <expression> }
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
Behavior行为
If the value of the expression is an array, 如果表达式的值是一个数组,$addToSet
appends the whole array as a single element.$addToSet
会将整个数组附加为单个元素。
If the value of the expression is a document, MongoDB determines that the document is a duplicate if another document in the array matches the to-be-added document exactly. 如果表达式的值是一个文档,那么如果数组中的另一个文档与要添加的文档完全匹配,MongoDB就会确定该文档是重复的。Specifically, the existing document has the exact same fields and values in the exact same order.具体来说,现有文档具有完全相同的字段和值,其顺序完全相同。
Examples实例
Use in $group
Stage在$group
阶段中使用
$group
StageConsider a 考虑一个包含以下文档的sales
collection with the following documents: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") }
Grouping the documents by the day and the year of the 根据date
field, the following operation uses the $addToSet
accumulator to compute the list of unique items sold for each group:date
字段的日期和年份对文档进行分组,以下操作使用$addToSet
累加器来计算每组销售的唯一项目列表:
db.sales.aggregate(
[
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
itemsSold: { $addToSet: "$item" }
}
}
]
)
The operation returns the following results:该操作返回以下结果:
{ "_id" : { "day" : 46, "year" : 2014 }, "itemsSold" : [ "xyz", "abc" ] }
{ "_id" : { "day" : 34, "year" : 2014 }, "itemsSold" : [ "xyz", "jkl" ] }
{ "_id" : { "day" : 1, "year" : 2014 }, "itemsSold" : [ "abc" ] }
Use in $setWindowFields
Stage在$setWindowFields
阶段中使用
$setWindowFields
StageNew in version 5.0. 5.0版新增。
Create a 创建一个包含加利福尼亚州(cakeSales
collection that contains cake sales in the states of California (CA
) and Washington (WA
):CA
)和华盛顿州(WA
)蛋糕销售的cakeSales
系列:
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 }
] )
This example uses 此示例使用$addToSet
in the $setWindowFields
stage to output the unique cake type
sales for each state
:$setWindowFields
阶段中的$addToSet
来输出每个state
的唯一蛋糕type
销售额:
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
cakeTypesForState: {
$addToSet: "$type",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )
In the example:在示例中:
partitionBy: "$state"
partitions the documents in the collection by按state
.state
对集合中的文档进行分区。There are partitions forCA
andWA
.CA
和WA
有分区。sortBy: { orderDate: 1 }
sorts the documents in each partition by按orderDate
in ascending order (1
), so the earliestorderDate
is first.orderDate
按升序(1
)对每个分区中的文档进行排序,因此最早的orderDate
是第一个。
output
adds each unique caketype
to thecakeTypesForState
array field using$addToSet
that is run in a documents window.output
使用在documents
窗口中运行的$addToSet
将每个唯一的蛋糕类型添加到cakeTypesForState
数组字段中。The window contains documents between an该unbounded
lower limit and thecurrent
document.window
包含介于unbounded
下限和current
文档之间的文档。This means这意味着$addToSet
returns an array containing the unique caketype
fields for the documents between the beginning of the partition and the current document.$addToSet
返回一个数组,该数组包含分区开头和当前文档之间文档的唯一蛋糕type
字段。
In this example output, the cake 在此示例输出中,type
array for CA
and WA
is shown in the cakeTypesForState
field:CA
和WA
的cakeTypesForState
字段中显示了cakeTypes
数组:
{ "_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" ] }