mongodb shell query last document

To query the last document in MongoDB shell, you can use the findOne() method with an empty filter object and the sort() method with a descending order on the _id field. Here's an example:

db.collection.findOne({}, {sort: {_id: -1}})

This query will return the last document in the collection based on the _id field, which is automatically generated by MongoDB and typically represents the insertion order.

Keep in mind that the _id field should be indexed for better performance when using this query.