Tried to get last record value one but not working. How to do it in mongoose.
Sample data records in mongodb:
[
{
_id: 5f0c73f69b34214310d0acee,
name: "Test",
age: 28,
voice: "Not Tested"
},
{
_id: 5f0c73f69b34214310d0akke,
name: "Test2",
age: 38,
voice: "Tested"
}
]
Mongoose query:
The value of voice property in the last record is "Tested" so I tried to get that value.
var Order = mongoose.model(collectionName);
Order.find({}).sort({ _id: -1 }).limit(1).then((products) => {
products.findOne({voice}, function(err,obj) {
console.log(obj);//Output should be = Tested
});
});