Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

mongodb - Index on ts field in oplog.rs is not updated

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...