After upgrading my mongo server from 3.4 to 4.2, some aggregations using $sort and $count are now very slow to execute. To be sure it was related to the upgrade I installed several versions of Mongo locally and tried the same aggregation on the same amount of data and here's the results :
db.getCollection('devices').aggregate([
{ $lookup: { from: "sites", localField: "_id", foreignField: "devices", as: "site" } },
{ $unwind: { path: "$site", preserveNullAndEmptyArrays: true } },
{ $lookup: { from: "users", localField: "site._id", foreignField: "sites", as: "users" } },
{ $count: "count"}
])
Mongo version Time
3.4 => 0.47s
3.6 => 0.632s
4.0 => 0.641s
4.2 => 2.98s
4.3.5 => 3.1s
db.getCollection('devices').aggregate([
{ $lookup: { from: "sites", localField: "_id", foreignField: "devices", as: "site" } },
{ $unwind: { path: "$site", preserveNullAndEmptyArrays: true } },
{ $lookup: { from: "users", localField: "site._id", foreignField: "sites", as: "users" } },
{ $sort: { _id: 1 } },
{ $skip: 0 },
{ $limit: 20 }
])
Mongo version Time
3.4 => 0.476s
3.6 => 0.581s
4.0 => 0.669s
4.2 => 2.89s
4.3.5 => 3.24s
I have indexes on every fields used by the $lookup I know that the $sort should be put at the first stages of the pipeline to use the index but as this query is written dynamically, the sorted field is not always on _id and can be one of the field coming from the $lookup, also $match may be added dynamically between the stages, eg. $match: {"users.type": "customer"}
My real questions are "How can the performances getting so worse by upgrading mongo ? Am I missing something obvious ?"
Thanks for your help.