定义
$match根据指定的查询谓词过滤文档。匹配的文档将传递到下一个管道阶段。
兼容性
可以使用 $match 查找托管在以下环境中的部署:
MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
{ $match: { <query predicate> } }
行为
管道优化
尽可能早地将
$match放在聚合管道中。由于$match限制了聚合管道中的文档总数,因此早期的$match操作会最大限度地减少管道中的处理量。如果在管道的开头放置一个
$match,查询可以像使用任何其他db.collection.find()或db.collection.findOne()那样使用索引。
查询谓词中的表达式
0、Null、False 或缺失值
如果满足以下条件之一,则 $match 阶段会从管道结果中过滤掉文档:
$match查询谓词返回该文档的0、null或false值。$match查询谓词使用了该文档中缺少的字段。
限制
您不能在
$match阶段使用$where。您不能在
$match阶段中使用$near或$nearSphere。或者也可以:在
$match阶段使用带有$center或$centerSphere的$geoWithin查询谓词操作符。
要在
$match阶段中使用$text,$match阶段必须是管道的第一阶段。视图不支持
$text。注意
$text为自管理(非 Atlas)部署提供文本查询功能。对于MongoDB上托管的数据, MongoDB还提供改进的全文查询解决方案MongoDB Search。
使用MongoDB Search 筛选Atlas上的数据
对于存储在 MongoDB Atlas 中的数据,您可以在运行 查询时使用 MongoDB Search 复合操作符 操作符filter $search选项来匹配或过滤文档。$match在$search 之后运行 的性能低于使用$search 复合操作 操作符 选项运行filter 的性能。
要了解有关 filter 选项的更多信息,请参阅 Atlas 文档中的复合操作符。
示例
这些示例会使用包含以下文档且名为 articles 的集合:
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 } { "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 } { "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 } { "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55, "views" : 5000 } { "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 } { "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 } { "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 }
相等匹配
以下操作使用 $match 来执行等值匹配:
db.articles.aggregate( [ { $match : { author : "dave" } } ] );
$match 会选择 author 字段等于 dave 的文档,而聚合返回以下内容:
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 } { "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
执行计数
以下示例使用 $match 管道操作符选择要处理的文档,然后将结果导入到 $group 管道操作符,以计算文档的数量:
db.articles.aggregate( [ { $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } }, { $group: { _id: null, count: { $sum: 1 } } } ] );
在聚合管道中,$match 选择 score 大于 70 但小于 90 或 views 大于或等于 1000 的文档。然后,这些文档通过管道传送到 $group 进行计数。该聚合返回以下内容:
{ "_id" : null, "count" : 5 }
匹配数组元素
要根据大量中的元素过滤来自集合或上一个聚合阶段的文档,请在 $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 int Runtime { get; set; } public string Title { get; set; } public string Rated { get; set; } public List<string> Genres { get; set; } public string Plot { get; set; } public ImdbData Imdb { get; set; } public int Year { get; set; } public int Index { get; set; } public string[] Comments { get; set; } [] public DateTime LastUpdated { get; set; } }
注意
用于 Pascal Case 的 ConventionPack
此页面上的 C# 类在其属性名称中使用 Pascal 命名法,而 MongoDB 集合中的字段名称则使用 camel 命名法。为了解决这种差异,可以在应用程序启动时使用以下代码注册一个 ConventionPack:
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() }; ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
要使用MongoDB .NET/ C#驾驶员将 $match 阶段添加到聚合管道,请对 PipelineDefinition对象调用 Match() 方法。
以下示例创建一个管道阶段,用于匹配 Title字段等于 "The Shawshank Redemption" 的所有 Movie 文档:
var pipeline = new EmptyPipelineDefinition<Movie>() .Match(m => m.Title == "The Shawshank Redemption");
本页上的 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;
了解详情
请参阅完整聚合管道教程,以获取有关聚合的更多信息和应用场景。