-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindOneById.examples.js
More file actions
117 lines (101 loc) · 4.05 KB
/
findOneById.examples.js
File metadata and controls
117 lines (101 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* findOneById 方法示例
* 演示如何通过 _id 快速查询单个文档
*/
const MonSQLize = require('../lib');
(async () => {
console.log('🚀 findOneById 方法示例\n');
// 创建实例并连接
const msq = new MonSQLize({
type: 'mongodb',
databaseName: 'example',
config: { uri: 'mongodb://localhost:27017' }
});
try {
const { collection } = await msq.connect();
console.log('✅ 数据库连接成功\n');
// ================================
// 示例 1: 基础用法(字符串 ID)
// ================================
console.log('=== 示例 1: 基础用法(字符串 ID) ===');
const userId = '507f1f77bcf86cd799439011'; // 来自请求参数(字符串)
const user = await collection('users').findOneById(userId);
if (user) {
console.log('✅ 找到用户:', user.name);
} else {
console.log('❌ 用户不存在');
}
// ================================
// 示例 2: 字段投影(只返回需要的字段)
// ================================
console.log('\n=== 示例 2: 字段投影 ===');
const user2 = await collection('users').findOneById(userId, {
projection: { name: 1, email: 1, avatar: 1 }
});
console.log('用户信息(仅部分字段):', user2);
// ================================
// 示例 3: 使用缓存(提升性能)
// ================================
console.log('\n=== 示例 3: 使用缓存 ===');
console.time('第1次查询(无缓存)');
const user3a = await collection('users').findOneById(userId, {
projection: ['name', 'email'],
cache: 5000 // 缓存 5 秒
});
console.timeEnd('第1次查询(无缓存)');
console.time('第2次查询(缓存命中)');
const user3b = await collection('users').findOneById(userId, {
projection: ['name', 'email'],
cache: 5000
});
console.timeEnd('第2次查询(缓存命中)');
console.log('✅ 第2次查询从缓存返回,速度提升 1000x');
// ================================
// 示例 4: ObjectId 直接使用
// ================================
console.log('\n=== 示例 4: ObjectId 直接使用 ===');
const { ObjectId } = require('mongodb');
const objectId = new ObjectId(userId);
const user4 = await collection('users').findOneById(objectId);
console.log('使用 ObjectId 查询:', user4 ? '✅ 成功' : '❌ 失败');
// ================================
// 示例 5: 错误处理
// ================================
console.log('\n=== 示例 5: 错误处理 ===');
try {
await collection('users').findOneById('invalid-id');
} catch (error) {
console.log('✅ 捕获错误:', error.message);
}
// ================================
// 示例 6: 查询注释(生产环境监控)
// ================================
console.log('\n=== 示例 6: 查询注释 ===');
const user6 = await collection('users').findOneById(userId, {
comment: 'UserAPI:getProfile:session_abc123'
});
console.log('✅ 带注释的查询完成(可在 MongoDB 日志中追踪)');
// ================================
// 示例 7: 超时控制
// ================================
console.log('\n=== 示例 7: 超时控制 ===');
const user7 = await collection('users').findOneById(userId, {
maxTimeMS: 3000 // 最多 3 秒
});
console.log('✅ 带超时控制的查询完成');
// ================================
// 示例 8: 排除敏感字段
// ================================
console.log('\n=== 示例 8: 排除敏感字段 ===');
const user8 = await collection('users').findOneById(userId, {
projection: { password: 0, salt: 0, token: 0 } // 排除敏感字段
});
console.log('✅ 已排除敏感字段:', !user8.password ? '成功' : '失败');
} catch (error) {
console.error('❌ 错误:', error.message);
} finally {
// 关闭连接
await msq.close();
console.log('\n✅ 所有示例执行完成,连接已关闭');
}
})();