Get the latest record from mongodb collection

前端 未结 8 2069
我寻月下人不归
我寻月下人不归 2020-11-29 17:40

I want to know the most recent record in a collection. How to do that?

Note: I know the following command line queries works:

1. db.test.find().sort(         


        
8条回答
  •  春和景丽
    2020-11-29 18:26

    This will give you one last document for a collection

    db.collectionName.findOne({}, {sort:{$natural:-1}})
    

    $natural:-1 means order opposite of the one that records are inserted in.

    Edit: For all the downvoters, above is a Mongoose syntax, mongo CLI syntax is: db.collectionName.find({}).sort({$natural:-1}).limit(1)

提交回复
热议问题