定义
$exists$exists运算符匹配包含或不包含指定字段的文档,包括字段值为null的文档。注意
MongoDB
$existsdoes not correspond to SQL operatorexists. For SQLexists, see$in.For MongoDB Search
exists, see exists (MongoDB Search Operator) in the Atlas documentation.
兼容性
可以使用 $exists 查找托管在以下环境中的部署:
MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
注意
表达式不支持$exists操作符。要检查表达式中某个字段是否存在,可以使用 $type 聚合操作符来检查字段的类型是否为 missing。
有关更多信息,请参阅 $type 存在性检查。
语法
要指定 $exists 表达式,请使用以下原型:
{ field: { $exists: <boolean> } }
当 <boolean> 为 true 时,$exists 匹配包含该字段的文档,包括字段值为 null 的文档。如果 <boolean> 为 false,则查询仅返回不包含该字段的文档。
使用MongoDB Search 在Atlas上查询数据
对于存储在 MongoDB Atlas 中的数据,您可以在运行 查询时使用 MongoDB Search exists(MongoDB Search Operator) $search操作符。$exists$search在 之后运行 的性能低于运行带有$search exists(MongoDB搜索操作符) 操作符的 。
要学习;了解有关此操作符的MongoDB Search 版本的更多信息,请参阅Atlas文档中的exists ( MongoDB Search Operator)操作符。
示例
本页上的示例使用sample_mflix示例数据集中的数据。有关如何将此数据集加载到自管理MongoDB 部署中的详细信息,请参阅加载示例数据集。如果对示例数据库进行了任何修改,则可能需要删除并重新创建数据库才能运行本页上的示例。
存在且不等于
考虑以下示例:
db.movies.find( { rated: { $exists: true, $nin: [ "R", "PG-13" ] } } ).limit(5)
This query selects 5 documents in the movies collection where the rated field exists and its value does not equal "R" or "PG-13".
Null Values
The movies collection in the sample_mflix database contains documents where some fields are present and others are missing. For example, the rated field exists in 11,455 documents and is absent from the remaining 9,894 documents.
$exists: true
以下查询将指定查询谓词 rated: { $exists: true }:
db.movies.find( { rated: { $exists: true } }, { _id: 0, title: 1, rated: 1 } ).limit( 3 )
The results consist of three documents that contain the field rated:
[ { title: 'The Great Train Robbery', rated: 'TV-G' }, { title: 'A Corner in Wheat', rated: 'G' }, { title: 'Traffic in Souls', rated: 'TV-PG' } ]
$exists: false
以下查询将指定查询谓词 rated: { $exists: false }:
db.movies.find( { rated: { $exists: false } }, { _id: 0, title: 1, year: 1 } ).limit( 3 )
The results consist of three documents that do not contain the field rated:
[ { title: 'Winsor McCay, the Famous Cartoonist of the N.Y. Herald and His Moving Comics', year: 1911 }, { title: 'Gertie the Dinosaur', year: 1914 }, { title: 'In the Land of the Head Hunters', year: 1914 } ]
使用稀疏索引提高 $exists 性能
下表比较使用稀疏索引和非稀疏索引的 $exists 查询性能:
$exists 查询 | 使用稀疏索引 | Using a Non-Sparse Index |
|---|---|---|
| 最高效。MongoDB 可以进行精确匹配,不需要 | 比不含索引的查询更高效,但仍需 |
| 无法使用索引,需要 | 需要使用 |
以下查询检查集合中的所有文档:对使用非稀疏索引的字段使用 { $exists: true } 或对未编入索引的字段使用 { $exists: true }。为了提高性能,请在 field 上创建稀疏索引,如以下场景所示:
The
moviescollection contains documents where themetacriticfield is present in some documents and absent from others. Of the 21,349 documents, 6,964 have themetacriticfield and 14,385 do not.在
metacritic字段上创建稀疏索引:db.movies.createIndex( { metacritic: 1 }, { name: "metacriticSparseIndex", sparse: true } ) 以下示例计算
metacritic字段具有值(包括 null)并使用稀疏索引的文档:db.movies.countDocuments( { metacritic: { $exists: true } } ) The example returns 6964. The operation does not count the documents that are missing the
metacriticfield.
提示
如果只需 field 具有非空值的文档,您可以:
可以使用
$ne: null代替$exists: true。不需要在
field上使用稀疏索引。
例如,使用 movies 集合:
db.stockSales.countDocuments( { auditDate: { $ne: null } } )
The example returns 6964. Documents that are missing the metacritic value or have a null metacritic value are not counted.