Definition定义
Takes two or more arrays and returns a single array containing the unique elements that appear in any input array. 接受两个或多个数组,并返回一个包含任何输入数组中出现的唯一元素的数组。$setUnion can be used as an aggregation accumulator or an array operator.$setUnion可以用作聚合累加器或数组运算符
Aggregation Accumulator聚合累加器
$setUnion
$setUnion is available as an accumulator in these stages:在以下阶段可作为蓄能器使用:
Syntax语法
When used as an aggregation accumulator, 当用作聚合累加器时,$setUnion has the following syntax:$setUnion具有以下语法:
{ $setUnion: "<array field>" }Behavior行为
$setUnion performs set operation on arrays, treating arrays as sets. If an array contains duplicate entries, 对数组执行集合操作,将数组视为集合。如果数组包含重复条目,$setUnion ignores the duplicate entries. $setUnion会忽略重复条目。$setUnion ignores the order of the elements.$setUnion忽略元素的顺序。
$setUnion filters out duplicates in its result to output an array that contains only unique entries. The order of the elements in the output array is unspecified.$setUnion筛选掉结果中的重复项,以输出一个仅包含唯一条目的数组。输出数组中元素的顺序未指定。
If a set contains a nested array element, 如果一个集合包含嵌套数组元素,则$setUnion does not descend into the nested array but evaluates the array at top-level.$setUnion不会下降到嵌套数组中,而是在顶级计算数组。
Arguments that are 结果中包含null or resolve to null are included in the results. Arguments that refer to a field that is missing are not included in the results.null或解析为null的参数。引用缺失字段的参数不包括在结果中。
Example示例
Create a collection named 使用以下文档创建一个名为sales with the following documents:sales的集合:
db.sales.insertMany( [
{
_id: 1,
items: [ "laptop", "tablet" ],
location: "NYC"
},
{
_id: 2,
items: [ "phone", "tablet" ],
location: "NYC"
},
{
_id: 3,
location: "NYC"
},
{
_id: 4,
items: [ "desktop", { "accessories": [ "mouse", "keyboard"] } ],
location: "NYC"
}
] )
This example shows how you can use 此示例显示了如何将$setUnion as an accumulator. This example accumulates all the unique elements in the items arrays when grouping on the location field:$setUnion用作累加器。此示例在对位置字段进行分组时,会累积items数组中的所有唯一元素:
db.sales.aggregate( [
{
$group: {
_id: "$location",
array: { "$setUnion": "$items" }
}
}
] )
The operation returns the following result:该操作返回以下结果:
[
{
"_id": "NYC",
"array": [
"laptop", "tablet", "phone", "desktop", { "accessories": [ "mouse", "keyboard"] }
]
}
]Array Operator数组运算符
$setUnion
Syntax语法
When used as an array operator, 当用作数组运算符时,$setUnion has the following syntax:$setUnion具有以下语法:
{
$setUnion: [ <expression1>, <expression2>, ... ]
}Behavior行为
$setUnion performs set operation on arrays, treating arrays as sets. If an array contains duplicate entries, $setUnion ignores the duplicate entries. $setUnion对数组执行集合操作,将数组视为集合。如果数组包含重复条目,$setUnion会忽略重复条目。$setUnion ignores the order of the elements.$setUnion忽略元素的顺序。
$setUnion filters out duplicates in its result to output an array that contains only unique entries. The order of the elements in the output array is unspecified.$setUnion筛选掉结果中的重复项,以输出一个仅包含唯一条目的数组。输出数组中元素的顺序未指定。
The arguments can be any valid expression as long as they each resolve to an array. 参数可以是任何有效的表达式,只要它们各自解析为数组即可。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
If a set contains a nested array element, 如果一个集合包含嵌套数组元素,则$setUnion does not descend into the nested array but evaluates the array at top-level.$setUnion不会下降到嵌套数组中,而是在顶级计算数组。
|
| |
|
| |
|
| |
|
| "a" and "b" as individual string elements and the array [ "a", "b" ] as a unique element."a"和"b"作为单独的字符串元素,数组[ "a", "b" ]作为唯一元素。 |
|
| |
|
|
Example示例
Consider an 考虑一个有以下文件的flowers collection with the following documents:flowers集合:
db.flowers.insertMany( [
{ "_id" : 1, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "rose", "orchid" ] },
{ "_id" : 2, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "orchid", "rose", "orchid" ] },
{ "_id" : 3, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "rose", "orchid", "jasmine" ] },
{ "_id" : 4, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "jasmine", "rose" ] },
{ "_id" : 5, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ ] },
{ "_id" : 6, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ [ "rose" ], [ "orchid" ] ] },
{ "_id" : 7, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ [ "rose", "orchid" ] ] },
{ "_id" : 8, "flowerFieldA" : [ ], "flowerFieldB" : [ ] },
{ "_id" : 9, "flowerFieldA" : [ ], "flowerFieldB" : [ "rose" ] }
] )
The following operation uses the 以下操作使用$setUnion operator to return an array of elements found in the flowerFieldA array or the flowerFieldB array or both:$setUnion运算符返回在flowerFieldA数组或flowerFieldB数组或两者中找到的元素数组:
db.flowers.aggregate(
[
{ $project: { flowerFieldA:1, flowerFieldB: 1, allValues: { $setUnion: [ "$flowerFieldA", "$flowerFieldB" ] }, _id: 0 } }
]
)
The operation returns the following results:该操作返回以下结果:
{ "flowerFieldA": [ "rose", "orchid" ], "flowerFieldB": [ "rose", "orchid" ], "allValues": [ "orchid", "rose" ] }
{ "flowerFieldA": [ "rose", "orchid" ], "flowerFieldB": [ "orchid", "rose", "orchid" ], "allValues": [ "orchid", "rose" ] }
{ "flowerFieldA": [ "rose", "orchid" ], "flowerFieldB": [ "rose", "orchid", "jasmine" ], "allValues": [ "orchid", "rose", "jasmine" ] }
{ "flowerFieldA": [ "rose", "orchid" ], "flowerFieldB": [ "jasmine", "rose" ], "allValues": [ "orchid", "rose", "jasmine" ] }
{ "flowerFieldA": [ "rose", "orchid" ], "flowerFieldB": [ ], "allValues": [ "orchid", "rose" ] }
{ "flowerFieldA": [ "rose", "orchid" ], "flowerFieldB": [ [ "rose" ], [ "orchid" ] ], "allValues": [ "orchid", "rose", [ "rose" ], [ "orchid" ] ] }
{ "flowerFieldA": [ "rose", "orchid" ], "flowerFieldB": [ [ "rose", "orchid" ] ], "allValues": [ "orchid", "rose", [ "rose", "orchid" ] ] }
{ "flowerFieldA": [ ], "flowerFieldB": [ ], "allValues": [ ] }
{ "flowerFieldA": [ ], "flowerFieldB": [ "rose" ], "allValues": [ "rose" ] }Limitations局限性
$setUniononly supports arrays and expressions that resolve to an array.仅支持解析为数组的数组和表达式。$setUniondoes not guarantee the order of the elements in the output array.不保证输出数组中元素的顺序。