我有一个mongodb集合,其中包含以下结构的文档(简化它,数字只是例如):
{'a':1,'b':{'c':2}}
我想运行以下mongodb查询:
{'$ where':'this.a< this.b.c'}
以上不起作用。 这种查询的正确语法是什么?
答案 0 :(得分:3)
发现问题:并非我的所有收藏文件都包含" 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"}