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

时间:2014-12-02 16:50:15

标签: mongodb pymongo mongodb-query

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

  

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

我想运行以下mongodb查询:

  

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

以上不起作用。 这种查询的正确语法是什么?

1 个答案:

答案 0 :(得分:3)

发现问题:并非我的所有收藏文件都包含&#34; b&#34;价值,因此我收到一个错误: 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"}