Using findOne in mongodb to get element with max id

后端 未结 5 1806
别那么骄傲
别那么骄傲 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

    You can get max _id using aggregation of mongodb. Find and sort may overkill's.

    db.myCollection.aggregate({
        $group: {
            _id: '',
            last: {
                $max: "$_id"
            }
        }
    });
    

提交回复
热议问题