On this page本页内容
$setEquals
Compares two or more arrays and returns 比较两个或多个数组,如果它们具有相同的不同元素,则返回true if they have the same distinct elements and false otherwise.true,否则返回false。
$setEquals has the following syntax:语法如下:
{ $setEquals: [ <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.有关表达式的详细信息,请参阅表达式。
$setEquals performs set operation on arrays, treating arrays as sets. 对数组执行集合操作,将数组视为集合。If an array contains duplicate entries, 如果数组包含重复项,$setEquals ignores the duplicate entries. $setEquals将忽略重复项。$setEquals ignores the order of the elements.忽略元素的顺序。
If a set contains a nested array element, 如果集合包含嵌套数组元素,则$setEquals does not descend into the nested array but evaluates the array at top-level.$setEquals不会下降到嵌套数组中,而是在顶层计算数组。
{ $setEquals: [ [ "a", "b", "a" ], [ "b", "a" ] ] } | true |
{ $setEquals: [ [ "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 以下操作使用$setEquals operator to determine if the A array and the B array contain the same elements:$setEquals运算符确定A数组和B数组是否包含相同的元素:
db.experiments.aggregate(
[
{ $project: { A: 1, B: 1, sameElements: { $setEquals: [ "$A", "$B" ] }, _id: 0 } }
]
)
The operation returns the following results:该操作返回以下结果:
{ "A" : [ "red", "blue" ], "B" : [ "red", "blue" ], "sameElements" : true }
{ "A" : [ "red", "blue" ], "B" : [ "blue", "red", "blue" ], "sameElements" : true }
{ "A" : [ "red", "blue" ], "B" : [ "red", "blue", "green" ], "sameElements" : false }
{ "A" : [ "red", "blue" ], "B" : [ "green", "red" ], "sameElements" : false }
{ "A" : [ "red", "blue" ], "B" : [ ], "sameElements" : false }
{ "A" : [ "red", "blue" ], "B" : [ [ "red" ], [ "blue" ] ], "sameElements" : false }
{ "A" : [ "red", "blue" ], "B" : [ [ "red", "blue" ] ], "sameElements" : false }
{ "A" : [ ], "B" : [ ], "sameElements" : true }
{ "A" : [ ], "B" : [ "red" ], "sameElements" : false }