Docs HomeMongoDB Manual

$setUnion (aggregation)

On this page本页内容

Definition定义

$setUnion

Takes two or more arrays and returns an array containing the elements that appear in any input array.获取两个或多个数组,并返回一个数组,该数组包含出现在任何输入数组中的元素。

$setUnion has the following syntax:具有以下语法:

{ $setUnion: [ <expression1>, <expression2>, ... ] }

The arguments can be any valid expression as long as they each resolve to an array. 参数可以是任何有效的表达式,只要它们各自解析为一个数组即可。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Behavior行为

$setUnion performs set operation on arrays, treating arrays as sets. 对数组执行set操作,将数组视为集合。If an array contains duplicate entries, $setUnion ignores the duplicate entries. 如果数组包含重复的条目,$setUnion将忽略重复的条目。$setUnion ignores the order of the elements.忽略元素的顺序。

$setUnion filters out duplicates in its result to output an array that contain only unique entries. 筛选掉结果中的重复项,以输出仅包含唯一项的数组。The order of the elements in the output array is unspecified.输出数组中元素的顺序未指定。

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结果
{ $setUnion: [ [ "a", "b", "a" ], [ "b", "a" ] ] }
[ "b", "a" ]
{ $setUnion: [ [ "a", "b" ], [ [ "a", "b" ] ] ] }
[ [ "a", "b" ], "b", "a" ]
Note

Starting in MongoDB 5.2, the sort order for $setUnion is undefined. 从MongoDB 5.2开始,$setUnion的排序顺序是未定义的。To sort an array, refer to $sortArray.要对数组进行排序,请参阅$sortArray

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" ] }