MongoDB - MongoDB CRUD Operations, Query Documents, Query for Null or Missing Fields
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的更多相关文章
- MongoDB - MongoDB CRUD Operations, Delete Documents
Delete Methods MongoDB provides the following methods to delete documents of a collection: Method De ...
- MongoDB - MongoDB CRUD Operations, Insert Documents
MongoDB provides the following methods for inserting documents into a collection: db.collection.inse ...
- MongoDB - MongoDB CRUD Operations, Update Documents
Update Methods MongoDB provides the following methods for updating documents in a collection: Method ...
- Mongodb系列- CRUD操作介绍
---恢复内容开始--- 一 Create 操作 在MongoDB中,插入操作的目标是一个集合. MongoDB中的所有写入操作在单个文档的层次上都是原子的. For examples, see In ...
- MongoDB - MongoDB CRUD Operations
CRUD operations create, read, update, and delete documents. Create Operations Create or insert opera ...
- MongoDB - MongoDB CRUD Operations, Bulk Write Operations
Overview MongoDB provides clients the ability to perform write operations in bulk. Bulk write operat ...
- MongoDB的CRUD操作
1. 前言 在上一篇文章中,我们介绍了MongoDB.现在,我们来看下如何在MongoDB中进行常规的CRUD操作.毕竟,作为一个存储系统,它的基本功能就是对数据进行增删改查操作. MongoDB中的 ...
- .NET中MongoDB之CRUD
参考文档 https://docs.mongoing.com/mongodb-crud-operations https://docs.mongodb.com/manual/crud/ https:/ ...
- springboot连接mongodb进行CRUD
springboot连接mongodb进行CRUD的过程: 在执行以下操作前已安装了mongodb并创建了用户和数据库,使用Robo 3T可成功连接. 1.创建springboot项目,加入以下mav ...
随机推荐
- 【dp】New Keyboard
http://codeforces.com/gym/101397 B dp[i][j][k]: i为前一个行动的状态,0-switch.1-type,j为当前状态layout的编号,k 是已键入的字符 ...
- QHash和QMultiHash使用
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QHash和QMultiHash使用 本文地址:http://techieliang. ...
- 【Leetcode】179. Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- tomcat下部署了多个项目启动报错java web error:Choose unique values for the 'webAppRootKey' context-param in your web.xml files
应该是tomcat下部署了多个项目且都使用log4j. <!--如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的"webapp.root". ...
- linux 转移mysql文件操作流程
1.现将mysql停服 2.将文件拷贝到指定目录cp ./sales_trade_2.ibd /db/data/mysql/data_warehouse/sales_trade_2.ibd 3.检查新 ...
- linux 上传下载
xshell yum就是傻瓜式的安装软件,你要装什么,yum什么就行了,红帽系统才有yum,乌班图和debian是没有的 输入命令:sudo yum -y install lrzsz rz 上传 从 ...
- 第86天:HTML5应用程序标签和智能表单
一.HTML5应用程序标签 1.datalist需要数据载体 input list属性指向数据源 2.progress进度条 -webkit-appearance: none; /*如果要改默认样 ...
- Notepad++查找和替换空行/空格/换行
Notepad++查找和替换支持正则表达式,功能很强大,但比较复杂因此暂不研究 Notepad++使用正则表达式查找,首先需要勾选查找/替换窗口左下部的“正则表达式(E)”\r\n表示换行,其中\r表 ...
- Codeforces Round #362(Div1) D Legen...(AC自动机+矩阵快速幂)
题目大意: 给定一些开心串,每个串有一个开心值,构造一个串,每包含一次开心串就会获得一个开心值,求最大获得多少开心值. 题解: 首先先建立AC自动机.(建立fail指针的时候,对val要进行累加) 然 ...
- Django文字教程
user-----URL对应关系-------视图函数def func1()-------------- 函数给用户返回的实质上就是一个字符串,过程:通过open函数打开HTML,把HTML读到内存中 ...