检查MongoDB是否存在该字段 [英] Check that Field Exists with MongoDB

查看:150
本文介绍了检查MongoDB是否存在该字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图查找所有具有字段集并且不为空的记录.

So I'm attempting to find all records who have a field set and isn't null.

我尝试使用$exists,但是根据 MongoDB文档,此查询将返回等于null的字段.

I try using $exists, however according to the MongoDB documentation, this query will return fields who equal null.

$exists与包含存储空值的字段的文档匹配.

$exists does match documents that contain the field that stores the null value.

所以我现在假设我必须做这样的事情:

So I'm now assuming I'll have to do something like this:

db.collection.find({ "fieldToCheck" : { $exists : true, $not : null } })

但是,每当我尝试此操作时,都会收到错误消息[invalid use of $not]任何人都知道如何查询此消息吗?

Whenever I try this however, I get the error [invalid use of $not] Anyone have an idea of how to query for this?

推荐答案

使用$ne(表示不等于")

db.collection.find({ "fieldToCheck": { $exists: true, $ne: null } })

这篇关于检查MongoDB是否存在该字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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