为什么我不能在 Mongo 管道匹配中使用 $nin、$exists 等?

Tom*_*omb 4 mongodb node.js aggregation-framework

对于这个答案,我上下看了好几遍,但没有任何效果。我有一个带有匹配项的管道查询,如下所示:

$match: {
  $expr: {
    $and: [
      ....
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

在 $and 我有各种条件,使用 $eq、$in、$ne、$gt、$lt 等。

但是尽我所能,我无法让它识别 $nin 或 $exists。我正在尝试添加一个术语,用于搜索不存在的密钥,例如:

{ $exists: [ '$key', 0 ] }
Run Code Online (Sandbox Code Playgroud)

我不断得到

MongoError:无法识别的表达式“$exists”

MongoError:无法识别的表达式“$nin”

有人可以帮忙吗??

Ash*_*shh 5

您只能aggregation$exprand 中使用运算符,$nin并且$exists是查询运算符而不是运算符aggregation。在$expr表达式之外使用上述条件。

就像是

{ "$match": {
  "key": { "$exists": true },
  "$expr": {
    "$and": [
      {...}, {...}
    ]
  }
}}
Run Code Online (Sandbox Code Playgroud)