Search code examples
mongodbaggregation-frameworkquery-optimization

Mongodb aggregation pipeline optimization - 2 stage of $match


I am researching on performance optimization of aggregation pipeline recently. I came across this problem when deciding which to index. I set a compound index for {'receiverId': 1,'type': 1 } . My original pipeline looks like this. I am thinking whether indexing the isRead is necessary too. The reason why I didn't create a three fields compound index because other queries only use recieverId and type only.

notifications.aggregate([{
        $match: {
            'recieverId': ObjectId(xxx),
            'type': xxx,
            'isRead': false
        }
    },
    {
     ...
    },
], (err, result) => {
    return res.status(200).json(result);
});

So, I changed to this below. wondering if pipeline like this could save some calculation cost after filtering the recieverId and type. Will the isRead, second $match filter from the result of the first $match?

notifications.aggregate([{
        $match: {
            'recieverId': ObjectId(xxx),
            'type': xxx,
        }
    },
    {
        $match: {
            'isRead': false
        }
    },
    {
     ...
    },
], (err, result) => {
    return res.status(200).json(result);
});

Solution

  • It actually dosen't matter how you write this as mongo will optimize this pipeline.

    From the docs:

    When a $match immediately follows another $match, the two stages can coalesce into a single $match.

    Read about it and other pipeline optimizations mongo does here.