MongoDB Change Stream: Can I get value before update/delete?

2.7k Views Asked by At

Change Stream Event on an update operation just return the document that changes to, same as oplog. Can I get the document (or some updated values) before update?

MySQL row-based binlog can do this with full binlog_row_image.

2

There are 2 best solutions below

1
On BEST ANSWER

No, from change stream an update event looks like:

{
   _id: { < Resume Token > },
   operationType: 'update',
   clusterTime: <Timestamp>,
   ns: {
      db: 'engineering',
      coll: 'users'
   },
   documentKey: {
      _id: ObjectId("58a4eb4a30c75625e00d2820")
   },
   updateDescription: {
      updatedFields: {
         email: '[email protected]'
      },
      removedFields: ['phoneNumber']
   }
}

Only the new values are present, unlike MySQL where you get both after and before.

1
On

In the findAndModify command you have an option to specify (new : true/false) whether you return the new version (true) or the original version of the document (false, this is the default)