I have an array of productIds
and a Product
collection. I want to query the product collection
for all the products in the productIds array but I want the result to be in the same order as
the query array. The productsIds are in a sorted order using other parameters which is not
available in the documents in Products collection, so I cannot use a sort on product collection.
Is there a way to preserve the order?
Example:
productIds: [362,128,4783,1,44]
After db.collection('products'). find({id:{$in:productIds}}).toArray()
I want the documents to be in the order as that of the productIds
Currently I am getting a different order.