So I'd like to order products by price - ASC and DESC, but some of the products will have a discounted:true attribute. If discounted is true sort should use discountPrice field and if fals - they should use price field.
Im using mongoose version: ^5.1.0
Here is my (not working) solution
products.find({}, {sort: { price: 1, discountPrice: 1 }})
Using those products:
{
name: 'p1',
price: 10,
discountPrice: 5,
discounted: true
},
{
name: 'p2',
price: 11,
discountPrice: 8,
discounted: false
},
{
name: 'p3',
price: 990,
discountPrice: 6,
discounted: true
}
I expect the output to be:
[p1, p3, p2]
but the actual output is:
[p1, p2, p3]