I have a collection like this:
{ 'datetime': some-date, 'lat': '32.00', 'lon': '74.00' }, { 'datetime': some-date, 'lat': '32.00', 'lon': '74.00' }
How can I get the latest record from MongoDB, where the datetime is the latest one? I want only a single record.
You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});
Use sort
and limit
:
db.col.find().sort({"datetime": -1}).limit(1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With