I have an application that reads the oplog.rs collection in a mongodb 2.2 replica set by querying on the ts field.

I added an index on the ts field, but it doesn't get updated when new entries are inserted into the oplog.

What am I missing? I can't find anything in the mongodb docs about indexes on capped collections are not supported (rather the opposite), and I can't find any info about the oplog being special.

Thanks

Solution 1

As you've discovered, secondary indexes are not supported on system collections such as local.oplog.rs and *.system.profile. In MongoDB 2.4 and older the indexes would appear to have been created, but were never actually updated. Newer versions of MongoDB (2.6+) return an error if you try to directly update a system collection with an unsupported change such as attempting to create additional indexes.

The oplog.rs collection is definitely "special" because its intended use is only for replication. The replication internals make some assumptions about expected operations for the oplog on this basis. For example, replication only needs to insert oplog entries -- unlike a capped collection that you may create yourself, oplog entries are never updated.

Applications are expected to read the oplog with a tailable cursor if they need to follow new entries that are inserted into the oplog, or to do a find using $natural order.

The tailable cursor tutorial goes into some more detail on usage, but a few particular points to note are:

  • Tailable cursors do not use indexes and return documents in natural order.
  • Because tailable cursors do not use indexes, the initial scan for the query may be expensive; but, after initially exhausting the cursor, subsequent retrievals of the newly added documents are inexpensive

Solution 2

What am I missing? I can't find anything in the mongodb docs about indexes on capped collections are not supported (rather the opposite), and I can't find any info about the oplog being special.

Your question is a litle vague as to exactly how you are going about your current implementation. However, it seems that it is not that the index is not updated but rather you are not notified of updates because MongoDB has no pub/sub feature yet: https://jira.mongodb.org/browse/SERVER-3385

You could use a tailable cursor on this collection ( http://docs.mongodb.org/manual/tutorial/create-tailable-cursor/ ) to actually get updates over a prolonged time.