Mongodb Change Stream get previous document snapshot

Hello,
I just realise that there is no way to get the previous snapshot of the document.
Before using Mongodb I have used Firebase wherein using Cloud Firestore Triggers you get both previous and current documents.

Thank you

Hi @Akash_Kumar6,

I appreciate your frustration, however there are more constructive ways to share your feedback and publicly engage with other community members. Please review our Community Code of Conduct for expectations around community participation including positive and respectful communication.

I won’t ask you to upvote or watch the feature request to Extend update and change events with document pre-image as this was completed last year and available in MongoDB Realm Database Triggers (which are analogous to the Firebase solution you referenced).

I would note that product signals like feature requests and upvotes are important for the product planning process, and are the expected path to help our team understand which features are of most interest to end users.

Thanks,
Stennie

4 Likes

Hi, @Stennie_X,

Thanks for your answer.
Is there any update on when we can get the same feature in Change Stream?

Thanks.

Hi @Akash_Kumar6 , the pre- and post-update snapshots of the document in Change Streams are currently planned to be released in MongoDB 6.0

3 Likes

Thanks for the update @Katya.

Hi Katya:

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. Can anyone please help?
My GO drive 1.11.1 and my MongoDB edition is 6.0.3 Community. Any input will be very helpful. Thanks.

Kevin

sorry I’m not an expert with go driver, but you also need to request to to ouput the pre-/post-image when you open change stream by using fullDocumentBeforeChange/fullDocument options on Watch()