Using findOne in mongodb to get element with max id

后端 未结 5 1804
别那么骄傲
别那么骄傲 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 02:04

    $maxId="";
    
    $Cursor =$collection->find();
    
    foreach($cursor as $document) { 
        $maxid =max($arr=(array($document['id'])));
    }
    
    print_r($maxid+1);
    

提交回复
热议问题