Definition定义
$setDifferenceTakes 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.取两个集合,并返回一个数组,其中包含仅存在于第一个集合中的元素;即,执行第二组相对于第一组的相对补码。$setDifferencehas 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 ignores the order of the elements.$setDifference会忽略重复条目。$setDifference忽略元素的顺序。
$setDifference 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.筛选结果中的重复项,以输出仅包含唯一项的数组。输出数组中元素的顺序未指定。
If a set contains a nested array element, 如果一个集合包含嵌套数组元素,则$setDifference does not descend into the nested array but evaluates the array at top-level.$setDifference不会下降到嵌套数组中,而是在顶级计算数组。
|
| "a" is present in both arrays, so it is removed from the result. "c" is only present in the first array."a"存在于两个数组中,因此它将从结果中删除。"c"仅存在于第一个数组中。 |
|
| "a" and "c" are present in both arrays so they are removed from the result. No more elements remain in the first array."a"和"c"都存在于两个数组中,因此它们会从结果中删除。第一个数组中不再有元素。 |
|
| "a" and "b" are present in both arrays so they are removed from the result. Duplicates are also removed. No more elements remain in the first array."a"和"b"都存在于两个数组中,因此它们会从结果中删除。重复项也会被删除。第一个数组中不再有元素。 |
|
| "a" and "b" from the first array are not found in the second array, which contains a nested array."a"和"b"。 |
|
| |
|
| "a" is present in the second array so it is removed from the result. Duplicates are also removed. No more elements remain in the first array."a"存在于第二个数组中,因此将其从结果中删除。重复项也会被删除。第一个数组中不再有元素。 |
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 以下操作使用$setDifference operator to return an array of elements found in the flowerFieldB array but not in the flowerFieldA array:$setDifference运算符返回在flowerFieldB数组中但在flowerFieldA数组中找不到的元素数组:
db.flowers.aggregate(
[
{ $project: { flowerFieldA: 1, flowerFieldB: 1, inBOnly: { $setDifference: [ "$flowerFieldB", "$flowerFieldA" ] }, _id: 0 } }
]
)
The operation returns the following results:该操作返回以下结果:
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "rose", "orchid" ], "inBOnly" : [ ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "orchid", "rose", "orchid" ], "inBOnly" : [ ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "rose", "orchid", "jasmine" ], "inBOnly" : [ "jasmine" ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "jasmine", "rose" ], "inBOnly" : [ "jasmine" ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ ], "inBOnly" : [ ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ [ "rose" ], [ "orchid" ] ], "inBOnly" : [ [ "rose" ], [ "orchid" ] ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ [ "rose", "orchid" ] ], "inBOnly" : [ [ "rose", "orchid" ] ] }
{ "flowerFieldA" : [ ], "flowerFieldB" : [ ], "inBOnly" : [ ] }
{ "flowerFieldA" : [ ], "flowerFieldB" : [ "rose" ], "inBOnly" : [ "rose" ] }