Docs HomeMongoDB Manual

Query for Null or Missing Fields查询Null或缺少的字段

You can query for null or missing fields in MongoDB using the following methods:您可以使用以下方法在MongoDB中查询null或缺失字段:


➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples or select MongoDB Compass.


Different query operators in MongoDB treat null values differently.MongoDB中不同的查询运算符对null值的处理方式不同。

This page provides examples of operations that query for null values using the Collection.find() method in the MongoDB Node.js Driver.本页提供了使用MongoDB Node.js驱动程序中的Collection.find()方法查询null值的操作示例。

The examples on this page use the inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:此页面上的示例使用inventory集合。连接到MongoDB实例中的测试数据库,然后创建inventory集合:

Important

Use BsonNull.Value with the MongoDB C# driver to query for null or missing fields in MongoDB.在MongoDB C#驱动程序中使用BsonNull.Value可以查询MongoDB中的null或缺失字段。

await db.collection('inventory').insertMany([{ _id: 1, item: null }, { _id: 2 }]);

Equality Filter相等筛选器

The { item : null } query matches documents that either contain the item field whose value is null or that do not contain the item field.{ item : null }查询匹配包含值为nullitem字段不包含item字段的文档。

const cursor = db.collection('inventory').find({
item: null
});

The query returns both documents in the collection.查询返回集合中的两个文档。

Type Check类型检查

The { item : { $type: 10 } } query matches only documents that contain the item field whose value is null; i.e. the value of the item field is of BSON Type Null (BSON Type 10):{ item : { $type: 10 } }查询只匹配包含值为nullitem字段的文档;即,item字段的值为BSON类型Null(BSON类型10):

const cursor = db.collection('inventory').find({
item: { $type: 10 }
});

The query returns only the document where the item field has a value of null.查询仅返回项字段值为null的文档。

Existence Check存在性检查

The following example queries for documents that do not contain a field. 以下示例查询不包含字段的文档。[1]

The { item : { $exists: false } } query matches documents that do not contain the item field:{ item : { $exists: false } }查询与不包含item字段的文档匹配:

const cursor = db.collection('inventory').find({
item: { $exists: false }
});

The query only returns the document that does not contain the item field.查询仅返回不包含item字段的文档。

Tip

See also: 另请参阅:

Reference documentation for the $type and $exists operators.$type$exists运算符的参考文档。

[1] Starting in MongoDB 4.2, users can no longer use the query filter $type: 0 as a synonym for $exists:false. 从MongoDB 4.2开始,用户不能再使用查询筛选器$type:0作为$exists:false的同义词。To query for null or missing fields, see Query for Null or Missing Fields.若要查询null或缺少的字段,请参阅查询null或丢失的字段

Query for Null or Missing Fields with MongoDB Atlas使用MongoDB Atlas查询空字段或缺失字段

The example in this section uses the sample training dataset. 本节中的示例使用示例训练数据集To learn how to load the sample dataset into your MongoDB Atlas deployment, see Load Sample Data.要了解如何将示例数据集加载到MongoDB Atlas部署中,请参阅加载示例数据

To query for a null or missing field in MongoDB Atlas, follow these steps:要查询MongoDB Atlas中的null或缺失字段,请执行以下步骤:

1

Navigate to the collection.导航到集合。

  1. In the Atlas UI, click Database in the sidebar.Atlas UI中,单击侧边栏中的“数据库”。
  2. For the database deployment that contains the sample data, click Browse Collections.对于包含示例数据的数据库部署,请单击“浏览集合”。
  3. In the left navigation pane, select the sample_training database.在左侧导航窗格中,选择sample_training数据库。
  4. Select the companies collection.选择companies集合。
2

Insert a blank document.插入一个空白文档。

Click the Insert Document button to display the dialog box, and then click Insert to insert a document with only the _id field.单击“插入文档”按钮以显示对话框,然后单击“插入”以插入仅包含_id字段的文档。

3

Specify a query filter document.指定查询筛选器文档。

To find a document that contains a null or missing value, specify a query filter document in the Filter field. 若要查找包含null值或缺少值的文档,请在“筛选器”字段中指定查询筛选器文档A query filter document uses query operators to specify search conditions.查询筛选器文档使用查询运算符来指定搜索条件。

Different query operators in MongoDB treat null values differently. MongoDB中不同的查询运算符对null值的处理方式不同。To apply a query filter, copying each of the following documents into the Filter search bar and click Apply.若要应用查询筛选器,请将以下每个文档复制到“筛选器”搜索栏中,然后单击“应用”。

Use the following query filter to match documents that either contain a description field with a null value or do not contain the description field:使用以下查询筛选器匹配包含具有null值的description字段或不包含description字段的文档:

{ description : null }

Use the following query filter to match only documents that contain a description field with a null value. 使用以下查询筛选器仅匹配包含空值description字段的文档。This filter specifies that the value of the field must be BSON Type Null (BSON Type 10):此筛选器指定字段的值必须为BSON Type Null(BSON Type 10):

{ description : { $type: 10 } }

Use the following query filter to match only documents that do not contain the description field. 使用以下查询筛选器仅匹配不包含description字段的文档。Only the document that you inserted earlier should appear:仅应显示您先前插入的文档:

{ description : { $exists: false } }