Lenny
Lenny

Reputation: 164

Do expiring documents appear in the oplog in MongoDB?

I have a collection with a TTL index configured to expire documents after a certain amount of time. Does those deletions appear in the oplog?

Upvotes: 0

Views: 406

Answers (1)

Blakes Seven
Blakes Seven

Reputation: 50406

Of course they do. It is a basic principle of the oplog that it's primary purpose to to record database changes that need to be populated to other replica set members.

Therefore every single change needs to be recorded so that other members can read this information and "replay" those changes themselves in order to keep the state consistent.

All a TTL really does is place a marker on a collection index that allows it to be picked up by a standard running thread within the mongod process. When this process periodically runs, all items in the marked collection that have dates beyond the limits specified are processed with a standard .remove() operation:

db.collection.remove({ "date": { "$lte": new Date( Date.now - ttlTime ) } })

As effectively being the sort of operation ( "serialized" ) that you will see occur.

Naturally this thread only makes sense to run on a "PRIMARY" node as well, as just like all normal operations, this is the place where all "write" operations occur.

Upvotes: 3

Related Questions