Document Preimages
On this page
Overview
Every database trigger execution has a related change event. You can configure these change events to include document preimages. A preimage is a snapshot of a document before a change.
Example
Consider updateOne
in a collection with preimages:
pets.updateOne( { name: "Fido" }, { $inc: { age: 1 } } )
Preimages add the following data to write operation change events
the document preimage in the
fullDocumentBeforeChange
fieldPreimage - the full document as it existed before the change{ "name": "Fido", "age": 3 } the document postimage in the
fullDocument
fieldPostimage - the full document immediately after the change{ "name": "Fido", "age": 4 }
Performance
For clusters running MongoDB version 6.0 and newer, triggers use the cluster's built-in change stream preimages feature. Older versions of MongoDB store preimages directly in the oplog. Both of these add storage and compute overhead for each operation on the collection which may cause performance issues for collections with a high write throughput.
Enable Document Preimages
You can enable preimages when you configure a database trigger. Document Preimage is a setting on trigger configuration. Toggle this to enable document preimages for the collection.
View Preimage-Enabled Collections
To view the list of collections that store document preimages:
Navigate to the Linked Data Source configuration screen.
Expand the Advanced Configuration section.
This section contains the Preimage Preferences by Collection table.
This table lists every collection that stores preimages in the oplog. Enabling preimages for a collection applies to all collections. This includes collections tied to triggers in a different App. Collections with no triggers that use preimages also appear here.
Disable Preimages for a Collection
You can disable collection-level preimages in the App Services UI.
To disable preimages for a collection:
(Optional) Disable document preimages for triggers in the collection. Triggers with preimages continue to fire after disabling preimages. But they don't have the
fullDocumentBeforeChange
field in change events. This includes triggers in other App Services Apps.Terminate sync, if the collection is part of a synced cluster.
View the Preimage Preferences By Collection table. Press the Disable button for a collection. This turns off preimages for that collection.
Re-enable Sync, if the collection is part of a synced cluster.
Warning
Restore Sync after Terminating Sync
When you terminate and re-enable Atlas Device Sync, clients can no longer Sync. Your client must implement a client reset handler to restore Sync. This handler can discard or attempt to recover unsynchronized changes.