我有一个 mongodb 集合,其中包含以下结构的文档(简化它,数字只是为了举例):
{'a' : 1, 'b' : {'c':2}}
我想运行以下 mongodb 查询:
{'$where' : 'this.a < this.b.c'}
上述方法无效。 此类查询的正确语法是什么?
最佳答案
发现问题:并非我所有的收藏文档都包含“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"}
关于mongodb - 使用 $where 与嵌套字段进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27254686/