How to shrink MongoDB's oplog.rs collection?

1.5k views Asked by At

After storing some binary data in MongoDB 4.2.5 (3 nodes replicate set) the oplog.rs collection did grow to ca. 700MB. The binary data was removed and the data model restructured, but the oplog.rs collection stays the same size (as expected). I do understand that it's a capped collection with a maximum size and eventually it'll reuse the space. In my case though, I'd like to reclaim the space and start over. The database is used mostly for internal testing purposes. I don't mind losing some data from the oplog, but I do mind having a big oplog file, since the whole database is just a few MB.

Is it safe to use the emptycapped command on the oplog.rs collection in a replicate set scenario? Do I need to run this command on each node? Do I need to compact the collection after the deletion (last part from https://docs.mongodb.com/manual/tutorial/change-oplog-size/)?

Is there any other way to gracefully "reset" the oplog and free up the space?

1

There are 1 answers

1
Yahya On

OpLog is limited by what size you have defined in config or whether you have left it to default.

The OpLog (operations log) is a special capped collection that keeps a rolling record of all operations that modify the data stored in your databases.

It fills up to the defined size as the changes are coming through (or noops heartbeats).

If you want to reduce the size, reset the OpLog size in your config. But don't forget, larger OpLog size means you get a better OpLog window.

OpLog Window tells you how long a secondary member can be offline and still catch up to the primary without doing a full resync.