Database Manual / Reference / Query Language / Query Predicates / Data Type

$exists

Definition定义

$exists

The $exists operator matches documents that contain or do not contain a specified field, including documents where the field value is null.$exists运算符匹配包含或不包含指定字段的文档,包括字段值为null的文档。

Note

MongoDB $exists does not correspond to SQL operator exists. For SQL exists, refer to the $in operator.MongoDB$exists与SQL运算符exists不对应。如果SQL存在,请参考$in运算符。

For MongoDB Search exists, refer to the exists (MongoDB Search Operator) operator in the Atlas documentation.有关MongoDB搜索存在,请参阅Atlas文档中的exists(MongoDB搜索运算符)运算符

Compatibility兼容性

You can use $exists for deployments hosted in the following environments:您可以将$exists用于在以下环境中托管的部署:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

To specify an $exists expression, use the following prototype:要指定$exists表达式,请使用以下原型:

{ field: { $exists: <boolean> } }

When <boolean> is true, $exists matches the documents that contain the field, including documents where the field value is null. If <boolean> is false, the query returns only the documents that do not contain the field. <boolean>true时,$exists与包含该字段的文档匹配,包括字段值为null的文档。如果<boolean>false,则查询仅返回不包含该字段的文档。[1]

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

Query Data on Atlas by Using MongoDB Search使用MongoDB搜索在Atlas上查询数据

For data stored in MongoDB Atlas, you can use the MongoDB Search exists (MongoDB Search Operator) operator when running $search queries. 对于存储在MongoDB Atlas中的数据,您可以在运行$search查询时使用MongoDB搜索exists(MongoDB搜索运算符)运算符Running $exists after $search is less performant than running $search with the exists (MongoDB Search Operator) operator.$search之后运行$exists比使用exists(MongoDB搜索运算符)运算符运行$search的性能低。

To learn more about the MongoDB Search version of this operator, see the exists (MongoDB Search Operator) operator in the Atlas documentation.要了解有关此运算符的MongoDB搜索版本的更多信息,请参阅Atlas文档中的exists(MongoDB搜索运算符)运算符

Examples示例

Exists and Not Equal To存在但不等于

Consider the following example:考虑以下示例:

db.inventory.find( { qty: { $exists: true, $nin: [ 5, 15 ] } } )

This query will select all documents in the inventory collection where the qty field exists and its value does not equal 5 or 15.此查询将选择inventory收款中存在qty字段且其值不等于515的所有文档。

Null Valuesnull

The following examples uses a collection named spices with the following documents:以下示例使用名为spices的集合和以下文档:

db.spices.insertMany( [
{ saffron: 5, cinnamon: 5, mustard: null },
{ saffron: 3, cinnamon: null, mustard: 8 },
{ saffron: null, cinnamon: 3, mustard: 9 },
{ saffron: 1, cinnamon: 2, mustard: 3 },
{ saffron: 2, mustard: 5 },
{ saffron: 3, cinnamon: 2 },
{ saffron: 4 },
{ cinnamon: 2, mustard: 4 },
{ cinnamon: 2 },
{ mustard: 6 }
] )

$exists: true

The following query specifies the query predicate saffron: { $exists: true }:以下查询指定了查询谓词saffron: { $exists: true }

db.spices.find( { saffron: { $exists: true } } )

The results consist of those documents that contain the field saffron, including the document whose field saffron contains a null value:结果由包含字段saffron的文档组成,包括字段saffron包含null值的文档:

{ saffron: 5, cinnamon: 5, mustard: null }
{ saffron: 3, cinnamon: null, mustard: 8 }
{ saffron: null, cinnamon: 3, mustard: 9 }
{ saffron: 1, cinnamon: 2, mustard: 3 }
{ saffron: 2, mustard: 5 }
{ saffron: 3, cinnamon: 2 }
{ saffron: 4 }

$exists: false

The following query specifies the query predicate cinnamon: { $exists: false }:以下查询指定了查询谓词cinnamon: { $exists: false }

db.spices.find( { cinnamon: { $exists: false } } )

The results consist of those documents that do not contain the field cinnamon:结果包括那些不含cinnamon的文件:

{ saffron: 2, mustard: 5 }
{ saffron: 4 }
{ mustard: 6 }

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

Use a Sparse Index to Improve $exists Performance使用稀疏指数提高$exists性能

The following table compares $exists query performance using sparse and non-sparse indexes:下表比较了使用稀疏索引和非稀疏索引的$exists查询性能:

$exists Query查询Using a Sparse Index使用稀疏索引Using a Non-Sparse Index使用非稀疏索引
{ $exists: true }Most efficient. MongoDB can make an exact match and does not require a FETCH.效率最高。MongoDB可以进行精确匹配,不需要FETCHMore efficient than queries without an index, but still requires a FETCH.比没有索引的查询更有效,但仍然需要FETCH
{ $exists: false }Cannot use the index and requires a COLLSCAN.无法使用索引,需要COLLSCANRequires a FETCH.需要FETCH

Queries that use { $exists: true } on fields that use a non-sparse index or that use { $exists: true } on fields that are not indexed examine all documents in a collection. 对使用非稀疏索引的字段使用{ $exists: true }或对未索引的字段采用{ $exists: true }的查询会检查集合中的所有文档。To improve performance, create a sparse index on the field as shown in the following scenario:为了提高性能,请在field上创建稀疏索引,如下所示:

  1. Create a stockSales collection:创建stockSales销售集合:

    db.stockSales.insertMany( [
    { _id: 0, symbol: "MDB", auditDate: new Date( "2021-05-18T16:12:23Z" ) },
    { _id: 1, symbol: "MDB", auditDate: new Date( "2021-04-21T11:34:45Z" ) },
    { _id: 2, symbol: "MSFT", auditDate: new Date( "2021-02-24T15:11:32Z" ) },
    { _id: 3, symbol: "MSFT", auditDate: null },
    { _id: 4, symbol: "MSFT", auditDate: new Date( "2021-07-13T18:32:54Z" ) },
    { _id: 5, symbol: "AAPL" }
    ] )

    The document with an _id of:_id为下述的文档:

    • 3 has a null auditDate value.auditDate值为null
    • 5 is missing the auditDate value.缺少auditDate值。
  2. Create a sparse index on the auditDate field:auditDate字段上创建稀疏索引

    db.getCollection( "stockSales" ).createIndex(
    { auditDate: 1 },
    { name: "auditDateSparseIndex", sparse: true }
    )
  3. The following example counts the documents where the auditDate field has a value (including null) and uses the sparse index:以下示例对auditDate字段有值(包括null)的文档进行计数,并使用稀疏索引

    db.stockSales.countDocuments( { auditDate: { $exists: true } } )

    The example returns 5. The document that is missing the auditDate value is not counted.该示例返回5。缺少auditDate值的文档不计算在内。

Tip

If you only need documents where the field has a non-null value, you:如果您只需要field值为非空的文档,您可以:

  • Can use $ne: null instead of $exists: true.可以使用$ne: null代替$exists: true
  • Do not need a sparse index on the field.字段上不需要稀疏索引

For example, using the stockSales collection:例如,使用stockSales集合:

db.stockSales.countDocuments( { auditDate: { $ne: null } } )

The example returns 4. Documents that are missing the auditDate value or have a null auditDate value are not counted.该示例返回4。缺少auditDate值或auditDate为空的文档不计算在内。