MongoSnapshotStore
The SnapshotStore of one bounded context on MongoDB (documentation/designs/03-log-and-snapshot.md §3).
Each aggregate type has its own collection, wow_snapshot__<context>__<aggregate>, holding one document per aggregate, whose _id is its key {t: tenant, a: aggregate id}: no index besides _id_ keeps it unique, and the key's order is the tenant's, then the aggregate id's. The document also holds the two as tenant_id and aggregate_id, which the queries read, and every write's filter names them, the shard key of a sharded collection; it has no aggregate name, the collection is the aggregate type. An aggregate type's collection is created, with its key and format record, the first time this store touches the type, or up front by createCollection. Two partial indexes cover the predicates the query side adds to every query: {tenant_id, owner_id} and {tenant_id, space_id}, over the snapshots that are not deleted. The state and the access attributes are BSON documents, so the snapshot can be queried in place as the default read model; the state's decimals are Decimal128, exact (ExactBson). A save is one conditional upsert whose filter carries the version and state revision guard (version < new, the stored state_revision not above the new one): no transaction, and concurrent saves leave the highest version (see conditionalUpsert). A save refused for a higher state revision then marks the document stale (stale_version). The claimed state revision of each collection is a document of TABLES, keyed by the collection.
Calls run on dispatcher, virtual threads by default. A failure of the database is a defect and is thrown: a snapshot is a cache, so the caller may drop it and rebuild later.
Properties
CATCH_UP_WRITES_IN_FLIGHT while the subscription is behind, the default two otherwise. A write here is one unordered bulkWrite of at most the batch, which returns only once a majority journaled it (MongoDatabases), and mongod spends about the same a snapshot whatever the batch (0.08–0.14 ms, measured 2026-11-13): calls at once scale, a larger batch does not. Two keep up below the peak with their calls large; at the peak, where every batch is full, two held the subscription at their bound and four kept up. Four is what the subscription had in flight before the calls were merged across partitions (four lanes, each one call at a time).
This store rewrites its snapshots in place: it is its own rebuild control plane.
Functions
Creates the key index of aggregateName's collection when missing, once per store; refuses snapshots recorded in another format version (MongoFormat).
Nothing to create up front: each aggregate type's collection is created on its first use (createCollection), since the store does not know the context's aggregate types. Kept for the host, which calls it at startup.
Makes type's collection and its indexes when missing, then resets its record while its generation is below generation (SnapshotRebuildStore.renew); the record after, or null when it has none.
Saves snapshots: in the rounds SnapshotSaveOutcome.rounds gives, each round's snapshots of one aggregate type with one unordered bulkWrite of conditional upserts (MongoSnapshotWrites), each the save's own modifier $set, guarded as save guards one and over a document without a stale mark. Each operation either applied (it matched and wrote the snapshot, or found no document and inserted it: written) or met the key with its insert (the document exists and the filter did not match it: the guard refused the version, the document carries a stale mark, or a concurrent insert won the key). Such a snapshot is saved again alone, as save would (conditionalUpsert), which tells the three apart: it writes over a stale mark and clears one of a version up to its own, and marks a higher state revision stale. So a batch that all lands is one round trip; a marked document, during a rebuild, costs its save alone; one the server refused with another error, or whose state cannot be BSON, is SnapshotSaveOutcome.Failed alone.