Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extrace raw fullDocument from changed stream #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions gtm.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type Op struct {
Doc interface{} `json:"doc,omitempty"`
UpdateDescription map[string]interface{} `json:"updateDescription,omitempty"`
ResumeToken OpResumeToken `json:"-"`
RawFullDoc interface{} `json:"-"`
}

type ReplStatus struct {
Expand Down Expand Up @@ -144,6 +145,11 @@ type ChangeDoc struct {
UpdateDescription map[string]interface{} "updateDescription"
}

// extract fullDocument as bson.Raw
type fullDocOnly struct {
RawFullDoc bson.Raw "fullDocument"
}

func (cd *ChangeDoc) docId() interface{} {
return cd.DocKey["_id"]
}
Expand Down Expand Up @@ -1325,6 +1331,19 @@ func ConsumeChangeStream(ctx *OpCtx, client *mongo.Client, ns string, o *Options
ctx.ErrC <- errors.Wrap(err, "Error decoding change doc")
break
}

// get raw full document
var fullDoc fullDocOnly
if err = stream.Decode(&fullDoc); err != nil {
ctx.ErrC <- errors.Wrap(err, "Error decoding change doc")
break
}

if len(changeDoc.FullDoc) > 0 && len(fullDoc.RawFullDoc) <= 0 {
ctx.ErrC <- errors.New("extract raw fullDocument fail")
break
}

resumeAfter = changeDoc.Id
startAt = nil
startAfter = nil
Expand Down Expand Up @@ -1371,6 +1390,12 @@ func ConsumeChangeStream(ctx *OpCtx, client *mongo.Client, ns string, o *Options
Timestamp: changeDoc.mapTimestamp(),
ResumeToken: token,
}

// output raw full document
if len(fullDoc.RawFullDoc) > 0 {
op.RawFullDoc = fullDoc.RawFullDoc
}

if op.matchesNsFilter(o) {
if changeDoc.hasUpdate() {
op.UpdateDescription = changeDoc.UpdateDescription
Expand Down