定义
$match根据指定的查询谓词筛选文档。匹配的文档将传递到下一个管道阶段。
兼容性
可以使用 $match 查找托管在以下环境中的部署:
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
{ $match: { <query predicate> } }
行为
管道优化
If you place a
$matchat the very beginning of a pipeline, the query can take advantage of indexes like any otherdb.collection.find()ordb.collection.findOne().
查询谓词中的表达式
0、Null、False 或缺失值
如果满足以下条件之一,则 $match 阶段会从管道结果中过滤掉文档:
$match查询谓词返回该文档的0、null或false值。$match查询谓词使用了该文档中缺少的字段。
限制
您不能在
$match阶段使用$where。您不能在
$match阶段中使用$near或$nearSphere。或者也可以:Use the
$geoWithinquery predicate operator with$centeror$centerSpherein the$matchstage.
To use
$textin a$matchstage, the$matchstage has to be the first stage of the pipeline.视图不支持
$text。注意
$text提供自管理(非 Atlas)部署的文本查询功能。对于托管在 MongoDB Atlas 上的数据,MongoDB 提供了一种改进的全文查询解决方案,Atlas Search。
使用 Atlas Search 筛选 Atlas 数据
For data stored in MongoDB Atlas, you can use the Atlas Search compound Operator operator filter option to match or filter documents when running $search queries. Running $match after $search is less performant than running $search with the compound Operator operator filter option.
要了解有关 filter 选项的更多信息,请参阅 Atlas 文档中的复合操作符。
示例
本页上的示例使用sample_mflix示例数据集中的数据。有关如何将此数据集加载到自管理MongoDB 部署中的详细信息,请参阅加载示例数据集。如果对示例数据库进行了任何修改,则可能需要删除并重新创建数据库才能运行本页上的示例。
相等匹配
The following operation uses $match to perform an equality match on the rated field. The runtime filter limits the result to a small, representative set:
db.movies.aggregate( [ { $match : { rated : "TV-PG", runtime : { $gt: 1000 } } } ] )
The $match selects the documents where the rated field equals "TV-PG" and runtime is greater than 1000.
执行计数
The following example selects documents to process using the $match pipeline operator and then pipes the results to the $group pipeline operator to compute a count of the documents:
db.movies.aggregate( [ { $match: { $or: [ { runtime: { $gt: 1000 } }, { year: { $lt: 1910 } } ] } }, { $group: { _id: null, count: { $sum: 1 } } } ] )
[ { _id: null, count: 6 } ]
In the aggregation pipeline, $match selects the documents where either the runtime is greater than 1000 or the year is earlier than 1910. These documents are then sent to the $group to perform a count.
匹配数组元素
要根据数组字段中的元素过滤 document,在 $match 阶段的查询谓词中使用 $elemMatch 操作符:
db.aggregate( [ { $documents: [ { student_id: 1, scores: [ 0.75, 0.65, 0.73 ] }, { student_id: 2, scores: [ 0.9, 0.88, 0.98 ] }, { student_id: 3, scores: [ 0.9, 0.84, 0.93 ] } ] }, { $match: { scores: { $elemMatch: { $gte: 0.9 } } } } ] )
[ { student_id: 2, scores: [ 0.9, 0.88, 0.98 ] }, { student_id: 3, scores: [ 0.9, 0.84, 0.93 ] } ]
本页上的C#示例使用Atlas示例数据集中的 sample_mflix数据库。要学习;了解如何创建免费的MongoDB Atlas 群集并加载示例数据集,请参阅MongoDB .NET/ C#驱动程序文档中的入门。
以下 Movie 类对 sample_mflix.movies 集合中的文档进行建模:
[] public class Movie { [] public ObjectId Id { get; set; } [] public string Title { get; set; } = null!; [] public int? Year { get; set; } [] public int? Runtime { get; set; } [] public string? Rated { get; set; } [] public int Metacritic { get; set; } [] public string? Plot { get; set; } [] public string? Type { get; set; } [] public string[]? Cast { get; set; } [] public string[]? Directors { get; set; } [] public string[]? Writers { get; set; } [] public ImdbData? Imdb { get; set; } }
To use the MongoDB .NET/C# driver to add a $match stage to an aggregation pipeline, call the UnionWith() method on a PipelineDefinition object.
以下示例创建一个管道阶段,用于匹配 Title字段等于 "The Godfather" 的所有 Movie 文档:
var pipeline = new EmptyPipelineDefinition<Movie>() .Match(m => m.Title == "The Godfather");
{ "_id" : "...", "title" : "The Godfather", "metacritic" : 100, "rated" : "R", "runtime" : 175, "imdb" : "..." }
本页上的 Node.js 示例使用 Atlas 示例数据集中的 sample_mflix数据库。要学习如何创建免费的MongoDB Atlas 集群并加载示例数据集,请参阅MongoDB Node.js驱动程序文档中的入门。
要使用MongoDB Node.js驱动程序将 $match 阶段添加到聚合管道,请在管道对象中使用 $match操作符。
以下示例创建一个管道阶段,该阶段匹配所有 movie 文档,其中 title 字段等于 "The Shawshank Redemption"。然后,示例运行聚合管道:
const pipeline = [ { $match: { title: "The Shawshank Redemption" } } ]; const cursor = collection.aggregate(pipeline); return cursor;
了解详情
请参阅完整聚合管道教程,以获取有关聚合的更多信息和应用场景。