Skip to content

Commit cf603dd

Browse files
committed
add write action
1 parent d184fb5 commit cf603dd

15 files changed

Lines changed: 2254 additions & 10 deletions

CHANGELOG.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@
55
## [未发布]
66

77
### 新增
8+
- **insertOne 和 insertMany 写操作文档补充**(2025-11-10)
9+
- README.md:添加"写入操作"表格和详细使用示例(第 8 节)
10+
- index.d.ts:添加 TypeScript 类型声明(InsertOneOptions、InsertOneResult、InsertManyOptions、InsertManyResult、WriteConcern)
11+
- docs/write-operations.md:创建详细文档(全面的 API 说明、使用示例、性能对比、最佳实践、常见问题)
12+
- 完整遵循 Guidelines v2.md 第 3.1 章"四要素"(Code ✅、Tests ✅、Examples ✅、Documentation ✅)
13+
14+
- **insertOne 和 insertMany 写操作支持**(2025-11-10)
15+
- 实现 `insertOne(document, options)` - 单文档插入操作
16+
- 支持参数:writeConcern, comment, bypassDocumentValidation
17+
- 返回:`{ acknowledged, insertedId }`
18+
- 自动缓存失效:插入成功后自动失效该集合的所有查询缓存
19+
- 慢查询日志:插入耗时超过 slowQueryMs 时记录
20+
- 错误处理:检测重复键错误(E11000)并转换为标准错误码 `DUPLICATE_KEY`
21+
- 实现 `insertMany(documents, options)` - 批量文档插入操作
22+
- 支持参数:ordered(默认 true), writeConcern, comment, bypassDocumentValidation
23+
- 返回:`{ acknowledged, insertedCount, insertedIds }`
24+
- 批量插入性能优化:500 文档批量插入比 500 次单次插入快约 50 倍
25+
- 有序模式(ordered: true):遇到错误停止,已插入文档不会回滚
26+
- 无序模式(ordered: false):遇到错误继续插入其他文档
27+
- 自动缓存失效:批量插入成功后自动失效该集合的所有查询缓存
28+
- 创建文件:`lib/mongodb/writes/insert-one.js`(136 行)、`lib/mongodb/writes/insert-many.js`(184 行)
29+
- 测试覆盖:insertOne 测试 17 个用例,insertMany 测试 21 个用例(包括性能对比测试)
30+
- 示例文件:`examples/insertOne.examples.js``examples/insertMany.examples.js`
31+
- 错误码支持:新增 `DUPLICATE_KEY` 错误码(用于重复键检测)
32+
- 测试运行器增强:支持 `beforeEach/afterEach` 钩子,支持 `this.timeout()` 方法
33+
834
- **readPreference 副本集读偏好支持**(2025-11-07)
935
- 在连接配置中添加 `readPreference` 选项,支持副本集读写分离场景
1036
- 支持 5 种读偏好模式:`primary`(默认)、`primaryPreferred``secondary``secondaryPreferred``nearest`

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ const MonSQLize = require('monsqlize');
8888
| **count()** | 统计文档数量 | [docs/count.md](./docs/count.md) |
8989
| **explain()** | 查询计划分析 | [docs/explain.md](./docs/explain.md) |
9090

91+
### 写入操作
92+
93+
| 方法 | 说明 | 文档链接 |
94+
|------|------|---------|
95+
| **insertOne()** | 插入单个文档 | [examples/insertOne.examples.js](./examples/insertOne.examples.js) |
96+
| **insertMany()** | 批量插入文档(10-50x 性能提升) | [examples/insertMany.examples.js](./examples/insertMany.examples.js) |
97+
9198
### 集合管理
9299

93100
| 方法 | 说明 | 文档链接 |
@@ -352,6 +359,49 @@ msq.on('error', (data) => {
352359

353360
---
354361

362+
### 8. 写入操作(insertOne / insertMany)
363+
364+
```js
365+
// 插入单个文档
366+
const result1 = await collection('products').insertOne({
367+
document: {
368+
name: 'iPhone 15 Pro',
369+
price: 999,
370+
category: 'electronics',
371+
createdAt: new Date()
372+
},
373+
comment: 'ProductAPI:createProduct:user_123' // 日志跟踪(可选)
374+
});
375+
376+
console.log('插入成功:', result1.insertedId);
377+
378+
// 批量插入文档(10-50x 性能提升)
379+
const result2 = await collection('products').insertMany({
380+
documents: [
381+
{ name: 'MacBook Pro', price: 2499, category: 'electronics' },
382+
{ name: 'iPad Air', price: 599, category: 'electronics' },
383+
{ name: 'AirPods Pro', price: 249, category: 'accessories' }
384+
],
385+
ordered: true, // 遇到错误时停止(默认)
386+
comment: 'ProductAPI:batchImport'
387+
});
388+
389+
console.log(`成功插入 ${result2.insertedCount} 条数据`);
390+
391+
// 自动缓存失效(插入后自动清理相关缓存)
392+
// 无需手动调用 invalidate()
393+
```
394+
395+
**性能对比**:
396+
- 单条插入(insertOne): ~10-20ms/条
397+
- 批量插入(insertMany): ~0.5-1ms/条 **(10-50x 更快)**
398+
399+
**详细示例**:
400+
- [examples/insertOne.examples.js](./examples/insertOne.examples.js) - 8 个完整示例(基础/错误处理/缓存失效/性能对比)
401+
- [examples/insertMany.examples.js](./examples/insertMany.examples.js) - 8 个完整示例(ordered/unordered 模式/性能测试)
402+
403+
---
404+
355405
### 5. 多层缓存(本地内存 + Redis)
356406

357407
```js

STATUS.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
|------|----------|----------|----------|-------------|------|
1818
| **核心功能** | 23 | 2 | 5 | 2 | 32 |
1919
| **MongoDB 读方法** | 9 | 0 | 3 | 0 | 12 |
20-
| **MongoDB 写方法 - Insert** | 0 | 0 | 2 | 0 | 2 |
20+
| **MongoDB 写方法 - Insert** | 2 | 0 | 0 | 0 | 2 |
2121
| **MongoDB 写方法 - Update** | 0 | 0 | 5 | 0 | 5 |
2222
| **MongoDB 写方法 - Delete** | 0 | 0 | 3 | 0 | 3 |
2323
| **MongoDB 写方法 - Bulk** | 0 | 0 | 1 | 0 | 1 |
2424
| **MongoDB 索引** | 0 | 0 | 5 | 0 | 5 |
2525
| **MongoDB 事务** | 0 | 0 | 3 | 0 | 3 |
2626
| **MongoDB 其他** | 0 | 0 | 15 | 0 | 15 |
27-
| **总计** | **32** | **2** | **42** | **2** | **78** |
27+
| **总计** | **34** | **2** | **40** | **2** | **78** |
2828

29-
**完成度**: 41.0% (32/78)
29+
**完成度**: 43.6% (34/78)
3030
**核心功能完成度**: 71.9% (23/32)
3131

3232
---
@@ -152,8 +152,8 @@
152152
- ✅ 已支持: hint, collation, batchSize, comment (find/findOne/count/aggregate), **readPreference (全局配置)**
153153

154154
### MongoDB 方法(Writes - Insert)
155-
- insertOne
156-
- insertMany
155+
- insertOne
156+
- insertMany
157157

158158
### MongoDB 方法(Writes - Update)
159159
- ❌ updateOne

0 commit comments

Comments
 (0)