Get latest MongoDB record by field of datetime
Use sort
and limit
:
db.col.find().sort({"datetime": -1}).limit(1)
For node.js
you can use the findOne()
function:
db.collection('yourCollectionName').findOne(
{},
{ sort: { datetime: -1 } },
(err, data) => {
console.log(data);
},
);