I have a query that passes an array of ids in a specific order.
find({
id: { $in: [5, 3, 4, 1, 2] },
})
This query returns an array of documents with corresponding ids, but the documents are sorted in the ascending order.
[ {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}]
Instead I want the returned array to have documents in the same order in which the query specifies the ids. That is
[{id: 5}, {id: 3}, {id: 4}, {id: 1}, {id: 2}]
Is there a way to prevent mongodb from sorting the result?