> ## 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 集合

`docSnapshots` 保存文档副本：document-updater 会在某次编辑合并（折叠）评论或修订之前拍下这些副本。只有 document-updater 写入它，位置在 `services/document-updater/app/js/SnapshotManager.js`。Web 在 `services/web/app/src/models/DocSnapshot.mjs` 中有一个 Mongoose 模型，但没有代码使用它。

#### 字段

| 字段 | 类型 | 含义 | 来源 |
| - | - | - | - |
| `_id` | ObjectId | 快照 ID。 | `SnapshotManager.js:24` |
| `project_id` | ObjectId | 项目。 | `SnapshotManager.js:19,26`, `DocSnapshot.mjs:7` |
| `doc_id` | ObjectId | 文档（`docs._id`）。 | `SnapshotManager.js:20,27`, `DocSnapshot.mjs:8` |
| `version` | Number | 编辑前文档的 OT 版本。 | `SnapshotManager.js:28`, `services/document-updater/app/js/UpdateManager.js:132,264-271`, `DocSnapshot.mjs:9` |
| `lines` | Array of String | 编辑前文档的内容。 | `SnapshotManager.js:29`, `DocSnapshot.mjs:10` |
| `pathname` | String | 当时文档在项目中的路径。 | `SnapshotManager.js:30`, `DocSnapshot.mjs:11` |
| `ranges` | Mixed | 编辑前的 `comments` 和 `changes`，结构与 `docs.ranges` 相同（参见 [docs](https://ayakaleaf-pro.mintlify.site/zh-CN/dev/data-model/docs-collection.md)）。`id`、`op.t` 和 `metadata.user_id` 会转换为 ObjectId，`metadata.ts` 会转换为 Date。 | `SnapshotManager.js:31,41-68`, `DocSnapshot.mjs:12` |
| `ts` | Date | 快照的拍摄时间。TTL 索引会在 30 天后删除该快照。 | `SnapshotManager.js:32`, `DocSnapshot.mjs:13` |

#### 写入方

| 代码 | 作用 |
| - | - |
| `UpdateManager.js:248-271` | document-updater 应用一次编辑后，如果这次编辑折叠了范围（变空的范围增多，或删除了不止一个范围），就会记录编辑前的文档（`services/document-updater/app/js/RangesManager.js:104-106`）。 |
| `SnapshotManager.js:17-36` `recordSnapshot` | 插入快照。 |

#### 读取方

没有代码读取它。Web 模型虽已定义，但没有任何地方使用。

#### 索引

| 名称 | 键 | 选项 | 迁移 |
| - | - | - | - |
| `ts_1` | `{ ts: 1 }` | TTL，`expireAfterSeconds: 2592000`（30 天） | `tools/migrations/20190912145007_create_docSnapshots_indexes.mjs` |
| `project_id_1_ts_1` | `{ project_id: 1, ts: 1 }` | | `tools/migrations/20200120163346_atlas_recommended_indexes.mjs` |

`20190912145007_create_docSnapshots_indexes.mjs` 中的 `project_id_1` 索引已被 `20231101191643_drop_unused_indexes.mjs` 删除。

#### 说明

* 只有经过 `UpdateManager.applyUpdate` 的编辑才会拍摄快照。由 `HistoryOTUpdateManager` 处理的编辑不会（`UpdateManager.js:74-77`）。
* `doc_id` 上没有索引。请先按 `project_id` 和 `ts` 查找文档的快照，再按 `doc_id` 过滤。


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