The Document Pre- and Post-Images failed to present the previous value

I am using the Change Streams with Document Pre- and Post-Images which allows us to output a document before and after changes. Here is my sample code in Go:

    result := dbClient.Database("admin").
        RunCommand(ctx, bson.M{"setFeatureCompatibilityVersion": "6.0"})
    if result.Err() != nil {
        fmt.Println(result.Err())
    }
    ....

    collOpts := options.CreateCollection().
        SetChangeStreamPreAndPostImages(bson.M{"enabled": true})

    err := db.CreateCollection(ctx, collLog, collOpts)
    if err != nil {
        logger.Error(err, fmt.Sprintf("failed to set up an additional "+
            "collection options"))
    }
    ....
    _, err = dbHandler.Collection(collLog).InsertOne(ctx, change)

The result is no difference between Mongo 5 and 6 because it does not show the previous value in the result. Can anyone please help?

My go version is go1.17.13, GO drive 1.11.1 and my MongoDB edition is 6.0.3 Community. Any input will be very helpful.

Thanks.

Please also close my previous topic regarding “About using the changeStreamPreAndPostImages in Mongo 6.0” since I could not change the title after post. Sorry for the inconvenience. Appreciated.

Kevin

Here is my output in my Change Stream collection:

"OperationType": "update",
  "Database": "ng-real-porpoise-1",
  "Collection": "entities",
  "FullDocument": null,
  "UpdateFields": {
    "NewFields.suite": {
      "$numberLong": "1001"
    }
  },

The value of NewFields.suite has been updated from “456” to “1001” in the UpdateFields, but it shows “1001” only. There is NO previous value (i.e., the “456”) in the record.

In an update stream event, the fullDocumentBeforeChange field is missing, even with changeStreamPreAndPostImages set to true. An update event stream only contains the following fields:

  • _id
  • operationType
  • clusterTime
  • wallTime
  • ns
  • documentKey
  • updateDescription

We got it is working. Here is the sample code in GO:

    dbClient.Database("<your_db_name>").RunCommand(ctx, bson.D{
        bson.E{
            Key: "collMod", Value: "entities",
        },
        bson.E{
            Key: "changeStreamPreAndPostImages",
            Value: bson.D{
                bson.E{Key: "enabled", Value: true}},
            },
    })

    opt := options.ChangeStream().SetFullDocumentBeforeChange("required")
    csStream, err := dbClient.Watch(ctx, mongo.Pipeline{match}, opt)
    if err != nil {
        return err
    }
    defer csStream.Close(ctx)

    for csStream.Next(ctx) {
        ....
    }

Other than enabling the changeStreamPreAndPostImages, the

    opt := options.ChangeStream().SetFullDocumentBeforeChange("required")
    csStream, err := dbClient.Watch(ctx, mongo.Pipeline{match}, opt)

is the key. The Mongo doc does not say it well.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.