> ## 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-TW/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.