Aggregation Framework - $match

Overview

$match filters documents. Documents which do not match the specified predicate are filtered out and do not progress further along the aggregation pipeline. Documents which do match are passed along unchanged.

Specification

$match predicate syntax is always exactly the same as that for queries; See Querying and Advanced Queries.

Within a pipeline, the $match operator appears thusly:

db.article.aggregate(
    { $match : <match-predicate> }
);

Here is an example with a simple field equality test:

db.aggregate(
    { $match : { author : "dave" } }
);

On its own like this, this is the equivalent of db.article.find({ author : "dave" }).

Here is another example, this time with a range test:

db.article.aggregate(
    { $match : { score : { $gt : 50, $lte : 90 } }
);

Notes

$match should be placed as early in the aggregation pipeline as possible. This minimizes the number of documents after it, thereby minimizing later processing. Placing a $match at the very beginning of a pipeline will enable it to take advantage of indexes in exactly the same way as a regular query (find()/findOne()).

Follow @mongodb

MongoDB Pittsburgh - May 15
MongoNYC - May 23
MongoDB Paris - Jun 14
MongoDB UK - Jun 20
MongoDC - June 26


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.

blog comments powered by Disqus