Skip to content

Commit c9caaba

Browse files
committed
add aggregate
1 parent fde34e0 commit c9caaba

12 files changed

Lines changed: 1839 additions & 142 deletions

File tree

‎README.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- [状态](#status)
77
- [安装](#install)
88
- [快速开始](#quick-start)
9+
- [聚合查询(aggregate)](#aggregate)
910
- [深度分页(聚合版,Mongo)](#deep-pagination-agg)
1011
- [统一 findPage:游标 + 跳页 + offset + totals](#findpage-unified)
1112
- [返回耗时(meta)](#返回耗时meta)
@@ -76,6 +77,48 @@ const MonSQLize = require('monsqlize');
7677
})();
7778
```
7879

80+
<a id='aggregate'></a>
81+
## 聚合查询(aggregate)
82+
> 提示:可在构造时通过 defaults 配置 aggregateMaxTimeMS(默认 10s)。如需允许落盘,请在本次调用显式传入 allowDiskUse: true。
83+
`aggregate(options)` 支持以数组形式传入聚合管道,并在管道中使用 `$lookup` 等操作符进行联表查询。
84+
85+
- 适用:复杂查询与报表,或需要联表的场景。
86+
- 缓存:仅当 `options.cache>0` 时启用;缓存键包含 `op=aggregate | pipelineHash`。
87+
- 透传(Mongo 专属):支持在 options 里传 `hint`/`collation`,分别透传至 `aggregate` 的 `hint`/`collation`。
88+
> 兼容性提示:`aggregate hint` 需要较新的 MongoDB/Node 驱动版本(建议 MongoDB ≥ 4.2,Node 驱动 ≥ 5.x)。
89+
90+
示例:
91+
```js
92+
const MonSQLize = require('monsqlize');
93+
const { collection } = await new MonSQLize({
94+
type: 'mongodb',
95+
databaseName: 'example',
96+
config: { uri: 'mongodb://localhost:27017' },
97+
}).connect();
98+
99+
const pipeline = [
100+
{
101+
$lookup: {
102+
from: 'user',
103+
let: { userId: { $toObjectId: '$userId' } },
104+
pipeline: [ { $match: { $expr: { $eq: ['$_id','$$userId'] } } } ],
105+
as: 'userInfo'
106+
}
107+
},
108+
{ $match: { status: 'paid' } },
109+
{ $sort: { createdAt: -1, _id: 1 } },
110+
{ $limit: 10 }
111+
];
112+
113+
// 聚合查询
114+
const result = await collection('orders').aggregate(pipeline, {
115+
cache: 3000,
116+
});
117+
console.log(result);
118+
```
119+
> 说明:当前 Mongo 适配器的 `aggregate` 基于原生驱动实现,未来跨数据库将复用该方法名,以各自最优实现(如 SQL Join)。
120+
121+
79122
<a id='deep-pagination-agg'></a>
80123
## 深度分页(聚合版,Mongo)
81124
> 提示:可在构造时通过 defaults 配置 findPageMaxLimit(默认 500)。如需聚合允许落盘,请在本次调用显式传入 allowDiskUse: true;未来版本可能提供 cursorSecret 以增强游标防篡改。

‎STATUS.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,18 @@
8686
- 支持 limit/skip 普通分页;未传 limit 使用全局 findLimit(默认 10);limit=0 表示不限制。
8787
- ✅ 深分页(统一 findPage)
8888
- 游标 after/before + 跳页 page(书签 bm: + 少量 hops)+ offset 兜底(小范围 `$skip+$limit`)+ totals(none/async/approx/sync)。
89-
- 稳定排序(默认补 `_id`);页内 `$lookup` 支持;书签/总数键采用去敏“查询形状哈希”,复用实例 cache。
89+
- 稳定排序(默认补 `_id`);页内 `$lookup` 支持;书签/总数键采用去敏"查询形状哈希",复用实例 cache。
9090
- totals 优化:异步 totals 返回短 token(keyHash),并启用 5s inflight 去重;失败语义统一(total:null)。
9191
- ☑️ 链表/聚合驱动分页
9292
- 方案A(先分页后联表)已支持;按联表字段排序/筛选的方案B(先联表后分页)计划中。
9393
- ✅ count
9494
- 统计匹配文档数;totals.sync/async 会透传 hint/collation/maxTimeMS。
95+
- 性能优化:空查询自动使用 estimatedDocumentCount(基于元数据,速度快);有查询条件使用 countDocuments(精确统计)。
9596
- ❌ stream(find 流式返回)
96-
- 计划中。
97-
- ❌ 聚合(aggregate/或 find 支持聚合)
98-
- 后续可能透传或翻译。
97+
- 支持流式查询,适合处理大数据集;默认 batchSize=1000;支持 maxTimeMS/hint/collation/noCursorTimeout。
98+
- 自动记录慢查询日志;触发 slow-query 和 query 事件;不支持缓存(流式特性)。
99+
- ✅ 聚合(aggregate)
100+
- 支持 MongoDB 聚合管道透传;支持 maxTimeMS/allowDiskUse/hint/collation/comment;默认禁用缓存(cache=0);可选返回 meta 耗时信息。
99101
- ❌ distinct
100102
- 仅 Mongo 适配器语义;尚未纳入抽象。
101103
- ❌ explain
@@ -105,8 +107,6 @@
105107
- ❌ 高级查询/游标选项统一抽象
106108
- batchSize/hint/collation/noCursorTimeout/tailable/max/min/returnKey/allowPartialResults/
107109
readPreference/readConcern。
108-
- ❌ showRecordId
109-
- Mongo 专属选项。
110110
- ❌ comment / let
111111
- let 多见于聚合;待评估透传策略。
112112
- ❌ readPreferenceTags

0 commit comments

Comments
 (0)