Search code examples
mongodbcapped-collections

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.


Solution

  • 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.