Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do the oplog fields actually mean?

Tags:

mongodb

I've seen this question posed before, but the answers were very vague. I have been doing some research on oplog, and am trying to understand exactly how it works. In particular, I want to have a good understanding of the fields in an oplog document and what data they store.

These are the fields I have found through tests and what I think they mean as well as what I am still unsure of:

  • ts: timestamp of the write operation / oplog entry
  • h: a unique identifier for the oplog entry (but why is it sometimes positive and sometimes negative?)
  • op: type of operation performed (usually i/u/d for insert, update or delete)
  • ns: database & collection affected
  • o: the new state of the document after performing the change
  • o2: Seems to contain the _id field of the document during an update operation. Why is this needed when that same field is present as part of the o field, which also contains the rest of the document?
  • b: Seems to be a bool that appears for delete operations. What is the significance of this field?

I would like to confirm whether or not the points I made above are accurate, as well as clarifications for the bits that aren't clear. I am also interested to know if there any other fields that can appear in an oplog document.

like image 622
CynicalProgrammer Avatar asked Nov 14 '14 17:11

CynicalProgrammer


1 Answers

  • h is a hash (signed Long)
  • ts is the internal timestamp format (the "\x11" type shown at bsonspec.org; search the API docs for your driver at api.mongodb.org for further information)
  • you are correct on op, ns, o, and o2
  • there's also a "v" field (I'm gonna speculate that this is version, which would allow them to update the schema for the oplog).
  • b is True for all the delete operations I could find, so I can't provide any information.

The best source of documentation I've found is this. It was a presentation by a company called Stripe at 2014's MongoDB World conference, and it includes some sample Ruby code.

like image 90
David Willis Avatar answered Oct 03 '22 23:10

David Willis