> ## Documentation Index
> Fetch the complete documentation index at: https://ayakaleaf-pro.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Doc Snapshots Collection

`docSnapshots` holds copies of a doc that document-updater takes just before an edit that collapses comments or tracked changes. Only document-updater writes it, in `services/document-updater/app/js/SnapshotManager.js`. Web has a Mongoose model in `services/web/app/src/models/DocSnapshot.mjs`, but no code uses it.

#### Fields

| Field | Type | Meaning | Source |
| - | - | - | - |
| `_id` | ObjectId | Snapshot id. | `SnapshotManager.js:24` |
| `project_id` | ObjectId | Project. | `SnapshotManager.js:19,26`, `DocSnapshot.mjs:7` |
| `doc_id` | ObjectId | The doc (`docs._id`). | `SnapshotManager.js:20,27`, `DocSnapshot.mjs:8` |
| `version` | Number | OT version of the doc before the edit. | `SnapshotManager.js:28`, `services/document-updater/app/js/UpdateManager.js:132,264-271`, `DocSnapshot.mjs:9` |
| `lines` | Array of String | Content of the doc before the edit. | `SnapshotManager.js:29`, `DocSnapshot.mjs:10` |
| `pathname` | String | Path of the doc in the project at that time. | `SnapshotManager.js:30`, `DocSnapshot.mjs:11` |
| `ranges` | Mixed | `comments` and `changes` before the edit, the same shape as `docs.ranges` (see [docs](https://ayakaleaf-pro.mintlify.site/dev/data-model/docs-collection.md)). `id`, `op.t` and `metadata.user_id` are turned into ObjectIds and `metadata.ts` into Dates. | `SnapshotManager.js:31,41-68`, `DocSnapshot.mjs:12` |
| `ts` | Date | When the snapshot was taken. The TTL index deletes the snapshot 30 days later. | `SnapshotManager.js:32`, `DocSnapshot.mjs:13` |

#### Who Writes It

| Code | What it does |
| - | - |
| `UpdateManager.js:248-271` | After it applies an edit, document-updater records the doc as it was before the edit when the edit collapsed ranges: more ranges became empty, or more than one range was removed (`services/document-updater/app/js/RangesManager.js:104-106`). |
| `SnapshotManager.js:17-36` `recordSnapshot` | Inserts the snapshot. |

#### Who Reads It

No code reads it. The web model is defined but not used anywhere.

#### Indexes

| Name | Keys | Options | Migration |
| - | - | - | - |
| `ts_1` | `{ ts: 1 }` | TTL, `expireAfterSeconds: 2592000` (30 days) | `tools/migrations/20190912145007_create_docSnapshots_indexes.mjs` |
| `project_id_1_ts_1` | `{ project_id: 1, ts: 1 }` | | `tools/migrations/20200120163346_atlas_recommended_indexes.mjs` |

The `project_id_1` index of `20190912145007_create_docSnapshots_indexes.mjs` was dropped by `20231101191643_drop_unused_indexes.mjs`.

#### Notes

* Only edits that go through `UpdateManager.applyUpdate` take snapshots. Edits handled by `HistoryOTUpdateManager` do not (`UpdateManager.js:74-77`).
* There is no index on `doc_id`. Find the snapshots of a doc by `project_id` and `ts`, then filter on `doc_id`.


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