Database Manual / Reference / Query Language / Expressions

$setIsSubset (aggregation)(聚合)

Definition定义

$setIsSubset

Takes two arrays and returns true when the first array is a subset of the second, including when the first array equals the second array, and false otherwise.取两个数组,当第一个数组是第二个数组的子集时,包括当第一个数组等于第二个数组时,返回true,否则返回false

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

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

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

If a set contains a nested array element, $setIsSubset does not descend into the nested array but evaluates the array at top-level.如果一个集合包含嵌套数组元素,则$setIsSubset不会下降到嵌套数组中,而是在顶级计算数组。

Example示例Result结果Notes备注
{ $setIsSubset: [ [ "a", "c" ], [ "a", "b" ] ] }
falseNot a subset, because c is not present in the second array.不是子集,因为第二个数组中不存在c
{ $setIsSubset: [ [ "a", "c" ], [ "a", "b", "c" ] ] }
trueBoth elements in the first array are present in the second array.第一数组中的两个元素都存在于第二数组中。
{ $setIsSubset: [ [ "a", "b", "a" ], [ "b", "a" ] ] }
trueOnly the unique elements of the first array ["a", "b"] are considered, so the first array is a subset of the second and the result is true, regardless of duplicate elements or their order.只考虑第一个数组["a", "b"]的唯一元素,因此第一个数组是第二个数组的子集,结果为true,无论重复元素或其顺序如何。
{ $setIsSubset: [ [ "a", "b" ], [ [ "a", "b" ] ] ] }
falseThe second array [ [ "a", "b" ] ] contains a single element, wich itself is an array. Therefore, the first array [ "a", "b" ] is not considered a subset of the second and the result is false.第二个数组[ [ "a", "b" ] ]包含一个元素,该元素本身就是一个数组。因此,第一个数组[ "a", "b" ]不被视为第二个数组的子集,结果为false
{ $setIsSubset: [ [ ], [ "a", "b" ] ] }
trueAn empty set is considered a subset of any set.空集被认为是任何集合的子集。
{ $setIsSubset: [ [ "a", "a" ], [ "a", "b" ] ] }
trueA set is a subset of itself. Duplicate elements do not affect subset evaluation.集合是其自身的子集。重复元素不会影响子集计算。

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 $setIsSubset operator to determine if the flowerFieldA array is a subset of the flowerFieldB array:以下操作使用$setIsSubset运算符来确定flowerFieldA数组是否是flowerFieldB数组的子集:

db.flowers.aggregate(
[
{ $project: { flowerFieldA:1, flowerFieldB: 1, AisSubset: { $setIsSubset: [ "$flowerFieldA", "$flowerFieldB" ] }, _id:0 } }
]
)

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

{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "rose", "orchid" ], "AisSubset" : true }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "orchid", "rose", "orchid" ], "AisSubset" : true }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "rose", "orchid", "jasmine" ], "AisSubset" : true }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "jasmine", "rose" ], "AisSubset" : false }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ ], "AisSubset" : false }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ [ "rose" ], [ "orchid" ] ], "AisSubset" : false }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ [ "rose", "orchid" ] ], "AisSubset" : false }
{ "flowerFieldA" : [ ], "flowerFieldB" : [ ], "AisSubset" : true }
{ "flowerFieldA" : [ ], "flowerFieldB" : [ "rose" ], "AisSubset" : true }