@@ -46,27 +46,33 @@ const MonSQLize = require('monsqlize');
4646 }).connect ();
4747
4848 // 查询单个文档
49- const one = await collection (' test' ).findOne ({
50- query: { status: ' active' },
51- cache: 5000 , // 缓存 5 秒
52- maxTimeMS: 1500 // 覆盖全局超时
53- });
49+ const one = await collection (' test' ).findOne (
50+ { status: ' active' },
51+ {
52+ cache: 5000 , // 缓存 5 秒
53+ maxTimeMS: 1500 // 覆盖全局超时
54+ }
55+ );
5456 console .log (' findOne ->' , one);
5557
5658 // 查询多个文档
57- const list = await collection (' test' ).find ({
58- query: { category: ' electronics' },
59- limit: 10 , // 限制 10 条
60- cache: 3000 // 缓存 3 秒
61- });
59+ const list = await collection (' test' ).find (
60+ { category: ' electronics' },
61+ {
62+ limit: 10 , // 限制 10 条
63+ cache: 3000 // 缓存 3 秒
64+ }
65+ );
6266 console .log (' find ->' , list .length );
6367
6468 // 跨库访问
65- const event = await db (' analytics' ).collection (' events' ).findOne ({
66- query: { type: ' click' },
67- cache: 3000 ,
68- maxTimeMS: 1500
69- });
69+ const event = await db (' analytics' ).collection (' events' ).findOne (
70+ { type: ' click' },
71+ {
72+ cache: 3000 ,
73+ maxTimeMS: 1500
74+ }
75+ );
7076 console .log (' 跨库查询 ->' , event );
7177})();
7278```
@@ -138,22 +144,26 @@ const MonSQLize = require('monsqlize');
138144
139145``` js
140146// 数组模式(默认)
141- const products = await collection (' products' ).find ({
142- query: { category: ' electronics' , inStock: true },
143- projection: { name: 1 , price: 1 },
144- sort: { price: - 1 },
145- limit: 20 ,
146- cache: 5000 ,
147- comment: ' ProductAPI:listProducts:user_123' // 生产环境日志跟踪(可选)
148- });
147+ const products = await collection (' products' ).find (
148+ { category: ' electronics' , inStock: true },
149+ {
150+ projection: { name: 1 , price: 1 },
151+ sort: { price: - 1 },
152+ limit: 20 ,
153+ cache: 5000 ,
154+ comment: ' ProductAPI:listProducts:user_123' // 生产环境日志跟踪(可选)
155+ }
156+ );
149157
150158// 流式传输(大数据量)
151- const stream = await collection (' products' ).find ({
152- query: { category: ' electronics' },
153- stream: true , // 返回流
154- cache: 0 , // 禁用缓存
155- comment: ' ExportService:streamProducts' // 标识查询来源
156- });
159+ const stream = await collection (' products' ).find (
160+ { category: ' electronics' },
161+ {
162+ stream: true , // 返回流
163+ cache: 0 , // 禁用缓存
164+ comment: ' ExportService:streamProducts' // 标识查询来源
165+ }
166+ );
157167
158168stream .on (' data' , (doc ) => {
159169 console .log (' 处理文档:' , doc);
@@ -172,34 +182,40 @@ stream.on('end', () => {
172182
173183``` js
174184// 游标分页(推荐)
175- const page1 = await collection (' products' ).findPage ({
176- query: { category: ' electronics' },
177- limit: 20 ,
178- sort: { createdAt: - 1 },
179- bookmarks: {
180- step: 10 , // 每 10 页缓存一个书签
181- maxHops: 20 , // 最多跳跃 20 次
182- ttlMs: 3600000 // 书签缓存 1 小时
185+ const page1 = await collection (' products' ).findPage (
186+ { category: ' electronics' },
187+ {
188+ limit: 20 ,
189+ sort: { createdAt: - 1 },
190+ bookmarks: {
191+ step: 10 , // 每 10 页缓存一个书签
192+ maxHops: 20 , // 最多跳跃 20 次
193+ ttlMs: 3600000 // 书签缓存 1 小时
194+ }
183195 }
184- } );
196+ );
185197
186198console .log (' 第 1 页:' , page1 .data );
187199console .log (' 下一页游标:' , page1 .cursor );
188200
189201// 使用游标获取下一页
190- const page2 = await collection (' products' ).findPage ({
191- query: { category: ' electronics' },
192- limit: 20 ,
193- cursor: page1 .cursor // 传入上一页的游标
194- });
202+ const page2 = await collection (' products' ).findPage (
203+ { category: ' electronics' },
204+ {
205+ limit: 20 ,
206+ cursor: page1 .cursor // 传入上一页的游标
207+ }
208+ );
195209
196210// 跳页模式(跳到第 100 页)
197- const page100 = await collection (' products' ).findPage ({
198- query: { category: ' electronics' },
199- limit: 20 ,
200- page: 100 , // 跳到第 100 页
201- bookmarks: { step: 10 , maxHops: 20 , ttlMs: 3600000 }
202- });
211+ const page100 = await collection (' products' ).findPage (
212+ { category: ' electronics' },
213+ {
214+ limit: 20 ,
215+ page: 100 , // 跳到第 100 页
216+ bookmarks: { step: 10 , maxHops: 20 , ttlMs: 3600000 }
217+ }
218+ );
203219```
204220
205221** 详细文档** : [ docs/findPage.md] ( ./docs/findPage.md )
@@ -248,11 +264,13 @@ const msq = new MonSQLize({
248264});
249265
250266// 查询级缓存
251- const products = await collection (' products' ).find ({
252- query: { category: ' electronics' },
253- cache: 5000 , // 缓存 5 秒
254- maxTimeMS: 3000
255- });
267+ const products = await collection (' products' ).find (
268+ { category: ' electronics' },
269+ {
270+ cache: 5000 , // 缓存 5 秒
271+ maxTimeMS: 3000
272+ }
273+ );
256274
257275// 获取缓存统计
258276const stats = msq .getCacheStats ();
@@ -284,10 +302,12 @@ const msq = new MonSQLize({
284302const { db , collection } = await msq .connect ();
285303
286304// 跨库访问
287- const analyticsEvent = await db (' analytics' ).collection (' events' ).findOne ({
288- query: { type: ' click' },
289- cache: 3000
290- });
305+ const analyticsEvent = await db (' analytics' ).collection (' events' ).findOne (
306+ { type: ' click' },
307+ {
308+ cache: 3000
309+ }
310+ );
291311
292312// 关闭连接
293313await msq .close ();
@@ -314,9 +334,9 @@ await msq.connect();
314334const { collection } = msq;
315335
316336// 所有查询自动从从节点读取(降低主节点负载)
317- const products = await collection (' products' ).find ({
318- query : { category: ' electronics' }
319- } );
337+ const products = await collection (' products' ).find (
338+ { category: ' electronics' }
339+ );
320340
321341console .log (` ✅ 从从节点读取 ${ products .length } 条数据` );
322342```
@@ -436,18 +456,22 @@ const msq = new MonSQLize({
436456const { collection } = await msq .connect ();
437457
438458// 第 1 次查询:缓存 miss → 查询 MongoDB → 存入本地 + Redis
439- const products1 = await collection (' products' ).find ({
440- query: { category: ' electronics' },
441- cache: 10000 , // 缓存 10 秒
442- maxTimeMS: 3000
443- });
459+ const products1 = await collection (' products' ).find (
460+ { category: ' electronics' },
461+ {
462+ cache: 10000 , // 缓存 10 秒
463+ maxTimeMS: 3000
464+ }
465+ );
444466
445467// 第 2 次查询:本地缓存命中(0.001ms)
446- const products2 = await collection (' products' ).find ({
447- query: { category: ' electronics' },
448- cache: 10000 ,
449- maxTimeMS: 3000
450- });
468+ const products2 = await collection (' products' ).find (
469+ { category: ' electronics' },
470+ {
471+ cache: 10000 ,
472+ maxTimeMS: 3000
473+ }
474+ );
451475
452476// 如果本地缓存过期,但 Redis 还有 → 从 Redis 读取(1-2ms)并回填本地
453477```
0 commit comments