查找数组大小等于字段的位置

时间:2019-02-06 16:50:37

标签: node.js mongodb mongoose

我正在尝试查找数组大小等于字段值的所有文档。例如:

应找到此文档:

{
  arr: ["one", "two", "three"],
  expected: 3
}

但不是这个:

{
  arr: ["one", "two", "three", "four"],
  expected: 2
}

我假设我必须使用某种形式的聚合,所以我决定使用$expr

$expr: { $eq: [{ $size: "$arr" }, { ??? }] }

1 个答案:

答案 0 :(得分:1)

与在arr字段中使用$符号一样,您必须在expected字段中使用

db.collection.find({ "$expr": { "$eq": [{ "$size": "$arr" }, "$expected"] })
相关问题