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

标签 mongodb pymongo mongodb-query

我有一个 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/

相关文章:

MongoDb 文本搜索不返回结果

javascript - 如何根据特定 id 向 mLab 数组插入新项目

node.js - 将文档添加到文档(子文档)不起作用

javascript - 具有查找功能的 mongodb 聚合

json - MongoDB、NodeJS : updating an embedded document with new members

python - 如何远程登录 mongodb(通过 pymongo)并获取 db.serverStatus() 的输出

python - gridfs "list"方法返回具有非空集合的空列表

mongodb - 将 2 个 mongoDB 集合中的数据合并到 1 个文档中

java - 在投影中对大小数组进行除法运算来编写聚合查询的方法

Go 中的 MongoDB 与 JSON 和 BSON 的关系