On this page本页内容
$match¶Filters the documents to pass only the documents that match the specified condition(s) to the next pipeline stage.筛选文档以仅将符合指定条件的文档传递到下一个管道阶段。
The $match stage has the following prototype form:$match阶段的原型形式如下:
$match takes a document that specifies the query conditions. $match获取指定查询条件的文档。The query syntax is identical to the read operation query syntax; i.e. 查询语法与读取操作查询语法相同;亦即,$match does not accept raw aggregation expressions. $match不接受原始聚合表达式。Instead, use a 相反,使用$expr query expression to include aggregation expression in $match.$expr查询表达式在$match中包含聚合表达式。
$match as early in the aggregation pipeline as possible. $match放入聚合管道中。$match limits the total number of documents in the aggregation pipeline, earlier $match operations minimize the amount of processing down the pipe.$match限制了聚合管道中的文档总数,所以早期的$match操作可以最大限度地减少管道中的处理量。$match at the very beginning of a pipeline, the query can take advantage of indexes like any other db.collection.find() or db.collection.findOne().$match,查询可以像任何其他db.collection.find()或db.collection.findOne()一样利用索引。$match query syntax is identical to the read operation query syntax; i.e. $match does not accept raw aggregation expressions. $match查询语法与读取操作查询语法相同;亦即,$match不接受原始聚合表达式。$match, use a $expr query expression:$match中包含聚合表达式,请使用$expr查询表达式:
$where in $match queries as part of the aggregation pipeline.$match查询中使用$where。$near or $nearSphere in $match queries as part of the aggregation pipeline. $match查询中使用$near或$nearSphere。$geoNear stage instead of the $match stage.$geoNear阶段而不是$match阶段。$geoWithin query operator with $center or $centerSphere in the $match stage.$match阶段中,将$geoWithin查询运算符与$center或$centerSphere一起使用。$text in the $match stage, the $match stage has to be the first stage of the pipeline.$match阶段中使用$text,$match阶段必须是管道的第一阶段。
The examples use a collection named 这些示例使用了一个名为articles with the following documents:articles的集合,其中包含以下文档:
The following operation uses 以下操作使用$match to perform a simple equality match:$match执行简单的相等匹配:
The $match selects the documents where the author field equals dave, and the aggregation returns the following:$match选择author字段等于dave的文档,聚合返回以下内容:
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管道运算符以计算文档计数:
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:聚合返回以下内容: