mongodbaggregation-framework

Does second $match use index


From the MongoDB docs:

Place the $match as early in the aggregation pipeline as possible. Because $match limits the total number of documents in the aggregation pipeline, earlier $match operations minimize the amount of processing down the pipe.

If you place a $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().

Given the query

db.articles.aggregate( [ 
                    { $match : {date : {$gt: now, $lte: later } } },
                    { $match : { score : { $gt : 70, $lte : 90 } } },
                    { $group: { _id: null, count: { $sum: 1 } } }
                   ] );

where (of course), now and later represent properly formatted dates,

Would the aggregation framework utilize an index for the second match (if it is available), or is only the first match in an aggregation pipeline eligible to use an index.

Would the query be perform better as:

db.articles.aggregate( [ 
                        { $match : {date : {$gt: now, $lte: later }, score : { $gt : 70, $lte : 90 } } },
                        { $group: { _id: null, count: { $sum: 1 } } }
                   ] );

assuming an index exists that covers date and score?


Solution

  • No, MongoDB (as of v2.4.6) will only use an index on the first $match in your pipeline.

    For the second aggregation query you've posted, as long as you have a compound index on date and score, that index would get used for the query. This should perform better than your first query.