Definition定义
$setIsSubsetTakes two arrays and returns取两个数组,当第一个数组是第二个数组的子集时,包括当第一个数组等于第二个数组时,返回truewhen the first array is a subset of the second, including when the first array equals the second array, andfalseotherwise.true,否则返回false。$setIsSubsethas 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不会下降到嵌套数组中,而是在顶级计算数组。
| false | c is not present in the second array.c。 |
| true | |
| true | ["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,无论重复元素或其顺序如何。 |
| false | [ [ "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。 |
| true | |
| true |
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 }