MongoDB不等于 [英] MongoDB not equal to

查看:795
本文介绍了MongoDB不等于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MongoDB中显示文本字段不是(空白)"的查询

I'm trying to display a query in MongoDB where a text field is not '' (blank)

{ 'name' : { $not : '' }}

但是我收到错误invalid use of $not

我查看了文档,但是它们使用的示例用于复杂的情况(使用regexp和$not否定另一个运算符).

I've looked over the documentation but the examples they use are for complicated cases (with regexp and $not negating another operator).

我将如何做我想做的简单事情?

How would I do the simple thing I'm trying to do?

推荐答案

使用$ne-$not之后应是标准运算符:

Use $ne -- $not should be followed by the standard operator:

$ne的示例,代表不相等:

An examples for $ne, which stands for not equal:

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" }

现在是$not,它接受谓词($ne)并将其取反($not):

And now $not, which takes in predicate ($ne) and negates it ($not):

db.test.find({'post': {$not: {$ne : ""}}})
{ "_id" : ObjectId("4f68b19c768972d396fe2267"), "author" : "me", "post" : "" }

这篇关于MongoDB不等于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆