3

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.

shx2
  • 61,779
  • 13
  • 130
  • 153
  • The only explanation I can think of is if you have an index, since the tail *must* use natural order, the hint must be specified to avoid using the other index. – joao Oct 26 '14 at 13:49
  • @joao I would have thought so, but the docs say: "Tailable cursors do not use indexes"... – shx2 Oct 26 '14 at 14:49

1 Answers1

2

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.

Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89