$exists
On this page本页内容
Definition定义
$exists-
Syntax:
{ field: { $exists: <boolean> } }When当<boolean>is true,$existsmatches the documents that contain the field, including documents where the field value isnull.<boolean>为true时,$exists匹配包含该字段的文档,包括字段值为null的文档。If如果<boolean>is false, the query returns only the documents that do not contain the field.<boolean>为false,则查询仅返回不包含该字段的文档。[1]MongoDBMongoDB$existsdoes not correspond to SQL operatorexists. For SQLexists, refer to the$inoperator.$exists与SQL运算符exists不对应。对于SQL,请参阅$in运算符。
See also: 另请参阅:
| [1] | $type: 0 as a synonym for $exists:false. $type:0作为$exists:false的同义词。null或缺少的字段,请参阅查询null或丢失的字段。 |
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字段且其值不等于5或15的所有单据。
Null Values空值
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包含空值的文档:
{ 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: { $exists: false }的文件:
{ saffron: 2, mustard: 5 }
{ saffron: 4 }
{ mustard: 6 }
Starting in MongoDB 4.2, users can no longer use the query filter 从MongoDB 4.2开始,用户不能再使用查询筛选器$type: 0 as a synonym for $exists:false. To query for null or missing fields, see Query for Null or Missing Fields.$type: 0作为$exists:false的同义词。若要查询null或缺少的字段,请参阅查询null或丢失的字段。
Use a Sparse Index to Improve $exists Performance使用稀疏索引提高$exists性能
$exists PerformanceThe following table compares 下表比较了使用稀疏索引和非稀疏索引的$exists query performance using sparse and non-sparse indexes:$exists查询性能:
$exists | ||
|---|---|---|
{ $exists: true } | FETCH.FETCH。 | FETCH.FETCH。 |
{ $exists: false } | COLLSCAN.COLLSCAN。 | 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上创建稀疏索引,如以下场景所示:
Create a创建stockSalescollection: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_idof:_id为以下情况的文档:3has a null具有值为auditDatevalue.null的auditDate值。5is missing the缺少auditDatevalue.auditDate值。
Create a sparse index on the在auditDatefield:auditDate字段上创建稀疏索引:db.getCollection( "stockSales" ).createIndex(
{ auditDate: 1 },
{ name: "auditDateSparseIndex", sparse: true }
)The following example counts the documents where the以下示例统计auditDatefield 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该示例返回auditDatevalue is not counted.5。不计算缺少auditDate值的文档。
If you only need documents where the 如果您只需要field has a non-null value, you:field具有非null值的文档,则可以:
Can use可以使用$ne: nullinstead of$exists: true.$ne:null而不是$exist:true。Do not need a sparse index on the不需要field.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为空的文档不计算在内。