On this page本页内容
$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.有关表达式的详细信息,请参阅表达式。
$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
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
不会下降到嵌套数组中,而是在顶层计算数组。
{ $setUnion: [ [ "a", "b", "a" ], [ "b", "a" ] ] } | [ "b", "a" ] |
{ $setUnion: [ [ "a", "b" ], [ [ "a", "b" ] ] ] } | [ [ "a", "b" ], "b", "a" ] |
Starting in MongoDB 5.2, the sort order for 从MongoDB 5.2开始,$setUnion
is undefined. $setUnion
的排序顺序未定义。To sort an array, refer to 要对数组进行排序,请参阅$sortArray
.$sortArray
。
Consider an 考虑一个包含以下文档的experiments
collection with the following documents:experiments
集合:
{ "_id" : 1, "A" : [ "red", "blue" ], "B" : [ "red", "blue" ] } { "_id" : 2, "A" : [ "red", "blue" ], "B" : [ "blue", "red", "blue" ] } { "_id" : 3, "A" : [ "red", "blue" ], "B" : [ "red", "blue", "green" ] } { "_id" : 4, "A" : [ "red", "blue" ], "B" : [ "green", "red" ] } { "_id" : 5, "A" : [ "red", "blue" ], "B" : [ ] } { "_id" : 6, "A" : [ "red", "blue" ], "B" : [ [ "red" ], [ "blue" ] ] } { "_id" : 7, "A" : [ "red", "blue" ], "B" : [ [ "red", "blue" ] ] } { "_id" : 8, "A" : [ ], "B" : [ ] } { "_id" : 9, "A" : [ ], "B" : [ "red" ] }
The following operation uses the 以下操作使用$setUnion
operator to return an array of elements found in the A
array or the B
array or both:$setUnion
运算符返回在A
数组或B
数组或两者中找到的元素数组:
db.experiments.aggregate( [ { $project: { A:1, B: 1, allValues: { $setUnion: [ "$A", "$B" ] }, _id: 0 } } ] )
The operation returns the following results:该操作返回以下结果:
{ "A": [ "red", "blue" ], "B": [ "red", "blue" ], "allValues": [ "blue", "red" ] } { "A": [ "red", "blue" ], "B": [ "blue", "red", "blue" ], "allValues": [ "blue", "red" ] } { "A": [ "red", "blue" ], "B": [ "red", "blue", "green" ], "allValues": [ "blue", "red", "green" ] } { "A": [ "red", "blue" ], "B": [ "green", "red" ], "allValues": [ "blue", "red", "green" ] } { "A": [ "red", "blue" ], "B": [ ], "allValues": [ "blue", "red" ] } { "A": [ "red", "blue" ], "B": [ [ "red" ], [ "blue" ] ], "allValues": [ "blue", "red", [ "red" ], [ "blue" ] ] } { "A": [ "red", "blue" ], "B": [ [ "red", "blue" ] ], "allValues": [ "blue", "red", [ "red", "blue" ] ] } { "A": [ ], "B": [ ], "allValues": [ ] } { "A": [ ], "B": [ "red" ], "allValues": [ "red" ] }