Database Manual / Reference / Query Language / Expressions

$setUnion

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不会下降到嵌套数组中,而是在顶级计算数组。

Example示例Result结果Notes备注
{ $setUnion: [
[ "a", "c" ],
[ "a", "b" ]
] }
[ "a", "c", "b" ]
All elements from both arrays are combined and duplicate elements removed.将合并两个数组中的所有元素,并删除重复的元素。
{ $setUnion: [
[ "a", "c" ],
[ "a", "b", "c" ]
] }
[ "a", "c", "b" ]
All elements from both arrays are combined and duplicate elements removed.将合并两个数组中的所有元素,并删除重复的元素。
{ $setUnion: [
[ "a", "b", "a" ],
[ "b", "a" ]
] }
[ "a", "b" ]
All elements from both arrays are combined and duplicate elements removed.将合并两个数组中的所有元素,并删除重复的元素。
{ $setUnion: [
[ "a", "b" ],
[ [ "a", "b" ] ]
] }
[ "a", "b", [ "a", "b" ] ]
The result includes "a" and "b" as individual string elements and the array [ "a", "b" ] as a unique element.结果包括"a""b"作为单独的字符串元素,数组[ "a", "b" ]作为唯一元素。
{ $setUnion: [
[ ],
[ "a", "b" ]
] }
[ "a", "b" ]
The result consists of the unique elements in the second array because the first array is empty.结果由第二个数组中的唯一元素组成,因为第一个数组是空的。
{ $setUnion: [
[ "a", "a" ],
[ "a", "b" ]
] }
[ "a", "b" ]
All elements from both arrays are combined and duplicate elements removed.将合并两个数组中的所有元素,并删除重复的元素。

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局限性

  • $setUnion only supports arrays and expressions that resolve to an array.仅支持解析为数组的数组和表达式。
  • $setUnion does not guarantee the order of the elements in the output array.不保证输出数组中元素的顺序。

Learn More了解更多