On this page本页内容
$exists
Syntax: 语法:{ field: { $exists: <boolean> } }
When 当<boolean>
is true, $exists
matches the documents that contain the field, including documents where the field value is null
. <boolean>
为true
时,$exists
匹配包含该字段的文档,包括字段值为null
的文档。If 如果<boolean>
is false, the query returns only the documents that do not contain the field. <boolean>
为false
,则查询只返回不包含该字段的文档。[1]
MongoDB MongoDB$exists
does not correspond to SQL operator exists
. $exists
与SQL运算符exists
不对应。For SQL 对于SQL exists
, refer to the $in
operator.exists
,请参阅$in
运算符。
[1] | $type: 0 as a synonym for $exists:false . $type:0 作为$exists:false 的同义词。 |
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
The following examples uses a collection named 以下示例使用具有以下文档的名为records
with the following documents:records
的集合:
{ a: 5, b: 5, c: null } { a: 3, b: null, c: 8 } { a: null, b: 3, c: 9 } { a: 1, b: 2, c: 3 } { a: 2, c: 5 } { a: 3, b: 2 } { a: 4 } { b: 2, c: 4 } { b: 2 } { c: 6 }
$exists: true
The following query specifies the query predicate 以下查询指定了查询谓词a: { $exists: true }
:a: { $exists: true }
:
db.records.find( { a: { $exists: true } } )
The results consist of those documents that contain the field 结果由包含字段a
, including the document whose field a
contains a null value:a
的文档组成,包括字段a
包含空值的文档:
{ a: 5, b: 5, c: null } { a: 3, b: null, c: 8 } { a: null, b: 3, c: 9 } { a: 1, b: 2, c: 3 } { a: 2, c: 5 } { a: 3, b: 2 } { a: 4 }
$exists: false
The following query specifies the query predicate 以下查询指定了查询谓词b: { $exists: false }
:b: { $exists: false }
:
db.records.find( { b: { $exists: false } } )
The results consist of those documents that do not contain the field 结果包括不包含字段b
:b
的文档:
{ a: 2, c: 5 } { a: 4 } { c: 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
. $type:0
作为$exists:false
的同义词。To query for null or missing fields, see Query for Null or Missing Fields.要查询空字段或缺失字段,请参阅查询空字段和缺失字段。
$exists
Performance$exists
性能The following scenario is not optimal because all of the collection's documents are examined:以下场景不是最佳的,因为集合的所有文档都已检查:
field: { $exists: true }
, andfield: { $exists: true }
,并且field
has a non-sparse index or does not have an index.field
具有非稀疏索引或没有索引。To improve performance, create a sparse index on the 要提高性能,请在field
as shown in the following scenario:field
上创建稀疏索引,如以下场景所示:
Create a 创建stockSales
collection:stockSales
集合:
db.stockSales.insertMany( [ { _id: 0, symbol: "ABC", auditDate: new Date( "2021-05-18T16:12:23Z" ) }, { _id: 1, symbol: "ABC", auditDate: new Date( "2021-04-21T11:34:45Z" ) }, { _id: 2, symbol: "DEF", auditDate: new Date( "2021-02-24T15:11:32Z" ) }, { _id: 3, symbol: "DEF", auditDate: null }, { _id: 4, symbol: "DEF", auditDate: new Date( "2021-07-13T18:32:54Z" ) }, { _id: 5, symbol: "XYZ" } ] )
The document with an _id
of:_id
为的文档:
3
auditDate
value.auditDate
值。5
auditDate
value.auditDate
值。Create a sparse index on the 在auditDate
field:auditDate
字段上创建稀疏索引:
db.getCollection( "stockSales" ).createIndex( { auditDate: 1 }, { name: "auditDateSparseIndex", sparse: true } )
The following example counts the documents where the 以下示例统计auditDate
field has a value (including null) and uses the sparse index:auditDate
字段具有值(包括空)并使用稀疏索引的文档:
db.stockSales.countDocuments( { auditDate: { $exists: true } } )
The example returns 5. 示例返回5。The document that is missing the 不计算缺少auditDate
value is not counted.auditDate
值的文档。
If you only need documents where the 如果您只需要field
has a non-null value, you:field
具有非空值的文档,您可以:
$ne: null
instead of $exists: true
.$ne:null
而不是$exists:true
。field
.field
上的稀疏索引。For example, using the 例如,使用stockSales
collection:stockSales
集合:
db.stockSales.countDocuments( { auditDate: { $ne: null } } )
The example returns 4. 示例返回4。Documents that are missing the 缺少auditDate
value or have a null auditDate
value are not counted.auditDate
值或auditDate
值为空的文档不计算在内。