I have farm
collection that has apy
and liquidity
.
I want to get top 10 results by order of value of apy * liquidity
I tried like this.
var data = models.farm.find().sort({ $expr: { $multiply: ["$apy", "$liquidity"] } }).limit(10)
But it doesn't work well.
How can I get expected result?
Try adding another field with the value from apy * liquidity like this
var data = await models.farm.aggregate([
{
$addFields: {
apy_times_liquidity : { $multiply: ['$apy', '$liquidity'] }
}
},
{
$sort: { apy_times_liquidity : -1 }
},
{ $limit: 6 }
])
.exec()
.then(result => {
return result;
})
.catch(err => {
console.log(err);
});
res.send({ data: data });