Scanning Existing Data
A change stream reports what happens next. A scanner reads what is already there — a collection, a page at a time, in the order of an indexed key.
That covers the cases a change stream cannot: seeding a newly added sink with history, re-loading a range after a bad transform, re-processing a past window, or running a transform over real records. Left unbounded it also does something a change stream does, in a different way — it keeps following new documents as they arrive.
Scanner or Change Stream?
This is the decision worth getting right, and it turns on one question: does the data change after it is written?
| Change stream | Scanner | |
|---|---|---|
| Inserts | Yes | Yes |
| Updates | Yes | Only when ordered by a field that changes on write |
| Deletes | Yes | No |
| Reads history already in the collection | No — starts from now | Yes |
| Resume after a long outage | Limited by the oplog window | Unlimited — resumes from a document key |
| Permissions needed | Read — plus a metadata write to enable pre- and post-images | Read only |
| Replica set required | Yes | No |
An _id-ordered scan sees inserts only
Ordering by
_id, a scanner reports documents as they are first written. An update to a document the scan has already passed does not come back, and deletes are invisible. This is the single most important thing to know before choosing a scanner over a change stream.
So:
- Append-only data — event logs, audit trails, time series, telemetry, anything generated — a scanner is a complete alternative to a change stream, and in several respects a sturdier one.
- Mutable collections — a scanner complements a change stream rather than replacing it. Use a scan to load the history, and a change stream to keep up with changes.
Ordering by a field that is updated on every write, such as updated_at, brings modified documents around again — at the cost of re-emitting the whole document each time, and still without seeing deletes.
Why a Scanner Even Where a Change Stream Works
No oplog dependency. A change stream resumes from a token that is only valid while the corresponding entry remains in the replica set’s oplog. A pipeline stopped longer than the oplog window cannot resume from where it left off. A scan resumes from a document key instead, so a pipeline stopped for a week continues exactly where it stopped — the collection is the history, and there is no window to fall out of.
Lower privilege. Enabling a change stream with pre- and post-images requires a metadata write against the collection. A scan only ever issues a read, so it can run through a credential that has no write permission at all.
No replica set. Change streams require a replica set. A scan does not.
Configuring a Scanner
A scanner is a service, not a pipeline setting — the same as a generator. How to traverse the data belongs to the scanner; what to traverse stays on the pipeline’s source stage, whose resource names the collection as it does for any other source. A pipeline that needs a different range names a different scanner service.
| Field | Meaning |
|---|---|
| Target service | The MongoDB service holding the data. Its credentials stay with it — a scanner never carries its own. |
| Order by | The field the scan pages through. Must be indexed. Defaults to _id. |
| From | Exclusive lower bound. Omit to start at the beginning. |
| To | Inclusive upper bound — and the setting that decides whether the scan ever ends. |
| Filter | Equality-only. Applied before ordering. |
| Page size | Documents read per page, which is also the pipeline’s batch size. Defaults to 500. |
Todecides whether the scan finishesThere is no mode to choose, because the upper bound already says which behaviour you want.
With
Tothe scan stops on reaching the bound and the pipeline finishes — a backfill, a re-load, a past window.Without
Tothe scan reads everything that exists and then keeps watching for what appears above its position. For an_idordering that is every new insert, because a new document’s identifier always sorts above the current position.
Only MongoDB can be scanned. A scan needs an ordered, indexed key to page through: Google Pub/Sub has no such key — a subscription only delivers what arrives after it exists — and Kafka does not need one, because a Kafka source already reads from an offset or from a point in time. Both are refused when the scanner service is saved rather than when a pipeline first runs.
The Equality-Only Filter
The filter matches fields to values, exactly. It holds no query operators, and one that reaches it another way is refused when the service is saved:
scanner 'orders-backfill': filter on 'status' contains the operator '$ne' —
the filter matches values exactly. Use `from` and `to` for a range.
This keeps the read index-friendly and keeps arbitrary query expressions out of a hot path. Use From and To to express a range.
Running a Scan Alongside a Change Stream
Concurrent scan and change stream can regress a document
A scan reads a document at the moment its page is fetched. If a change stream over the same collection delivers a newer version of that document first, and the scan then emits the copy it read earlier, a sink that replaces by
_idwill overwrite the newer version with the older one.Nothing detects this today. Scanned events carry the time the page was read, so a transform or a sink can compare the two and discard the older — but this is not automatic, and the general fix is a conditional write that not every sink can express.
The safe patterns are to scan into a different collection from the one the change stream writes, or to let the scan finish before starting the stream.
Use Cases
- Seed a new sink. A downstream system is added to an existing pipeline and needs the history, not just what happens next: a bounded scan loads the existing records, then the regular pipeline keeps it current.
- Re-load after a bad transform. A transformation wrote incorrect data for a known range. Fix the script, then scan that range again with
FromandToto re-process exactly the affected records. - Replace a change stream on append-only data. An event or audit collection that is only ever inserted into can be followed by an unbounded scan, which removes the oplog-window failure mode entirely and needs only read access.
- Run a transform over real records. Test or apply a new transformation against production data without waiting for new events to arrive.