Using findOne in mongodb to get element with max id

后端 未结 5 1810
别那么骄傲
别那么骄傲 2020-12-01 01:18

I am trying to retrieve one element from a mongo collection, the one with the greatest _id field. I know this can be done by querying:

db.collection.find().s         


        
5条回答
  •  长情又很酷
    2020-12-01 01:51

    with PHP driver (mongodb)
    using findOne()

    $filter=[];
    $options = ['sort' => ['_id' => -1]]; // -1 is for DESC
    $result = $collection->findOne(filter, $options);
    $maxAge = $result['age']
    

提交回复
热议问题