Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index on ts field in oplog.rs is not updated

Tags:

mongodb

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

like image 547
Jonas Bergström Avatar asked Jan 14 '23 23:01

Jonas Bergström


1 Answers

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
like image 159
Stennie Avatar answered Jan 23 '23 12:01

Stennie