Get latest MongoDB record by field of datetime

后端 未结 2 1661
旧时难觅i
旧时难觅i 2020-12-05 09:29

I have a collection like this:

{
    \'datetime\': some-date,
    \'lat\': \'32.00\',
    \'lon\': \'74.00\'
},
{
    \'datetime\': some-date,
    \'lat\': \         


        
相关标签:
2条回答
  • 2020-12-05 10:06

    Use sort and limit:

    db.col.find().sort({"datetime": -1}).limit(1)
    
    0 讨论(0)
  • 2020-12-05 10:07

    For node.js you can use the findOne() function:

    db.collection('yourCollectionName').findOne(
      {},
      { sort: { datetime: -1 } },
      (err, data) => {
         console.log(data);
      },
    );
    
    0 讨论(0)
提交回复
热议问题