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 $exists does not correspond to SQL operator MongoDBexists
. $exists
与SQL运算符exists
不对应。For SQL 对于SQL exists
, refer to the $in
operator.exists
,请参阅$in
运算符。
See also参阅
$nin
, 、$in
, and 和Query for Null or Missing Fields查询空字段或缺少字段。
[1] | $type: 0 as a synonym for $exists:false . $type:0 用作$exists:false 的同义词。 |
Consider the following example:考虑下面的例子:
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
的所有单据。
The following examples uses a collection named 以下示例将名为records
with the following documents:records
的集合与以下文档一起使用:
$exists: true
¶The following query specifies the query predicate 以下查询指定查询谓词a: { $exists: true }
: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
包含null值的文档:
$exists: false
¶The following query specifies the query predicate 以下查询指定查询谓词b: { $exists: false }
:b: { $exists: false }
:
The results consist of those documents that do not contain the field 结果由不包含字段b
:b
的文档组成:
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.要查询空字段或缺少的字段,请参阅查询空字段或缺少的字段。
See also参阅