'How to check if a number is greater than the last element in an array of a node js schema

I am building an app in node.js using mongoDB. One of the fields in a schema I'm using is an array, into which I can push new numbers. I only want to be able to add a new number if it is higher than any number previously pushed into the array. I've been trying to do this by checking whether the new number is higher than the last element in the array.

const notHighest = await Item.findOne({
   nums:{$slice:-1}:$gt:{req.body.num}
})
if(notHighest) {
    return res.status(400).send({message:'Entry must be higher than the previous'})
}

This code doesn't work. Is there away the implement this or another way to go about solving this problem?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source