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.

时间: 2024-08-27 13:57:28

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

MongoDB - MongoDB CRUD Operations, Update Documents

Update Methods MongoDB provides the following methods for updating documents in a collection: Method Description  db.collection.updateOne() Updates at most a single document that match a specified filter even though multiple documents may match the s

MongoDB - MongoDB CRUD Operations

CRUD operations create, read, update, and delete documents. Create Operations Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection. MongoDB provides th

MongoDB - MongoDB CRUD Operations, Bulk Write Operations

Overview MongoDB provides clients the ability to perform write operations in bulk. Bulk write operations affect a singlecollection. MongoDB allows applications to determine the acceptable level of acknowledgement required for bulk write operations. N

MongoDB的CRUD操作

1. 前言 在上一篇文章中,我们介绍了MongoDB.现在,我们来看下如何在MongoDB中进行常规的CRUD操作.毕竟,作为一个存储系统,它的基本功能就是对数据进行增删改查操作. MongoDB中的增删改查操作,不同于我们熟悉的关系数据库中的操作.在关系数据库中,比如MySQL,我们通常使用SQL语句对数据库进行增(INSERT)删(DELETE)改(UPDATE)查(SELECT).MongoDB在对数据进行操作过程中,使用的是Document进行数据操作.在对数据库进行操作的时候,使用Do

springboot连接mongodb进行CRUD

springboot连接mongodb进行CRUD的过程: 在执行以下操作前已安装了mongodb并创建了用户和数据库,使用Robo 3T可成功连接. 1.创建springboot项目,加入以下maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </

MongoDB C# CRUD (3)

题记:媳妇要求发上来.这篇我要上首页. 1,NuGet引入MongoDB.接着是命名空间 using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Builders; using MongoDB.Driver.Linq; 2,全局声明 MongoDatabase mongo = new MongoClient(ConfigurationManager.AppSettings["PSConnStrMongoDB"]).G

Ruby操作MongoDB(进阶)-CRUD操作

MongDB数据库的使用离不开CRUD操作.什么是CRUD,就是创建文档,读取文档信息,更新文档和删除文档. key-value键值对标记 在MongoDB的Ruby驱动中,Key_value键值多次出现.而且有时会出现语法上的巧合,这取决于在使用的Ruby版本中如何申明. 在文档创建步骤中,1.9及之后版本支持以下语法: document={name:"Tom",age:20}. 但是如果你使用的是2.2或者更高的版本,你可以用双引号将你的key包起来.如: document={&q

[MongoDB]MongoDB与JAVA结合使用CRUD

汇总: 1. [MongoDB]安装MongoDB2. [MongoDB]Mongo基本使用:3. [MongoDB]MongoDB的优缺点及与关系型数据库的比较4. [MongoDB]MongoDB与JAVA结合使用CRUD 使用起来也相当简单,由于MongoDB是类文件的数据库,所以其操作起来非常方便 首先下载相应的jar包,这里我直接使用Maven自动获取,以下为POM文件中的配置: <dependency> <groupId>junit</groupId> &l

MongoDB - Introduction to MongoDB, MongoDB Extended JSON

JSON can only represent a subset of the types supported by BSON. To preserve type information, MongoDB adds the following extensions to the JSON format: Strict mode. Strict mode representations of BSON types conform to the JSON RFC. Any JSON parser c