$match (aggregation)
On this page本页内容
Definition定义
$match-
Filters the documents to pass only the documents that match the specified condition(s) to the next pipeline stage.筛选文档以仅将符合指定条件的文档传递到下一个管道阶段。The$matchstage has the following prototype form:$match阶段具有以下原型形式:{ $match: { <query> } }$matchtakes a document that specifies the query conditions.获取指定查询条件的文档。The query syntax is identical to the read operation query syntax; i.e.查询语法与读取操作查询语法相同;即$matchdoes not accept raw aggregation expressions. Instead, use a$exprquery expression to include aggregation expression in$match.$match不接受原始聚合表达式。相反,使用$expr查询表达式将聚合表达式包含在$match中。
Behavior行为
Pipeline Optimization管道优化
Place the在聚合管道中尽可能早地放置$matchas early in the aggregation pipeline as possible. Because$matchlimits the total number of documents in the aggregation pipeline, earlier$matchoperations minimize the amount of processing down the pipe.$match。由于$match限制了聚合管道中的文档总数,因此早期的$match操作可以最大限度地减少管道中的处理量。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().$match,那么查询可以像其他db.collection.find()或db.collection.findOne()一样利用索引。
Restrictions限制
The$matchquery syntax is identical to the read operation query syntax; i.e.$matchdoes not accept raw aggregation expressions.$match查询语法与读取操作查询语法相同;即$match不接受原始聚合表达式。To include aggregation expression in要在$match, use a$exprquery expression:$match中包含聚合表达式,请使用$expr查询表达式:{ $match: { $expr: { <aggregation expression> } } }You cannot use不能在$wherein$matchqueries as part of the aggregation pipeline.$match查询中使用$where作为聚合管道的一部分。You cannot use不能在$nearor$nearSpherein$matchqueries as part of the aggregation pipeline. As an alternative, you can either:$match查询中使用$near或$nearSphere作为聚合管道的一部分。作为替代方案,您可以:Use使用$geoNearstage instead of the$matchstage.$geoNear阶段,而不是$match阶段。Use在$geoWithinquery operator with$centeror$centerSpherein the$matchstage.$match阶段将$geoWithin查询运算符与$center或$centerSphere一起使用。
To use要在$textin the$matchstage, the$matchstage has to be the first stage of the pipeline.$match阶段中使用$text,$match必须是管道的第一个阶段。
Examples实例
The examples use a collection named 示例使用了一个名为articles with the following documents: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 }
Equality Match相等匹配
The following operation uses 以下操作使用$match to perform a simple equality match:$match执行简单的相等匹配:
db.articles.aggregate(
[ { $match : { author : "dave" } } ]
);
The $match selects the documents where the author field equals dave, and the aggregation returns the following:$match选择author字段等于dave的文档,聚合返回以下内容:
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
Perform a Count执行计数
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:$match管道运算符选择要处理的文档,然后将结果管道传输到$group管道运算符以计算文档数:
db.articles.aggregate( [
{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
In the aggregation pipeline, 在聚合管道中,$match selects the documents where either the score is greater than 70 and less than 90 or the views is greater than or equal to 1000. $match选择score大于70且小于90或views大于或等于1000的文档。These documents are then piped to the 然后,这些文档通过管道传输到$group to perform a count. $group以执行计数。The aggregation returns the following:聚合返回以下内容:
{ "_id" : null, "count" : 5 }
Additional Information附加信息
Refer to the following pages for more information and use cases on aggregation.有关聚合的更多信息和用例,请参阅以下页面。
Filter Data on Atlas Using Atlas Search使用Atlas搜索筛选Atlas上的数据
For your 对于针对Atlas集群上数据的$search queries against data on your Atlas cluster, you can use the Atlas Search compound operator filter option to match or filter documents. Running $match after $search is less performant than running $search with the compound operator filter option. $search之后的$match查询,您可以使用Atlas search复合运算符筛选器选项来匹配或筛选文档。在$search之后运行$match的性能不如使用复合运算符筛选器选项运行$search。To learn more about the 要了解有关filter option, see compound.filter选项的详细信息,请参阅复合。