I have an array of objects that I want to sort by. .sort() at top level works like this according to documentation:
Product.findById(productId)
.sort({ somefield: -1 })
I have tried something like this:
Product.findById(productId)
.sort( { requests: { _id: -1 } } )
But I get the following error:
TypeError: Invalid sort value: { requests: [object Object] }
If I have the object ID's in this order in mongodb (compass)
_id: 123456789
_id: 987654321
I want _id 987654321 show first in the list.
I have even added a date field now and tried to sort by that but the order still stays the same as what the record was inserted in.
Product.findById(productId)
.sort( { 'requests.requestDt': 1} )