Skip to main content

Overview

ChangeStreamContext<T> is the main runtime object passed to every handler method (@OnChange, @OnInsert, @OnUpdate, @OnDelete, @OnReplace). It provides access to the event metadata, documents, and runtime actions.

Document access

getFullDocument

Returns the full document after the operation, automatically converted to the requested type using Spring’s MongoConverter.
For DELETE operations, the full document is not available and the method returns Optional.empty().

getFullDocumentBeforeChange

Returns the document before the operation, automatically converted using Spring’s MongoConverter. This is useful for comparing old vs. new state (e.g., audit logging, status transition tracking).
Two prerequisites are required for pre-images to work:
  1. MongoDB 6.0+ — pre-images are not available on earlier versions.
  2. Pre-images must be enabled on the collection — MongoDB does not store pre-images by default. Run this command once per collection:
Or in Java with Spring’s MongoTemplate:
The @ChangeStream annotation controls how pre-images are requested:

getUpdateDescription

For UPDATE operations, returns an UpdateDescription describing the modified fields. Returns Optional.empty() for non-UPDATE operations.

UpdateDescription methods

Targeted field changes

hasFieldChanged and getUpdatedFieldValue let you react to specific fields without manually walking the updated-fields map:
Paths use dot notation for nested fields (e.g. "address.city").
Custom conversion — By default, getFullDocument(MyType.class) uses Spring’s MongoConverter, which respects your @Field annotations, custom converters registered in MongoCustomConversions, etc.If you need a completely different conversion strategy (e.g. Jackson, Gson), retrieve the raw BSON document and convert it yourself:

Event metadata

TransactionInfo

MongoDB emits lsid (logical session id) and txnNumber together for every event in a transaction, or omits both for standalone operations. They’re grouped in a record so handlers cannot observe one without the other; absence is represented by Optional.empty() at the call site. Useful for grouping events of the same transaction — audit logs, aggregation, atomicity-preserving downstream propagation.

Actions

sendToDlq

Manually routes the current event to the Dead Letter Queue.

saveCheckpointNow

Forces an immediate checkpoint save for the current resume token.

Custom metadata

Attach arbitrary key-value pairs to the event for downstream processing.