我正在尝试在MongoDB中显示一个文本字段不是''(空白)的查询
{ 'name' : { $not : '' }}
Run Code Online (Sandbox Code Playgroud)
但是我得到了错误 invalid use of $not
我查看了文档,但他们使用的示例是针对复杂的情况(使用regexp和$not否定另一个运算符).
我怎么做我想做的简单的事情?
om-*_*nom 135
使用$ne- $not应该遵循标准运算符:
一个例子$ne,代表不相等:
use test
switched to db test
db.test.insert({author : 'me', post: ""})
db.test.insert({author : 'you', post: "how to query"})
db.test.find({'post': {$ne : ""}})
{ "_id" : ObjectId("4f68b1a7768972d396fe2268"), "author" : "you", "post" : "how to query" }
Run Code Online (Sandbox Code Playgroud)
而现在$not,它接受谓词($ne)并否定它($not):
db.test.find({'post': {$not: {$ne : ""}}})
{ "_id" : ObjectId("4f68b19c768972d396fe2267"), "author" : "me", "post" : "" }
Run Code Online (Sandbox Code Playgroud)
Mar*_*cny 61
如果你想做多个$ne那么做
db.users.find({name : {$nin : ["mary", "dick", "jane"]}})
Run Code Online (Sandbox Code Playgroud)
Kem*_*lah 33
用$ne而不是$not
http://docs.mongodb.org/manual/reference/operator/ne/#op._S_ne
db.collections.find({"name": {$ne: ""}});
Run Code Online (Sandbox Code Playgroud)
从Mongo文档:
该
$not操作员仅影响其他操作员,不能独立检查字段和文档。因此,请使用$not运算符进行逻辑析取,然后使用该运算$ne符直接测试字段的内容。
由于您正在直接测试该字段,因此$ne是在此处使用的正确操作员。
编辑:
您要使用的情况$not是:
db.inventory.find( { price: { $not: { $gt: 1.99 } } } )
Run Code Online (Sandbox Code Playgroud)
那将选择所有文档,其中:
| 归档时间: |
|
| 查看次数: |
90407 次 |
| 最近记录: |