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. 用python脚本计算某一个文件的行数

    python可以统计文件的行数,你相信吗?不管你信不信反正我信了.下面我们来看一下python怎样统计文件的行数,代码很简单,我也做了注释,很简单的实现... 1 2 3 4 5 6 7 8 9 10 ...

  2. Strust2: 工作流程

    以下为Struts2的体系结构图: Struts2框架处理用户请求,大体分为以下几个过程: (1)用户发出一个HttpServletRequest请求 (2)请求经过一系列过滤器,最后达到Filter ...

  3. 第17章 程序管理与SELinux初探

    什么是进程 触发任何一个事件时,系统都会将它定义为一个进程,并且给予这个进程一个ID,称为PID,同时依据触发这个进程的用户与相关属性关系,给予这个进程一组有效的权限设置. 进程与程序 进程:执行一个 ...

  4. Guide: Solr performance tuning--转载

    原文地址:http://h3x.no/2011/05/10/guide-solr-performance-tuning Introduction I have for the last year be ...

  5. 【BZOJ3122】随机数生成器(BSGS,数论)

    [BZOJ3122]随机数生成器(BSGS,数论) 题面 BZOJ 洛谷 题解 考虑一下递推式 发现一定可以写成一个 \(X_{i+1}=(X_1+c)*a^i-c\)的形式 直接暴力解一下 \(X_ ...

  6. 【CF528D】Fuzzy Search(FFT)

    [CF528D]Fuzzy Search(FFT) 题面 给定两个只含有\(A,T,G,C\)的\(DNA\)序列 定义一个字符\(c\)可以被匹配为:它对齐的字符,在距离\(K\)以内,存在一个字符 ...

  7. 最长上升子序列nlogn算法

    LIS问题是经典的动态规划问题,它的状态转移相信大家都很熟悉: f[i] = f[k] + 1  (k < i 且 A[k] < A[i]) 显然这样做复杂度是O(n^2) 有没有更快的算 ...

  8. SQL Server参数化SQL语句中的like和in查询的语法(C#)

    sql语句进行 like和in 参数化,按照正常的方式是无法实现的 我们一般的思维是: Like参数化查询:string sqlstmt = "select * from users whe ...

  9. bzoj5055: 膜法师(BIT)

    大水题WA了两发T T 记录一下a[i]的前缀和,a[i]*a[j]就是sigma(a[j]*sumi[j-1]) 记录一下a[i]*a[j]的前缀和,a[i]*a[j]*a[k]就是sigma(a[ ...

  10. 【loj2461】【2018集训队互测Day 1】完美的队列

    #2461. 「2018 集训队互测 Day 1」完美的队列 传送门: https://loj.ac/problem/2461 题解: 直接做可能一次操作加入队列同时会弹出很多数字,无法维护:一个操作 ...