发现其中阵列大小等于场
我想找到的所有文件,其中一个数组的大小等于一个字段的值。例如:
这份文件应该发现:
{
arr: ["one", "two", "three"],
expected: 3
}
但没有这一项:
{
arr: ["one", "two", "three", "four"],
expected: 2
}
我假设我必须使用某种形式的聚集,所以我决定用$expr
:
$expr: { $eq: [{ $size: "$arr" }, { ??? }] }
回答如下:
同样,你使用arr
标志使用的$
领域,你必须使用expected
场
db.collection.find({ "$expr": { "$eq": [{ "$size": "$arr" }, "$expected"] })