> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# MongoDB collection observations

> Read one ObjectId-keyed MongoDB collection into your app over TLS.

Mount `@bijectionhq/app-documents` to observe one MongoDB 6.0 or later
collection under the label you pass as `data`.

## Mount and start

`documents({ data, provider })` returns the app to place at a path of your
tree. Bind the collection with `mongodbCollection(endpoint, credential, label)`.

```ts app.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, label, secret } from "@bijectionhq/bijection";
import documents, { mongodbCollection } from "@bijectionhq/app-documents";

const products = label();
const credential = secret();
const collection = mongodbCollection("mongo.example.com:27017", credential, products);

export default app({ products, credential, collection, docs: documents({ data: products, provider: collection }) });
```

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
bijection secret set credential --from-file mongo.json
bijection mutate docs.source.initialize '{"selection": {"mode": "mongo"}}'
```

`docs.operator` is granted from code: `ctx.grant(docs.operator, { to, under })` in one of your
mutations ([Grants](https://docs.bijection.com/access/grants.md)).

Set the credential to JSON with four strings.

| Field | Value |
| - | - |
| `database` | Database name |
| `collection` | Collection name |
| `subject` | The X.509 principal registered in MongoDB's `$external` database |
| `identity` | PEM client certificate chain followed by its private key |

Database and collection names may contain ASCII letters, digits, hyphens
and underscores. Grant `listCollections` on the database and `find` on the
selected collection. The server certificate must be valid for the configured
host under the deployment's platform trust. The connector uses one direct,
TLS-enabled endpoint with socket egress and makes no topology discovery.

Use one label for the receiver and the mount. The mount's `operator` role
clears it and is the only role `docs.source.*`, `docs.documents.list` and
`docs.documents.get` allow. The host charges retained work against that
label's reservation.

## Paging and recovery

Each exchange reads at most one document in ascending `_id` order. The app
requires ObjectId keys and retains typed document images. The `list` and
`get` queries render each payload as canonical Extended JSON.
It admits at most 36 KiB of document JSON and 48 KiB of encoded answer
record. BSON nesting and field counts are bounded. JavaScript with scope
is outside this mode.

The driver reads the collection UUID before and after the page. A collection
replacement stops advancement and keeps the prior cursor. Use
`source.health` to get the revision and gap instant for `source.reset`.
Resetting starts a new traversal and keeps earlier observations under their
original collection identity. Each row and its position commit together.

Traversals repeat after reaching the end. Pages can observe concurrent
writes. The app makes no snapshot or deletion-by-absence inference.

## Follow a change stream

Use the same mount with `{ selection: { mode: "mongo-changes" } }`. Point
the fixed endpoint at a MongoDB 6.0 or later replica-set member that can
serve change streams. Grant `changeStream` and `find` on the collection
and keep the existing `listCollections` grant. Enable
`changeStreamPreAndPostImages` on the collection and retain its images and
oplog long enough for the poller to resume.

The first read records the stream head. This mode captures subsequent
independently committed events, without a table scan. It applies inserts,
replacements and updates using exact post-images, and deletes by ObjectId.
Each accepted event commits with its opaque BSON resume token. An empty
batch can advance to the provider's post-batch token. The connector
closes the provider cursor after each read and requests `resumeAfter`
with the committed token on the next read.

One event can occupy up to 36 KiB of document JSON and 48 KiB of encoded
answer. Transactions carrying a session and transaction number, missing
required post-images and oversized events block advancement.
An invalidated stream, a lost resume token or a changed collection UUID
requires explicit reset. Resetting starts at the current head and retains
previously published rows. Deletion by absence is outside this mode.

[MongoDB X.509 client configuration](https://www.mongodb.com/docs/manual/core/tls/configure-client-tls-tutorial/)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.