I am trying to get the latest collection record from my MongoDB using mongoose.
My current code goes as follows:
ApiData.findOne(
(err, data) => {
if (err) {
// res.status(200).send(err);
}
if (data) { // Search could come back empty, so we should protect against sending nothing back
// res.status(200).send(data);
console.log(data);
} else { // In case no kitten was found with the given query
// res.status(200).send("No kitten found");
}
}, { sort: { _id: -1 }, limit: 1 });
But this does not work in sorting the latest data, only shows the first record.