Query for Null or Missing Fields查询Null
或缺少的字段
On this page本页内容
You can query for 您可以使用以下方法在MongoDB中查询null
or missing fields in MongoDB using the following methods:null
或缺失字段:
Your programming language's driver.您的编程语言的驱动程序。The MongoDB Atlas UI. To learn more, see Query for Null or Missing Fields with MongoDB Atlas.MongoDB Atlas UI。要了解更多信息,请参阅使用MongoDB Atlas查询Null
或缺失字段。- MongoDB Compass.
➤ 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 MongoDB中不同的查询运算符对null
values differently.null
值的处理方式不同。
This page provides examples of operations that query for 本页提供了使用MongoDB Node.js驱动程序中的null
values using the Collection.find() method in the MongoDB Node.js Driver.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
集合:
Use 在MongoDB C#驱动程序中使用BsonNull.Value
with the MongoDB C# driver to query for null
or missing fields in MongoDB.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 }
查询匹配包含值为null
的item
字段或不包含item
字段的文档。
const cursor = db.collection('inventory').find({
item: null
});
The query returns both documents in the collection.查询返回集合中的两个文档。
Type Check类型检查
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
字段的文档。
[1] | $type: 0 as a synonym for $exists:false . $type:0 作为$exists:false 的同义词。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 要查询MongoDB Atlas中的null
or missing field in MongoDB Atlas, follow these steps:null
或缺失字段,请执行以下步骤:
Navigate to the collection.导航到集合。
In the Atlas UI, click Database in the sidebar.在Atlas UI中,单击侧边栏中的“数据库”。For the database deployment that contains the sample data, click Browse Collections.对于包含示例数据的数据库部署,请单击“浏览集合”。In the left navigation pane, select the在左侧导航窗格中,选择sample_training
database.sample_training
数据库。Select the选择companies
collection.companies
集合。
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 MongoDB中不同的查询运算符对null
values differently. 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 此筛选器指定字段的值必须为BSON Type Null
(BSON Type 10):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 } }