On this page本页内容
$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:语法如下所示:
The arguments can be any valid expression as long as they each resolve to an array. 参数可以是任何有效的表达式,只要它们各自解析为一个数组。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
$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
不会下降到嵌套数组中,而是在顶层计算数组。
Result | |
---|---|
Consider an 考虑实验收集下列文件:experiments
collection with the following documents:
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
数组中找不到的元素数组:
The operation returns the following results:操作返回以下结果: