Reputation: 513
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);
});
Upvotes: 0
Views: 421
Reputation: 22316
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.
Upvotes: 1