mm_mongodb 1.4.5 → 1.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +132 -214
- package/compatibility_table.md +85 -0
- package/index.js +1161 -34
- package/package.json +1 -1
- package/test_redis_compatibility.js +123 -0
package/README.md
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
# mm_mongodb
|
|
2
2
|
|
|
3
|
-
MongoDB数据库操作模块,提供了简洁易用的API
|
|
3
|
+
MongoDB数据库操作模块,提供了简洁易用的API、缓存功能以及**完整的Redis兼容接口**。
|
|
4
4
|
|
|
5
5
|
## 功能特点
|
|
6
6
|
|
|
7
|
-
- 支持MongoDB连接池管理
|
|
8
|
-
- 自动连接和重连机制
|
|
9
|
-
- 事务支持
|
|
10
|
-
- 索引管理
|
|
11
|
-
- 缓存功能
|
|
12
|
-
-
|
|
13
|
-
- 环境变量配置支持
|
|
14
|
-
- 完善的错误处理和重试机制
|
|
15
|
-
- 查询注入防护
|
|
16
|
-
- 事件系统:支持监听关键操作的事件
|
|
7
|
+
- ✅ 支持MongoDB连接池管理
|
|
8
|
+
- ✅ 自动连接和重连机制
|
|
9
|
+
- ✅ 事务支持
|
|
10
|
+
- ✅ 索引管理
|
|
11
|
+
- ✅ 缓存功能
|
|
12
|
+
- ✅ **完整的Redis兼容API**:支持Redis风格的键值操作、列表、集合、有序集合、哈希等数据类型
|
|
13
|
+
- ✅ 环境变量配置支持
|
|
14
|
+
- ✅ 完善的错误处理和重试机制
|
|
15
|
+
- ✅ 查询注入防护
|
|
16
|
+
- ✅ 事件系统:支持监听关键操作的事件
|
|
17
17
|
|
|
18
18
|
## 安装
|
|
19
19
|
|
|
@@ -140,254 +140,195 @@ await db.createIndex('users', { name: 1 }, { unique: true });
|
|
|
140
140
|
await db.createTTLIndex('logs', { createdAt: 1 }, 3600); // 3600秒后过期
|
|
141
141
|
```
|
|
142
142
|
|
|
143
|
-
|
|
143
|
+
## Redis兼容API使用指南
|
|
144
|
+
|
|
145
|
+
mm_mongodb提供了**完整的Redis兼容API**,使您可以使用熟悉的Redis命令与MongoDB进行交互。这对于从Redis迁移到MongoDB或在两者间切换使用非常有用。
|
|
144
146
|
|
|
145
|
-
|
|
147
|
+
### 快速开始
|
|
146
148
|
|
|
147
149
|
```javascript
|
|
148
150
|
const { mongodb_cache_admin } = require('mm_mongodb');
|
|
149
151
|
|
|
150
|
-
//
|
|
152
|
+
// 获取Redis兼容的缓存实例
|
|
151
153
|
const cache = mongodb_cache_admin('default');
|
|
152
154
|
|
|
153
|
-
//
|
|
155
|
+
// 连接数据库(重要:必须先连接)
|
|
154
156
|
await cache.open();
|
|
155
157
|
|
|
156
|
-
//
|
|
157
|
-
await cache.set('user_1', { id: 1, name: '王五' }, 10);
|
|
158
|
-
|
|
159
|
-
// 获取缓存
|
|
160
|
-
const user = await cache.get('user_1');
|
|
161
|
-
|
|
162
|
-
// 删除缓存
|
|
163
|
-
await cache.del('user_1');
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
#### 数值操作
|
|
167
|
-
|
|
168
|
-
```javascript
|
|
169
|
-
// 增加整数值
|
|
170
|
-
await cache.addInt('counter', 1); // 增加1
|
|
171
|
-
|
|
172
|
-
// 增加浮点数值
|
|
173
|
-
await cache.addFloat('score', 0.5); // 增加0.5
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
#### 数组操作
|
|
177
|
-
|
|
178
|
-
```javascript
|
|
179
|
-
// 设置数组
|
|
180
|
-
await cache.list_set('users', ['张三', '李四', '王五']);
|
|
181
|
-
|
|
182
|
-
// 添加到数组
|
|
183
|
-
await cache.list_push('users', '赵六');
|
|
184
|
-
|
|
185
|
-
// 获取数组
|
|
186
|
-
const users = await cache.list_get('users');
|
|
187
|
-
|
|
188
|
-
// 判断元素是否在数组中
|
|
189
|
-
const exists = await cache.list_has('users', '张三');
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
#### 过期时间管理
|
|
193
|
-
|
|
194
|
-
```javascript
|
|
195
|
-
// 设置过期时间(秒)
|
|
196
|
-
await cache.expire('temp_data', 300); // 5分钟后过期
|
|
197
|
-
|
|
198
|
-
// 获取剩余过期时间
|
|
199
|
-
const ttl = await cache.ttl('temp_data');
|
|
200
|
-
if (ttl === -1) console.log('永不过期');
|
|
201
|
-
else if (ttl === -2) console.log('键不存在');
|
|
202
|
-
else console.log(`剩余${ttl}秒过期`);
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
## Redis兼容API使用指南
|
|
206
|
-
|
|
207
|
-
mm_mongodb提供了完整的Redis兼容API,使您可以使用熟悉的Redis命令与MongoDB进行交互。这对于从Redis迁移到MongoDB或在两者间切换使用非常有用。
|
|
208
|
-
|
|
209
|
-
### 基本键值操作
|
|
210
|
-
|
|
211
|
-
```javascript
|
|
212
|
-
// 设置键值(支持过期时间)
|
|
158
|
+
// 基本操作示例
|
|
213
159
|
await cache.set('name', 'Redis Compatible');
|
|
214
|
-
|
|
215
|
-
// 仅当键不存在时设置
|
|
216
|
-
await cache.setnx('counter', 1);
|
|
217
|
-
|
|
218
|
-
// 设置带过期时间的键(SET EX)
|
|
219
|
-
await cache.setex('temp_key', 60, '临时值');
|
|
220
|
-
|
|
221
|
-
// 获取键值
|
|
222
160
|
const value = await cache.get('name');
|
|
223
|
-
|
|
224
|
-
// 批量设置
|
|
225
|
-
await cache.mset({ 'key1': 'value1', 'key2': 'value2' });
|
|
226
|
-
|
|
227
|
-
// 批量获取
|
|
228
|
-
const values = await cache.mget('key1', 'key2', 'key3');
|
|
229
|
-
|
|
230
|
-
// 删除键
|
|
231
|
-
await cache.del('name');
|
|
232
|
-
|
|
233
|
-
// 批量删除键
|
|
234
|
-
await cache.delKeys('key1', 'key2');
|
|
235
|
-
|
|
236
|
-
// 判断键是否存在
|
|
237
|
-
const exists = await cache.has('name');
|
|
238
|
-
|
|
239
|
-
// 获取匹配的键名
|
|
240
|
-
const keys = await cache.keys('user:*');
|
|
241
161
|
```
|
|
242
162
|
|
|
243
|
-
###
|
|
163
|
+
### 核心缓存方法
|
|
164
|
+
|
|
165
|
+
| 方法名 | 说明 | 示例 |
|
|
166
|
+
|-------|------|------|
|
|
167
|
+
| `set(key, value, seconds)` | 设置键值,支持过期时间 | `await cache.set('name', 'value', 60);` |
|
|
168
|
+
| `get(key)` | 获取键值 | `const value = await cache.get('name');` |
|
|
169
|
+
| `del(key)` | 删除键 | `await cache.del('name');` |
|
|
170
|
+
| `has(key)` / `exists(key)` | 判断键是否存在 | `const exists = await cache.has('name');` |
|
|
171
|
+
| `clear(prefix)` | 清空缓存(可选前缀过滤) | `await cache.clear('temp:*');` |
|
|
172
|
+
| `keys(pattern)` | 获取匹配的键名 | `const keys = await cache.keys('user:*');` |
|
|
173
|
+
| `ttl(key)` | 获取剩余过期时间 | `const ttl = await cache.ttl('name');` |
|
|
174
|
+
| `expire(key, seconds)` | 设置过期时间 | `await cache.expire('name', 60);` |
|
|
175
|
+
| `add(key, value)` | 添加缓存(如果存在则不覆盖) | `await cache.add('counter', 1);` |
|
|
176
|
+
| `addInt(key, value)` | 增加整数值 | `await cache.addInt('counter', 10);` |
|
|
177
|
+
| `addFloat(key, value)` | 增加浮点数值 | `await cache.addFloat('score', 0.5);` |
|
|
178
|
+
| `addStr(key, value)` | 追加字符串 | `await cache.addStr('message', ' World');` |
|
|
179
|
+
| `sort(prefix, count, type)` | 排序并获取 | `const sorted = await cache.sort('user:*', 10, 'asc');` |
|
|
180
|
+
|
|
181
|
+
### 哈希表操作
|
|
244
182
|
|
|
245
183
|
```javascript
|
|
246
|
-
//
|
|
247
|
-
await cache.
|
|
184
|
+
// 设置哈希字段
|
|
185
|
+
await cache.hset('user:1', 'name', '张三');
|
|
248
186
|
|
|
249
|
-
//
|
|
250
|
-
const
|
|
187
|
+
// 获取哈希字段
|
|
188
|
+
const name = await cache.hget('user:1', 'name');
|
|
251
189
|
|
|
252
|
-
//
|
|
253
|
-
const
|
|
190
|
+
// 获取哈希所有字段和值
|
|
191
|
+
const user = await cache.hgetall('user:1');
|
|
254
192
|
|
|
255
|
-
//
|
|
256
|
-
await cache.
|
|
257
|
-
```
|
|
193
|
+
// 批量设置哈希字段
|
|
194
|
+
await cache.hmset('user:2', { name: '李四', age: 30 });
|
|
258
195
|
|
|
259
|
-
|
|
196
|
+
// 批量获取哈希字段
|
|
197
|
+
const values = await cache.hmget('user:2', 'name', 'age');
|
|
260
198
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
await cache.incr('counter'); // 自增1
|
|
199
|
+
// 删除哈希字段
|
|
200
|
+
await cache.hdel('user:1', 'name');
|
|
264
201
|
|
|
265
|
-
//
|
|
266
|
-
await cache.
|
|
202
|
+
// 批量删除哈希字段
|
|
203
|
+
await cache.hdelMulti('user:2', 'name', 'age');
|
|
267
204
|
|
|
268
|
-
//
|
|
269
|
-
await cache.
|
|
205
|
+
// 判断哈希字段是否存在
|
|
206
|
+
const exists = await cache.hexists('user:1', 'name');
|
|
270
207
|
|
|
271
|
-
//
|
|
272
|
-
await cache.
|
|
208
|
+
// 获取哈希字段数量
|
|
209
|
+
const count = await cache.hlen('user:1');
|
|
210
|
+
|
|
211
|
+
// 获取哈希所有字段名
|
|
212
|
+
const keys = await cache.hkeys('user:1');
|
|
213
|
+
|
|
214
|
+
// 获取哈希所有字段值
|
|
215
|
+
const vals = await cache.hvals('user:1');
|
|
273
216
|
```
|
|
274
217
|
|
|
275
218
|
### 列表操作
|
|
276
219
|
|
|
277
220
|
```javascript
|
|
278
221
|
// 从右侧推入元素
|
|
279
|
-
await cache.rpush('
|
|
222
|
+
await cache.rpush('messages', '你好');
|
|
280
223
|
|
|
281
224
|
// 从左侧推入元素
|
|
282
|
-
await cache.lpush('
|
|
225
|
+
await cache.lpush('messages', 'Hello');
|
|
283
226
|
|
|
284
227
|
// 获取列表长度
|
|
285
|
-
const length = await cache.llen('
|
|
228
|
+
const length = await cache.llen('messages');
|
|
286
229
|
|
|
287
230
|
// 获取列表指定范围的元素
|
|
288
|
-
const range = await cache.lrange('
|
|
231
|
+
const range = await cache.lrange('messages', 0, 1); // 获取前两个元素
|
|
289
232
|
|
|
290
233
|
// 获取列表指定索引的元素
|
|
291
|
-
const
|
|
234
|
+
const message = await cache.lindex('messages', 0);
|
|
292
235
|
|
|
293
236
|
// 从左侧弹出元素
|
|
294
|
-
const
|
|
237
|
+
const firstMessage = await cache.lpop('messages');
|
|
295
238
|
|
|
296
239
|
// 从右侧弹出元素
|
|
297
|
-
const
|
|
240
|
+
const lastMessage = await cache.rpop('messages');
|
|
298
241
|
|
|
299
242
|
// 修改列表指定索引的元素
|
|
300
|
-
await cache.lset('
|
|
243
|
+
await cache.lset('messages', 0, 'Updated Message');
|
|
301
244
|
|
|
302
245
|
// 删除列表中的元素
|
|
303
|
-
await cache.lrem('
|
|
246
|
+
await cache.lrem('messages', 2, 'value'); // 删除前2个值为'value'的元素
|
|
304
247
|
|
|
305
248
|
// 裁剪列表
|
|
306
|
-
await cache.ltrim('
|
|
249
|
+
await cache.ltrim('messages', 0, 4); // 保留前5个元素
|
|
307
250
|
```
|
|
308
251
|
|
|
309
252
|
### 集合操作
|
|
310
253
|
|
|
311
254
|
```javascript
|
|
312
255
|
// 添加集合成员
|
|
313
|
-
await cache.sadd('
|
|
256
|
+
await cache.sadd('users:online', 'user1');
|
|
314
257
|
|
|
315
258
|
// 批量添加集合成员
|
|
316
|
-
await cache.saddMulti('
|
|
259
|
+
await cache.saddMulti('users:online', 'user2', 'user3');
|
|
317
260
|
|
|
318
261
|
// 获取集合所有成员
|
|
319
|
-
const members = await cache.smembers('
|
|
262
|
+
const members = await cache.smembers('users:online');
|
|
320
263
|
|
|
321
264
|
// 判断成员是否在集合中
|
|
322
|
-
const
|
|
265
|
+
const isOnline = await cache.sismember('users:online', 'user1');
|
|
323
266
|
|
|
324
267
|
// 获取集合成员数量
|
|
325
|
-
const count = await cache.scard('
|
|
268
|
+
const count = await cache.scard('users:online');
|
|
326
269
|
|
|
327
270
|
// 移除集合成员
|
|
328
|
-
await cache.srem('
|
|
271
|
+
await cache.srem('users:online', 'user1');
|
|
329
272
|
|
|
330
273
|
// 批量移除集合成员
|
|
331
|
-
await cache.sremMulti('
|
|
274
|
+
await cache.sremMulti('users:online', 'user2', 'user3');
|
|
332
275
|
|
|
333
276
|
// 计算两个集合的差集
|
|
334
|
-
const
|
|
277
|
+
const offlineUsers = await cache.sdiff('users:all', 'users:online');
|
|
335
278
|
|
|
336
279
|
// 计算两个集合的交集
|
|
337
|
-
const
|
|
280
|
+
const activeUsers = await cache.sinter('users:online', 'users:paid');
|
|
338
281
|
|
|
339
282
|
// 计算两个集合的并集
|
|
340
|
-
const
|
|
283
|
+
const allUsers = await cache.sunion('users:online', 'users:offline');
|
|
341
284
|
```
|
|
342
285
|
|
|
343
|
-
###
|
|
286
|
+
### 有序集合操作
|
|
344
287
|
|
|
345
288
|
```javascript
|
|
346
|
-
//
|
|
347
|
-
await cache.
|
|
289
|
+
// 添加有序集合成员
|
|
290
|
+
await cache.zadd('ranking', 'user1', 100);
|
|
348
291
|
|
|
349
|
-
//
|
|
350
|
-
const
|
|
351
|
-
|
|
352
|
-
// 获取哈希所有字段和值
|
|
353
|
-
const allFields = await cache.hgetall('hash');
|
|
292
|
+
// 获取有序集合成员数量
|
|
293
|
+
const count = await cache.zcard('ranking');
|
|
354
294
|
|
|
355
|
-
//
|
|
356
|
-
await cache.
|
|
357
|
-
|
|
358
|
-
// 批量获取哈希字段
|
|
359
|
-
const values = await cache.hmget('hash', 'field1', 'field2', 'field3');
|
|
295
|
+
// 获取有序集合成员分数
|
|
296
|
+
const score = await cache.zscore('ranking', 'user1');
|
|
360
297
|
|
|
361
|
-
//
|
|
362
|
-
await cache.
|
|
298
|
+
// 按分数范围获取有序集合成员
|
|
299
|
+
const topUsers = await cache.zrangebyscore('ranking', 0, 100);
|
|
363
300
|
|
|
364
|
-
//
|
|
365
|
-
await cache.
|
|
301
|
+
// 移除有序集合成员
|
|
302
|
+
await cache.zrem('ranking', 'user1');
|
|
303
|
+
```
|
|
366
304
|
|
|
367
|
-
|
|
368
|
-
const exists = await cache.hexists('hash', 'field1');
|
|
305
|
+
### 批量操作
|
|
369
306
|
|
|
370
|
-
|
|
371
|
-
|
|
307
|
+
```javascript
|
|
308
|
+
// 批量设置键值
|
|
309
|
+
await cache.mset({ 'key1': 'value1', 'key2': 'value2' });
|
|
372
310
|
|
|
373
|
-
//
|
|
374
|
-
const
|
|
311
|
+
// 批量获取键值
|
|
312
|
+
const values = await cache.mget('key1', 'key2', 'key3');
|
|
375
313
|
|
|
376
|
-
//
|
|
377
|
-
|
|
314
|
+
// 批量删除键
|
|
315
|
+
await cache.del('key1', 'key2', 'key3');
|
|
378
316
|
```
|
|
379
317
|
|
|
380
|
-
###
|
|
318
|
+
### 数值操作
|
|
381
319
|
|
|
382
320
|
```javascript
|
|
383
|
-
//
|
|
384
|
-
await cache.
|
|
321
|
+
// 原子递增
|
|
322
|
+
await cache.incr('counter'); // 自增1
|
|
323
|
+
|
|
324
|
+
// 原子递减
|
|
325
|
+
await cache.decr('counter'); // 自减1
|
|
385
326
|
|
|
386
|
-
//
|
|
387
|
-
|
|
327
|
+
// 增加指定整数
|
|
328
|
+
await cache.addInt('counter', 10); // 增加10
|
|
388
329
|
|
|
389
|
-
//
|
|
390
|
-
|
|
330
|
+
// 减少指定整数
|
|
331
|
+
await cache.addInt('counter', -5); // 减少5
|
|
391
332
|
```
|
|
392
333
|
|
|
393
334
|
## 事件系统
|
|
@@ -413,23 +354,24 @@ cache.on('set_error', (data) => {
|
|
|
413
354
|
|
|
414
355
|
以下增删改类方法支持事件触发:
|
|
415
356
|
|
|
416
|
-
- 原子操作:incr, decr,
|
|
357
|
+
- 原子操作:incr, decr, addInt, addFloat
|
|
417
358
|
- 字符串操作:append, addStr
|
|
418
|
-
- 列表操作:rpush, lpush, lset, lrem, ltrim
|
|
359
|
+
- 列表操作:rpush, lpush, lset, lrem, ltrim, lpop, rpop
|
|
419
360
|
- 集合操作:sadd, saddMulti, srem, sremMulti
|
|
420
361
|
- 哈希操作:hset, hmset, hdel, hdelMulti
|
|
421
|
-
-
|
|
362
|
+
- 有序集合操作:zadd, zrem
|
|
363
|
+
- 键操作:set, del, expire, mset
|
|
422
364
|
|
|
423
365
|
## 性能优化建议
|
|
424
366
|
|
|
425
367
|
1. **使用连接池**:优先使用`mongodb_admin`和`mongodb_cache_admin`创建的连接池实例
|
|
426
368
|
2. **合理设置索引**:为常用查询字段创建索引
|
|
427
369
|
3. **查询优化**:避免全表扫描,使用精确查询
|
|
428
|
-
4. **批量操作**:优先使用批量操作方法如`
|
|
370
|
+
4. **批量操作**:优先使用批量操作方法如`mset`、`mget`等
|
|
429
371
|
5. **过期时间**:为临时数据设置合理的过期时间,避免数据膨胀
|
|
430
|
-
6.
|
|
372
|
+
6. **Redis兼容模式**:使用字符串键名可以简化操作,与Redis保持一致的使用体验
|
|
431
373
|
|
|
432
|
-
## 迁移指南
|
|
374
|
+
## 从Redis迁移指南
|
|
433
375
|
|
|
434
376
|
从Redis迁移到使用mm_mongodb的Redis兼容API时,只需以下步骤:
|
|
435
377
|
|
|
@@ -442,6 +384,7 @@ cache.on('set_error', (data) => {
|
|
|
442
384
|
// Redis客户端示例
|
|
443
385
|
// const redis = require('redis');
|
|
444
386
|
// const client = redis.createClient();
|
|
387
|
+
// await client.connect();
|
|
445
388
|
|
|
446
389
|
// mm_mongodb替换示例
|
|
447
390
|
const { mongodb_cache_admin } = require('mm_mongodb');
|
|
@@ -502,46 +445,17 @@ new MongoDB(scope = 'default', dir = '', config = {})
|
|
|
502
445
|
- `getIndexes(tableName)`:获取索引信息
|
|
503
446
|
- `dropIndex(tableName, indexName)`:删除索引
|
|
504
447
|
|
|
505
|
-
###
|
|
448
|
+
### Redis兼容API
|
|
506
449
|
|
|
507
|
-
|
|
450
|
+
详细的Redis兼容API见上方「Redis兼容API使用指南」章节。支持以下类别的API:
|
|
508
451
|
|
|
509
|
-
-
|
|
510
|
-
-
|
|
511
|
-
-
|
|
512
|
-
-
|
|
513
|
-
-
|
|
514
|
-
-
|
|
515
|
-
|
|
516
|
-
#### 数值操作
|
|
517
|
-
|
|
518
|
-
- `addInt(key, value)`:增加整数值
|
|
519
|
-
- `addFloat(key, value)`:增加浮点数值
|
|
520
|
-
- `addStr(key, value)`:追加字符串
|
|
521
|
-
|
|
522
|
-
#### 数组操作
|
|
523
|
-
|
|
524
|
-
- `list_set(key, value, seconds)`:设置数组
|
|
525
|
-
- `list_push(key, value)`:添加到数组
|
|
526
|
-
- `list_get(key, start, end)`:获取数组片段
|
|
527
|
-
- `list_has(key, value)`:判断元素是否在数组中
|
|
528
|
-
- `list_clear(key, value)`:清空数组
|
|
529
|
-
|
|
530
|
-
#### 过期时间
|
|
531
|
-
|
|
532
|
-
- `expire(key, seconds)`:设置过期时间
|
|
533
|
-
- `ttl(key)`:获取剩余过期时间
|
|
534
|
-
|
|
535
|
-
#### 其他操作
|
|
536
|
-
|
|
537
|
-
- `keys(pattern)`:获取匹配的键名
|
|
538
|
-
- `getrange(key, start, end)`:获取字符串范围
|
|
539
|
-
- `setrange(key, index, value)`:设置字符串范围
|
|
540
|
-
- `sort(prefix, count, type)`:排序并获取
|
|
541
|
-
|
|
542
|
-
#### Redis兼容API
|
|
543
|
-
|
|
544
|
-
详细的Redis兼容API见上方「Redis兼容API使用指南」章节。
|
|
452
|
+
- **核心缓存方法**:set, get, del, has, exists, clear, keys, ttl, expire等
|
|
453
|
+
- **哈希表操作**:hset, hget, hgetall, hmset, hmget, hdel, hdelMulti, hexists, hkeys, hvals, hlen等
|
|
454
|
+
- **列表操作**:rpush, lpush, lrange, lindex, llen, lpop, rpop, lset, lrem, ltrim等
|
|
455
|
+
- **集合操作**:sadd, saddMulti, srem, sremMulti, smembers, scard, sismember, sdiff, sinter, sunion等
|
|
456
|
+
- **有序集合操作**:zadd, zrem, zrangebyscore, zscore, zcard等
|
|
457
|
+
- **批量操作**:mget, mset等
|
|
458
|
+
- **数值操作**:incr, decr, addInt, addFloat等
|
|
545
459
|
|
|
546
460
|
## 安全注意事项
|
|
547
461
|
|
|
@@ -551,6 +465,10 @@ new MongoDB(scope = 'default', dir = '', config = {})
|
|
|
551
465
|
4. 连接错误时会自动重试,提高稳定性
|
|
552
466
|
5. 避免使用未经处理的用户输入作为查询条件
|
|
553
467
|
|
|
468
|
+
## 兼容性表格
|
|
469
|
+
|
|
470
|
+
查看详细的方法兼容性对比表格,请参考项目中的 [compatibility_table.md](https://github.com/qwwang/mm_mongodb/blob/main/compatibility_table.md) 文件。
|
|
471
|
+
|
|
554
472
|
## 依赖
|
|
555
473
|
|
|
556
474
|
- [mm_logs](https://www.npmjs.com/package/mm_logs):日志模块
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
```markdown
|
|
2
|
+
# 三种缓存方式方法兼容性对比表格
|
|
3
|
+
|
|
4
|
+
## 核心缓存方法
|
|
5
|
+
|
|
6
|
+
| 方法名 | Redis | MongoDB | CacheBase | 状态说明 |
|
|
7
|
+
|-------|-------|---------|-----------|----------|
|
|
8
|
+
| set | ✅ | ✅ | ✅ | 完全兼容 |
|
|
9
|
+
| get | ✅ | ✅ | ✅ | 完全兼容 |
|
|
10
|
+
| del | ✅ | ✅ | ✅ | 完全兼容 |
|
|
11
|
+
| has | ✅ | ✅ | ✅ | 完全兼容 |
|
|
12
|
+
| clear | ✅ | ✅ | ✅ | 完全兼容 |
|
|
13
|
+
| keys | ✅ | ✅ | ✅ | 完全兼容 |
|
|
14
|
+
| ttl | ✅ | ✅ | ✅ | 完全兼容 |
|
|
15
|
+
| add | ✅ | ✅ | ✅ | 完全兼容 |
|
|
16
|
+
| addInt | ✅ | ✅ | ✅ | 完全兼容 |
|
|
17
|
+
| addFloat | ✅ | ✅ | ✅ | 完全兼容 |
|
|
18
|
+
| addStr | ✅ | ✅ | ✅ | 完全兼容 |
|
|
19
|
+
| sort | ✅ | ✅ | ✅ | 模拟实现 |
|
|
20
|
+
|
|
21
|
+
## Redis兼容方法 - 哈希表操作
|
|
22
|
+
|
|
23
|
+
| 方法名 | Redis | MongoDB | CacheBase | 状态说明 |
|
|
24
|
+
|-------|-------|---------|-----------|----------|
|
|
25
|
+
| hset | ✅ | ✅ | ✅ | 模拟实现,通过set对象方式 |
|
|
26
|
+
| hget | ✅ | ✅ | ✅ | 模拟实现,通过get对象方式 |
|
|
27
|
+
| hgetall | ✅ | ✅ | ✅ | 模拟实现,返回完整对象 |
|
|
28
|
+
| hdel | ✅ | ✅ | ✅ | 模拟实现,删除对象属性 |
|
|
29
|
+
| hexists | ✅ | ✅ | ✅ | 模拟实现,通过hget判断 |
|
|
30
|
+
| hkeys | ✅ | ✅ | ✅ | 模拟实现,返回对象所有键 |
|
|
31
|
+
| hvals | ✅ | ✅ | ✅ | 模拟实现,返回对象所有值 |
|
|
32
|
+
| hmset | ✅ | ✅ | ✅ | 模拟实现,批量设置哈希表字段 |
|
|
33
|
+
| hmget | ✅ | ✅ | ✅ | 模拟实现,批量获取哈希表字段 |
|
|
34
|
+
| hlen | ✅ | ✅ | ✅ | 模拟实现,返回哈希表字段数量 |
|
|
35
|
+
|
|
36
|
+
## Redis兼容方法 - 集合操作
|
|
37
|
+
|
|
38
|
+
| 方法名 | Redis | MongoDB | CacheBase | 状态说明 |
|
|
39
|
+
|-------|-------|---------|-----------|----------|
|
|
40
|
+
| sadd | ✅ | ✅ | ✅ | 模拟实现,通过数组存储集合 |
|
|
41
|
+
| srem | ✅ | ✅ | ✅ | 模拟实现,从数组中移除元素 |
|
|
42
|
+
| sismember | ✅ | ✅ | ✅ | 模拟实现,检查元素是否在数组中 |
|
|
43
|
+
| smembers | ✅ | ✅ | ✅ | 模拟实现,返回数组所有元素 |
|
|
44
|
+
| scard | ✅ | ✅ | ✅ | 模拟实现,返回数组长度 |
|
|
45
|
+
| saddMulti | ✅ | ✅ | ✅ | 模拟实现,批量添加集合元素 |
|
|
46
|
+
| sremMulti | ✅ | ✅ | ✅ | 模拟实现,批量移除集合元素 |
|
|
47
|
+
| sdiff | ✅ | ✅ | ✅ | 模拟实现,集合差集操作 |
|
|
48
|
+
| sinter | ✅ | ✅ | ✅ | 模拟实现,集合并集操作 |
|
|
49
|
+
| sunion | ✅ | ✅ | ✅ | 模拟实现,集合交集操作 |
|
|
50
|
+
|
|
51
|
+
## Redis兼容方法 - 有序集合操作
|
|
52
|
+
|
|
53
|
+
| 方法名 | Redis | MongoDB | CacheBase | 状态说明 |
|
|
54
|
+
|-------|-------|---------|-----------|----------|
|
|
55
|
+
| zadd | ✅ | ✅ | ✅ | 模拟实现,通过对象存储分数 |
|
|
56
|
+
| zrem | ✅ | ✅ | ✅ | 模拟实现,移除有序集合元素 |
|
|
57
|
+
| zrangebyscore | ✅ | ✅ | ✅ | 模拟实现,按分数范围获取元素 |
|
|
58
|
+
| zscore | ✅ | ✅ | ✅ | 模拟实现,获取元素分数 |
|
|
59
|
+
| zcard | ✅ | ✅ | ✅ | 模拟实现,返回有序集合元素数量 |
|
|
60
|
+
|
|
61
|
+
## Redis兼容方法 - 列表操作
|
|
62
|
+
|
|
63
|
+
| 方法名 | Redis | MongoDB | CacheBase | 状态说明 |
|
|
64
|
+
|-------|-------|---------|-----------|----------|
|
|
65
|
+
| rpush | ✅ | ✅ | ✅ | 模拟实现,从列表右侧添加元素 |
|
|
66
|
+
| lpush | ✅ | ✅ | ✅ | 模拟实现,从列表左侧添加元素 |
|
|
67
|
+
| lrange | ✅ | ✅ | ✅ | 模拟实现,获取列表指定范围元素 |
|
|
68
|
+
| lindex | ✅ | ✅ | ✅ | 模拟实现,获取列表指定索引元素 |
|
|
69
|
+
| llen | ✅ | ✅ | ✅ | 模拟实现,获取列表长度 |
|
|
70
|
+
| lpop | ✅ | ✅ | ✅ | 模拟实现,从列表左侧弹出元素 |
|
|
71
|
+
| rpop | ✅ | ✅ | ✅ | 模拟实现,从列表右侧弹出元素 |
|
|
72
|
+
|
|
73
|
+
## Redis兼容方法 - 其他常用命令
|
|
74
|
+
|
|
75
|
+
| 方法名 | Redis | MongoDB | CacheBase | 状态说明 |
|
|
76
|
+
|-------|-------|---------|-----------|----------|
|
|
77
|
+
| expire | ✅ | ✅ | ✅ | 兼容,内部映射到ttl |
|
|
78
|
+
| incr | ✅ | ✅ | ✅ | 完全兼容,通过addInt模拟 |
|
|
79
|
+
| decr | ✅ | ✅ | ✅ | 完全兼容,通过addInt模拟 |
|
|
80
|
+
| exists | ✅ | ✅ | ✅ | 兼容,内部映射到has |
|
|
81
|
+
| mget | ✅ | ✅ | ✅ | 完全兼容,返回键值对对象 |
|
|
82
|
+
| mset | ✅ | ✅ | ✅ | 完全兼容,批量设置键值 |
|
|
83
|
+
|
|
84
|
+
注:✅ 完全兼容/功能正常 | ⚠️ 部分兼容/有异常 | ❌ 不兼容/未实现
|
|
85
|
+
```
|