Changestream event doesn't contain copy of inserted document inside fullDocument field

@Bobroslav_Zhmishenko when you say that event["fullDocument"] is empty, do you mean the value for the "fullDocument" key is empty or that the type assertion to models.Player returns “ok == false”?

If it’s the latter, I think the issue may be with the type assertion here:

doc, ok := event["fullDocument"].(models.Player)

Since the type passed to stream.Decode() is a bson.M, the decoded ChangeStream event document should be of type bson.M or bson.D, not models.Player. If you want to decode the document into a models.Player, you need to pass a struct with a type models.Player so the BSON unmarshaler knows to create that type.

For example, you could define a struct with fields that match the expected change stream document:

type CSEvent struct {
	OperationType string        `bson:"operationType"`
	FullDocument  models.Player `bson:"fullDocument"`
}

Then use that struct instead of a bson.M to call Decode() in iterateChangeStream():

var event CSEvent
err := stream.Decode(&event)
// ...

If you want to confirm you’re getting some document, you can inspect the contents and type of value of event["fullDocument"] like this:

fmt.Printf(
	"(%T): %v\n",
	event["fullDocument"],
	event["fullDocument"])
3 Likes