On this page本页内容
$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.有关表达式的详细信息,请参阅表达式。
$setIsSubset
performs set operation on arrays, treating arrays as sets. 对数组执行集合操作,将数组视为集合。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 |
Consider an 考虑一个包含以下文档的experiments
collection with the following documents:experiments
集合:
{ "_id" : 1, "A" : [ "red", "blue" ], "B" : [ "red", "blue" ] } { "_id" : 2, "A" : [ "red", "blue" ], "B" : [ "blue", "red", "blue" ] } { "_id" : 3, "A" : [ "red", "blue" ], "B" : [ "red", "blue", "green" ] } { "_id" : 4, "A" : [ "red", "blue" ], "B" : [ "green", "red" ] } { "_id" : 5, "A" : [ "red", "blue" ], "B" : [ ] } { "_id" : 6, "A" : [ "red", "blue" ], "B" : [ [ "red" ], [ "blue" ] ] } { "_id" : 7, "A" : [ "red", "blue" ], "B" : [ [ "red", "blue" ] ] } { "_id" : 8, "A" : [ ], "B" : [ ] } { "_id" : 9, "A" : [ ], "B" : [ "red" ] }
The following operation uses the 以下操作使用$setIsSubset
operator to determine if the A
array is a subset of the B
array:$setIsSubset
运算符确定A
数组是否为B
数组的子集:
db.experiments.aggregate( [ { $project: { A:1, B: 1, AisSubset: { $setIsSubset: [ "$A", "$B" ] }, _id:0 } } ] )
The operation returns the following results:该操作返回以下结果:
{ "A" : [ "red", "blue" ], "B" : [ "red", "blue" ], "AisSubset" : true } { "A" : [ "red", "blue" ], "B" : [ "blue", "red", "blue" ], "AisSubset" : true } { "A" : [ "red", "blue" ], "B" : [ "red", "blue", "green" ], "AisSubset" : true } { "A" : [ "red", "blue" ], "B" : [ "green", "red" ], "AisSubset" : false } { "A" : [ "red", "blue" ], "B" : [ ], "AisSubset" : false } { "A" : [ "red", "blue" ], "B" : [ [ "red" ], [ "blue" ] ], "AisSubset" : false } { "A" : [ "red", "blue" ], "B" : [ [ "red", "blue" ] ], "AisSubset" : false } { "A" : [ ], "B" : [ ], "AisSubset" : true } { "A" : [ ], "B" : [ "red" ], "AisSubset" : true }