I have a bunch of records and I need to get latest (most recent) and the oldest (least recent).
When googling I found this topic where I saw
Get 10 latest documents
MySchema.find().sort({ _id: -1 }).limit(10)
Get 10 oldest documents
MySchema.find().sort({ _id: 1 }).limit(10)
In case you want sorting based on some other property i.e. createdAt and get the oldest or latest. Again it is similar to the above query.
MySchema.find().sort({ createdAt: 1 }).limit(10) // oldest docs
MySchema.find().sort({ createdAt: -1 }).limit(10) // latest docs