Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is hint({$natural: 1}) redundant when using a tailable cursor?

In many examples I find for using tailable cursors on capped collections, the code includes:

hint( { $natural: 1 } )

(e.g. here), including the official docs (here), to "ensure we don't use any indexes", and that results are returned in natural (i.e. disk) order.

However, the docs also suggest this is the default behavior of tailable cursors:

Tailable cursors do not use indexes and return documents in natural order.

So is the use of the hint redundant?

I tried looking at the output of explain() with and without the hint, and as far as I can tell, there was no difference.

like image 689
shx2 Avatar asked Jan 24 '26 22:01

shx2


1 Answers

Yes, it is.

What you might want to do sometimes is to return the result in reverse natural order (the last document inserted the first to be returned. You can achieve this by:

db.cappedCollection.find().sort({$natural:-1})

effectively changing the FIFO nature of a capped collection to a LIFO nature.

like image 161
Markus W Mahlberg Avatar answered Jan 27 '26 15:01

Markus W Mahlberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!