문제

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.

도움이 되었습니까?

해결책

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);
  },
);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top