Different query operators in MongoDB treat null values differently.

The examples on this page use the db.collection.find() method in the mongo shell. To populate the users collection referenced in the examples, run the following in mongo shell:

db.users.insert(
[
{ "_id" : 900, "name" : null },
{ "_id" : 901 }
]
)

Equality Filter

The { name : null } query matches documents that either contain the name field whose value is null or that do not contain the name field.

Given the following query:

db.users.find( { name: null } )

The query returns both documents:

{ "_id" : 900, "name" : null }
{ "_id" : 901 }

If the query uses an index that is sparse, however, then the query will only match null values, not missing fields.

Changed in version 2.6: If using the sparse index results in an incomplete result, MongoDB will not use the index unless a hint() explicitly specifies the index. See Sparse Indexes for more information.

Type Check

The { name : { $type: 10 } } query matches documents that contains the name field whose value is null only; i.e. the value of the item field is of BSON Type Null (i.e. 10) :

db.users.find( { name : { $type: 10 } } )

The query returns only the document where the item field has a null value:

{ "_id" : 900, "name" : null }

Existence Check

The { name : { $exists: false } } query matches documents that do not contain the item field:

db.users.find( { name : { $exists: false } } )

The query returns only the document that does not contain the item field:

{ "_id" : 901 }

SEE ALSO: The reference documentation for the $type and $exists operators.

MongoDB - MongoDB CRUD Operations, Query Documents, Query for Null or Missing Fields的更多相关文章

  1. MongoDB - MongoDB CRUD Operations, Delete Documents

    Delete Methods MongoDB provides the following methods to delete documents of a collection: Method De ...

  2. MongoDB - MongoDB CRUD Operations, Insert Documents

    MongoDB provides the following methods for inserting documents into a collection: db.collection.inse ...

  3. MongoDB - MongoDB CRUD Operations, Update Documents

    Update Methods MongoDB provides the following methods for updating documents in a collection: Method ...

  4. Mongodb系列- CRUD操作介绍

    ---恢复内容开始--- 一 Create 操作 在MongoDB中,插入操作的目标是一个集合. MongoDB中的所有写入操作在单个文档的层次上都是原子的. For examples, see In ...

  5. MongoDB - MongoDB CRUD Operations

    CRUD operations create, read, update, and delete documents. Create Operations Create or insert opera ...

  6. MongoDB - MongoDB CRUD Operations, Bulk Write Operations

    Overview MongoDB provides clients the ability to perform write operations in bulk. Bulk write operat ...

  7. MongoDB的CRUD操作

    1. 前言 在上一篇文章中,我们介绍了MongoDB.现在,我们来看下如何在MongoDB中进行常规的CRUD操作.毕竟,作为一个存储系统,它的基本功能就是对数据进行增删改查操作. MongoDB中的 ...

  8. .NET中MongoDB之CRUD

    参考文档 https://docs.mongoing.com/mongodb-crud-operations https://docs.mongodb.com/manual/crud/ https:/ ...

  9. springboot连接mongodb进行CRUD

    springboot连接mongodb进行CRUD的过程: 在执行以下操作前已安装了mongodb并创建了用户和数据库,使用Robo 3T可成功连接. 1.创建springboot项目,加入以下mav ...

随机推荐

  1. Java微笔记(6)

  2. linux 虚拟网络模型介绍

    第一种隔离模型          每一个虚拟机实例的网卡都有两个接口,一端接在虚拟机内部,一端接在宿主机内部,如上图所示eth0就是接在虚拟机内部的,而vnet0就是接在宿主机内部的,只要再创建一个虚 ...

  3. 虚拟机下安装CentOS6.5系统教程

    虚拟机下安装CentOS6.5系统教程 时间:2014-12-09 01:40来源:linuxdown.net 作者:linuxdown.net 举报 点击:15315次 其实通过VM安装虚拟机还是蛮 ...

  4. WordPress使用淘宝IP地址库的API显示评论者的位置信息(二)

    1 淘宝IP地址库的接口说明 在上一篇文章<WordPress使用淘宝IP地址库的API显示评论者的位置信息(一)>中,vfhky使用了新浪工具提供的这个IP接口显示博客评论者的位置信息. ...

  5. web_config配置

    <configuration>    <system.web>      <compilation debug="true" targetFramew ...

  6. 框架整合小小总结【SSH】注解式

    Spring 注解式注册 bean: 大致分为以下几步: 开启 context 空间支持 开启自动扫描功能,指定扫描包路径 使用注解配置 bean (使用@Component 注解) 给 bean 注 ...

  7. iOS--开发从入门到精通

    前言: 从事iOS开发已有几个年头,平时对于iOS开发的知识积累都比较碎片化,为了更好的掌握开发技能, 索性整理iOS开发的知识体系,以便于后面进阶成iOS高级开发工程师. 一.iOS开发基础 开发设 ...

  8. SDOI2017 解题报告

    数字表格 \(T\)次询问,每次给出\(n,m(n,m\le 10^6)\),\(f\)为斐波那契数列,\(f_0=0,f_1=1\),求: \[ \prod _{i=1}^n\prod _{j=1} ...

  9. 秒杀多线程第七篇 经典线程同步 互斥量Mutex(续)

    java使用Synchronized关键字实现互斥,而同时有Lock支持. 这两个的效果是等同的,Synchronized性能的起伏较大,而lock比较收敛. 为了代码的可读性,Synchronize ...

  10. [洛谷P4626]一道水题 II

    题目大意:求$lcm(1,2,3,\cdots,n)\pmod{100000007}$,$n\leqslant10^8$ 题解:先线性筛出质数,然后求每个质数最多出现的次数,可以用$\log_in$来 ...