Docs HomeMongoDB Manual

$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, 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. 对数组执行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不会下降到嵌套数组中,而是在顶级计算数组。

Example示例Result结果
{ $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 }