rwynn / gtm

gtm (go tail mongo) is a MongoDB event listener
MIT License
146 stars 33 forks source link

What is the difference between Op.Data and Op.Doc ? #24

Open cmfunc opened 3 years ago

cmfunc commented 3 years ago
type Op struct {
    Id                interface{}            `json:"_id"`
    Operation         string                 `json:"operation"`
    Namespace         string                 `json:"namespace"`
    Data              map[string]interface{} `json:"data,omitempty"`
    Timestamp         primitive.Timestamp    `json:"timestamp"`
    Source            QuerySource            `json:"source"`
    Doc               interface{}            `json:"doc,omitempty"`
    UpdateDescription map[string]interface{} `json:"updateDescription,omitempty"`
    ResumeToken       OpResumeToken          `json:"-"`
}

I want to get latest document that have been change. for example, in "lessons" mongo collections, I update "$unset" the "room_id" filed of "lessons" collections , I need the doc data without "room_id" field. I shouled select Op.Data and Op.Doc ? Op.Doc is value of Op.Data field "Doc" ?

rwynn commented 3 years ago

Op.Data was added first, it is your MongoDB data as a go map. Later, Op.Doc was added so that the type could be changed to interface{}. This allows one to do custom unmarshalling into your own struct.

https://github.com/rwynn/gtm#custom-unmarshalling

If you don't do any custom unmarshalling into your own struct, then both fields get the go map.