使用 $where 与嵌套字段进行比较

Bar*_*man 5 mongodb pymongo mongodb-query

我有一个 mongodb 集合,其中包含以下结构的文档(简化它,数字只是示例):

{'a' : 1, 'b' : {'c':2}}

我想运行以下 mongodb 查询:

{'$where' : 'this.a < this.b.c'}

以上是行不通的。这种查询的正确语法是什么?

Bar*_*man 4

发现问题:并非所有我的集合文档都包含“b”值,因此我收到错误: db.alerts.find({$where:"this.a < this.b.c"}) error: { "$err" : "TypeError: Cannot read property 'c' of undefined", "code" : 16722 }

通过将我的查询更改为: {"b.c":{$exists : true}, $where : "this.c < this.b.c"}