$setIsSubset (aggregation)
On this page本页内容
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, andfalse
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. 对数组执行set操作,将数组视为集合。If an array contains duplicate entries, 如果数组包含重复的条目,$setIsSubset
ignores the duplicate entries. $setIsSubset
将忽略重复的条目。$setIsSubset
ignores the order of the elements.忽略元素的顺序。
If a set contains a nested array element, 如果集合包含嵌套数组元素,则$setIsSubset
does not descend into the nested array but evaluates the array at top-level.$setIsSubset
不会下降到嵌套数组中,而是在顶级计算数组。
{ $setIsSubset: [ [ "a", "b", "a" ], [ "b", "a" ] ] } | true |
{ $setIsSubset: [ [ "a", "b" ], [ [ "a", "b" ] ] ] } | false |
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", "blue", "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 }