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

查看:29
本文介绍了检查字段是否存在于 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?

推荐答案

Use $ne (for "not equal")

Use $ne (for "not equal")

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

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

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