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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…