$setDifference (aggregation)

On this page本页内容

Definition定义

$setDifference

Takes two sets and returns an array containing the elements that only exist in the first set; i.e. performs a relative complement of the second set relative to the first.获取两个集合并返回一个数组,其中包含仅存在于第一个集合中的元素;即,执行第二组相对于第一组的相对补码

$setDifference has the following syntax:语法如下所示:

{ $setDifference: [ <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行为

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

$setDifference 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, $setDifference does not descend into the nested array but evaluates the array at top-level.如果集合包含嵌套数组元素,$setDifference不会下降到嵌套数组中,而是在顶层计算数组。

Example示例Result
{ $setDifference: [ [ "a", "b", "a" ], [ "b", "a" ] ] }
[ ]
{ $setDifference: [ [ "a", "b" ], [ [ "a", "b" ] ] ] }
[ "a", "b" ]

Example示例

Consider an experiments collection with the following documents:考虑实验收集下列文件:

{ "_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 $setDifference operator to return an array of elements found in the B array but not in the A array:以下操作使用$setDifference运算符返回在B数组中找到但在A数组中找不到的元素数组:

db.experiments.aggregate(
   [
     { $project: { A: 1, B: 1, inBOnly: { $setDifference: [ "$B", "$A" ] }, _id: 0 } }
   ]
)

The operation returns the following results:操作返回以下结果:

{ "A" : [ "red", "blue" ], "B" : [ "red", "blue" ], "inBOnly" : [ ] }
{ "A" : [ "red", "blue" ], "B" : [ "blue", "red", "blue" ], "inBOnly" : [ ] }
{ "A" : [ "red", "blue" ], "B" : [ "red", "blue", "green" ], "inBOnly" : [ "green" ] }
{ "A" : [ "red", "blue" ], "B" : [ "green", "red" ], "inBOnly" : [ "green" ] }
{ "A" : [ "red", "blue" ], "B" : [ ], "inBOnly" : [ ] }
{ "A" : [ "red", "blue" ], "B" : [ [ "red" ], [ "blue" ] ], "inBOnly" : [ [ "red" ], [ "blue" ] ] }
{ "A" : [ "red", "blue" ], "B" : [ [ "red", "blue" ] ], "inBOnly" : [ [ "red", "blue" ] ] }
{ "A" : [ ], "B" : [ ], "inBOnly" : [ ] }
{ "A" : [ ], "B" : [ "red" ], "inBOnly" : [ "red" ] }