-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathaggregation_cursor.ts
205 lines (181 loc) · 6.39 KB
/
aggregation_cursor.ts
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { AggregateOperation, AggregateOptions } from '../operations/aggregate';
import { AbstractCursor, assertUninitialized } from './abstract_cursor';
import { executeOperation, ExecutionResult } from '../operations/execute_operation';
import { mergeOptions } from '../utils';
import type { Document } from '../bson';
import type { Sort } from '../sort';
import type { Topology } from '../sdam/topology';
import type { Callback, MongoDBNamespace } from '../utils';
import type { ClientSession } from '../sessions';
import type { OperationParent } from '../operations/command';
import type { AbstractCursorOptions } from './abstract_cursor';
import type { ExplainVerbosityLike } from '../explain';
import type { Projection } from '../mongo_types';
/** @public */
export interface AggregationCursorOptions extends AbstractCursorOptions, AggregateOptions {}
/** @internal */
const kParent = Symbol('parent');
/** @internal */
const kPipeline = Symbol('pipeline');
/** @internal */
const kOptions = Symbol('options');
/**
* The **AggregationCursor** class is an internal class that embodies an aggregation cursor on MongoDB
* allowing for iteration over the results returned from the underlying query. It supports
* one by one document iteration, conversion to an array or can be iterated as a Node 4.X
* or higher stream
* @public
*/
export class AggregationCursor<TSchema = Document> extends AbstractCursor<TSchema> {
/** @internal */
[kParent]: OperationParent; // TODO: NODE-2883
/** @internal */
[kPipeline]: Document[];
/** @internal */
[kOptions]: AggregateOptions;
/** @internal */
constructor(
parent: OperationParent,
topology: Topology,
namespace: MongoDBNamespace,
pipeline: Document[] = [],
options: AggregateOptions = {}
) {
super(topology, namespace, options);
this[kParent] = parent;
this[kPipeline] = pipeline;
this[kOptions] = options;
}
get pipeline(): Document[] {
return this[kPipeline];
}
clone(): AggregationCursor<TSchema> {
const clonedOptions = mergeOptions({}, this[kOptions]);
delete clonedOptions.session;
return new AggregationCursor(this[kParent], this.topology, this.namespace, this[kPipeline], {
...clonedOptions
});
}
map<T>(transform: (doc: TSchema) => T): AggregationCursor<T> {
return super.map(transform) as AggregationCursor<T>;
}
/** @internal */
_initialize(session: ClientSession | undefined, callback: Callback<ExecutionResult>): void {
const aggregateOperation = new AggregateOperation(this[kParent], this[kPipeline], {
...this[kOptions],
...this.cursorOptions,
session
});
executeOperation(this.topology, aggregateOperation, (err, response) => {
if (err || response == null) return callback(err);
// TODO: NODE-2882
callback(undefined, { server: aggregateOperation.server, session, response });
});
}
/** Execute the explain for the cursor */
explain(): Promise<Document>;
explain(callback: Callback): void;
explain(verbosity: ExplainVerbosityLike): Promise<Document>;
explain(
verbosity?: ExplainVerbosityLike | Callback,
callback?: Callback<Document>
): Promise<Document> | void {
if (typeof verbosity === 'function') (callback = verbosity), (verbosity = true);
if (verbosity === undefined) verbosity = true;
return executeOperation(
this.topology,
new AggregateOperation(this[kParent], this[kPipeline], {
...this[kOptions], // NOTE: order matters here, we may need to refine this
...this.cursorOptions,
explain: verbosity
}),
callback
);
}
/** Add a group stage to the aggregation pipeline */
group<T = TSchema>($group: Document): AggregationCursor<T>;
group($group: Document): this {
assertUninitialized(this);
this[kPipeline].push({ $group });
return this;
}
/** Add a limit stage to the aggregation pipeline */
limit($limit: number): this {
assertUninitialized(this);
this[kPipeline].push({ $limit });
return this;
}
/** Add a match stage to the aggregation pipeline */
match($match: Document): this {
assertUninitialized(this);
this[kPipeline].push({ $match });
return this;
}
/** Add a out stage to the aggregation pipeline */
out($out: number): this {
assertUninitialized(this);
this[kPipeline].push({ $out });
return this;
}
/**
* Add a project stage to the aggregation pipeline
*
* @remarks
* In order to strictly type this function you must provide an interface
* that represents the effect of your projection on the result documents.
*
* **NOTE:** adding a projection changes the return type of the iteration of this cursor,
* it **does not** return a new instance of a cursor. This means when calling project,
* you should always assign the result to a new variable. Take note of the following example:
*
* @example
* ```typescript
* const cursor: AggregationCursor<{ a: number; b: string }> = coll.aggregate([]);
* const projectCursor = cursor.project<{ a: number }>({ a: true });
* const aPropOnlyArray: {a: number}[] = await projectCursor.toArray();
* ```
*/
project<T = TSchema>($project: Projection<T>): AggregationCursor<T>;
project($project: Document): this {
assertUninitialized(this);
this[kPipeline].push({ $project });
return this;
}
/** Add a lookup stage to the aggregation pipeline */
lookup($lookup: Document): this {
assertUninitialized(this);
this[kPipeline].push({ $lookup });
return this;
}
/** Add a redact stage to the aggregation pipeline */
redact($redact: Document): this {
assertUninitialized(this);
this[kPipeline].push({ $redact });
return this;
}
/** Add a skip stage to the aggregation pipeline */
skip($skip: number): this {
assertUninitialized(this);
this[kPipeline].push({ $skip });
return this;
}
/** Add a sort stage to the aggregation pipeline */
sort($sort: Sort): this {
assertUninitialized(this);
this[kPipeline].push({ $sort });
return this;
}
/** Add a unwind stage to the aggregation pipeline */
unwind($unwind: Document | string): this {
assertUninitialized(this);
this[kPipeline].push({ $unwind });
return this;
}
// deprecated methods
/** @deprecated Add a geoNear stage to the aggregation pipeline */
geoNear($geoNear: Document): this {
assertUninitialized(this);
this[kPipeline].push({ $geoNear });
return this;
}
}