MongoDB Change Stream

Watches a MongoDB database for real-time change events and emits each change document as a JSON event.

Configuration

- mongodb_change_stream:
    name: watch_orders
    credentials_path: /etc/mongodb/credentials.json
    db_name: my_database

Fields

FieldTypeDefaultDescription
namestringrequiredTask name.
credentials_pathstringPath to MongoDB credentials file. Omit to connect to localhost:27017 without authentication. See Credentials.
db_namestringrequiredDatabase name to watch for changes.
depends_onlistUpstream task names.
retryobjectRetry configuration.

Output

FormatCrateDescription
JSONmongodbThe changed document, converted to JSON. See Behaviour.
Event fieldValue
event.subjectCollection the change happened in; the database name for a database-level change.
event.idThe change’s resume token, unique per change.
event.meta.document_keyThe changed document’s key ({"_id": ...}), absent for a collection or database change.
event.meta.databaseDatabase the change happened in.
event.meta.operation_typeChange type: insert, update, replace, delete, drop, rename, dropDatabase, or invalidate.

Behaviour

The change stream watches the entire database and emits every change. event.data is the changed document for an insert or a replacement, and the current version of the document for an update, looked up when the change is read; an update to a document deleted before the lookup carries null. For a delete it is the deleted document’s key ({"_id": ...}), and for a collection or database change such as a drop it is null. Route on event.meta.operation_type to handle each kind.

The stream reconnects automatically on connection loss using an infinite retry loop with exponential backoff and jitter.

:::caution[No resume token] The reader does not store MongoDB resume tokens. After a reconnection or restart it opens a new change stream, which starts at the current point in the oplog, so changes made while the reader was disconnected are not emitted. :::