How I can use sort function in findOne() function?

both methods are not working with findOne :

await colllections.findOne({}, { sort: { timestamp: -1} })

await colllections.findOne({}).({ sort: { timestamp: -1} })

The function findOne returns only one document. Why do you want to sort a single document?

I faced the same issue. There might be multiple documents that match find operator so I need to sort them to prevent ambiguity.
I found that solution: findOne({}, null, { sort: { timestamp: -1} })

1 Like

findOne( ) returns the first document found with the given criteria. The criteria could match multiple documents before that “first” is returned.
So, it is important to be able to pre-sort in some situations, to get a predictable result.

In any case @Anton_P 's solution addresses this, so we’re good.

1 Like

It makes a lot of sense. Thanks for the clarification.

An alternative is probably using

db.collection.find(filter).sort(xxx).limit(1)

1 Like

If you want to just find the latest inserted document as well, the ID field created by MongoDB actually has date associated with it, so you can sort for the latest N documents:

db.foo.find().sort({_id:-1}).limit(x);

If you use this approach, make sure that all you IDs were generated by Mongo. Custom IDs are possible, including arbitrary objects IDs that look just like auto generated ones.

Since the _id is generated by the driver it is possible to have an “older” _id from a more recent document created by a driver running of a different machine where the clock is off.

The order might also be off if 2 clients create 2 documents with the same timestamp when the driver with lowest (5 bytes random value) creates the document after.

To really sort the document on the creation time, the safest way is to use a field initialized with $currentTime because $currentTime is evaluated by the server.

actually findOne gives a promise . When you chain methods like sort() and lean() after findOne() , you are building up the query object with additional instructions.
In the case of Mongoose queries, the promise is resolved internally by Mongoose when you either call an executor method like exec() or use await to await the query. The findOne() method itself returns a Query object, which is a thenable object representing a pending operation.

To summarize, the promise returned by the findOne() method is resolved by calling an executor method or using await to await the query. The sort() and lean() methods help build the query but do not directly resolve the promise.

If you are frequently querying it, best way is to create an index and provide index hint for the query. So the findOne query will be executed directly on the index, which we will be sorted by descending order.

collections.index({ timestamp: -1 });

await colllections.findOne({}).(hint: { timestamp: -1} )

``
`await collections.find({})
.sort({ timestamp: -1 })
.limit(1)
.hint({ timestamp: -1 })
.toArray();

@Abul_Fazal1 , you can also use this way as well