mongodb - mongodb中类型不相等的查询

标签 mongodb

我正在执行以下查询,该查询完美运行,并为我带来了符合类型过滤器的文档部分

db.users.find({phoneNumber:/^\+/, devices:{$exists:true, $type:3}});

但是我试图获取所有存在但不是类型 3(对象)的设备,我可以尝试每种类型 [http://docs.mongodb.org/manual/reference/operator/query/type/] ,但想知道是否可以否定我尝试过的类型 3

db.users.count({phoneNumber:/^\+/, devices:{$exists:true, $type: { $neq: 3}}});
db.users.count({phoneNumber:/^\+/, devices:{$exists:true, $type: { $not: 3}}});
db.users.count({phoneNumber:/^\+/, devices:{$exists:true, $type: { $not: {$eq:3}}}});

但是它们都抛出相同的异常

exception: type not supported for appendMinElementForType

关于如何进行此查询的任何线索?

最佳答案

这应该可以按预期工作:

{phoneNumber:/^\+/ ,$and:[{devices:{$exists:true}},{devices:{$not:{$type:3}}}]}

mongodb 文档中的这个注释可能有助于了解这里发生了什么(复制自 MongoDB reference docs - $and operator manual page - 重点是我的):

MongoDB provides an implicit AND operation when specifying a comma separated list of expressions. Using an explicit AND with the $and operator is necessary when the same field or operator has to be specified in multiple expressions.

关于mongodb - mongodb中类型不相等的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20744123/

相关文章:

python - 字典数据提取问题

mongodb - 如何在 mongo JavaScript shell 中中止查询

node.js - MongoDB-查询优化

mongodb - 无法使用 MongoClientSettings 作为 MongoClient 的参数连接到 MongoDb

ruby-on-rails - 使用 Rspec 和 Mongoid 5.0 on Rails 4 清理或重置测试数据库

node.js - Mongodb数据库连接代码破坏了应用程序

node.js - Mongoose /keystonejs : unique field except if field is empty

mongodb - 如何使用mongodb对数据进行分组?

ruby ,Mongodb, Anemone : web crawler with possible memory leak?

linux - 脚本 linux mongodb 错误 SyntaxError : Unexpected [