I have the following problem, I have to do a sortBy to a mongo DB collection, but with 3 types of fields that are type int / number.
I currently have the query
await Products.find({}).sort({ miShopPrice: -1, discountPrice: -1, normalPrice: -1 })
but this returns me:
[
{
"normalPrice": 250,
"miShopPrice": 100,
"discountPrice": 0
},
{
"normalPrice": 64990,
"miShopPrice": 0,
"discountPrice": 0
},
{
"normalPrice": 19500,
"miShopPrice": 0,
"discountPrice": 0
},
{
"normalPrice": 1600,
"miShopPrice": 0,
"discountPrice": 0
}
]
mcdvoice
the condition is as follows
if you have miShopPrice and normalPrice → myShopPrice
if you have miShopPrice and discountPrice → myShopPrice
if you have discountPrice and normalPrice → discountPrice
if you only have normalPrice → normalPrice
what the query should return would be this:
[
{
"normalPrice": 64990,
"miShopPrice": 0,
"discountPrice": 0
},
{
"normalPrice": 19500,
"miShopPrice": 0,
"discountPrice": 0
},
{
"normalPrice": 1600,
"miShopPrice": 0,
"discountPrice": 0
},
{
"normalPrice": 250,
"miShopPrice": 100,
"discountPrice": 0
}
]
could you help me I would really appreciate it