@@ -86,33 +86,45 @@ export class AggregationCursor<TSchema = any> extends AbstractCursor<TSchema> {
86
86
) ;
87
87
}
88
88
89
+ /** Add a stage to the aggregation pipeline
90
+ * @example
91
+ * ```
92
+ * const documents = await users.aggregate().addStage({ $match: { name: /Mike/ } }).toArray();
93
+ * ```
94
+ * @example
95
+ * ```
96
+ * const documents = await users.aggregate()
97
+ * .addStage<{ name: string }>({ $project: { name: true } })
98
+ * .toArray(); // type of documents is { name: string }[]
99
+ * ```
100
+ */
101
+ addStage ( stage : Document ) : this;
102
+ addStage < T = Document > ( stage : Document ) : AggregationCursor < T > ;
103
+ addStage < T = Document > ( stage : Document ) : AggregationCursor < T > {
104
+ assertUninitialized ( this ) ;
105
+ this [ kPipeline ] . push ( stage ) ;
106
+ return this as unknown as AggregationCursor < T > ;
107
+ }
108
+
89
109
/** Add a group stage to the aggregation pipeline */
90
110
group < T = TSchema > ( $group : Document ) : AggregationCursor < T > ;
91
111
group ( $group : Document ) : this {
92
- assertUninitialized ( this ) ;
93
- this [ kPipeline ] . push ( { $group } ) ;
94
- return this ;
112
+ return this . addStage ( { $group } ) ;
95
113
}
96
114
97
115
/** Add a limit stage to the aggregation pipeline */
98
116
limit ( $limit : number ) : this {
99
- assertUninitialized ( this ) ;
100
- this [ kPipeline ] . push ( { $limit } ) ;
101
- return this ;
117
+ return this . addStage ( { $limit } ) ;
102
118
}
103
119
104
120
/** Add a match stage to the aggregation pipeline */
105
121
match ( $match : Document ) : this {
106
- assertUninitialized ( this ) ;
107
- this [ kPipeline ] . push ( { $match } ) ;
108
- return this ;
122
+ return this . addStage ( { $match } ) ;
109
123
}
110
124
111
125
/** Add an out stage to the aggregation pipeline */
112
126
out ( $out : { db : string ; coll : string } | string ) : this {
113
- assertUninitialized ( this ) ;
114
- this [ kPipeline ] . push ( { $out } ) ;
115
- return this ;
127
+ return this . addStage ( { $out } ) ;
116
128
}
117
129
118
130
/**
@@ -157,50 +169,36 @@ export class AggregationCursor<TSchema = any> extends AbstractCursor<TSchema> {
157
169
* ```
158
170
*/
159
171
project < T extends Document = Document > ( $project : Document ) : AggregationCursor < T > {
160
- assertUninitialized ( this ) ;
161
- this [ kPipeline ] . push ( { $project } ) ;
162
- return this as unknown as AggregationCursor < T > ;
172
+ return this . addStage < T > ( { $project } ) ;
163
173
}
164
174
165
175
/** Add a lookup stage to the aggregation pipeline */
166
176
lookup ( $lookup : Document ) : this {
167
- assertUninitialized ( this ) ;
168
- this [ kPipeline ] . push ( { $lookup } ) ;
169
- return this ;
177
+ return this . addStage ( { $lookup } ) ;
170
178
}
171
179
172
180
/** Add a redact stage to the aggregation pipeline */
173
181
redact ( $redact : Document ) : this {
174
- assertUninitialized ( this ) ;
175
- this [ kPipeline ] . push ( { $redact } ) ;
176
- return this ;
182
+ return this . addStage ( { $redact } ) ;
177
183
}
178
184
179
185
/** Add a skip stage to the aggregation pipeline */
180
186
skip ( $skip : number ) : this {
181
- assertUninitialized ( this ) ;
182
- this [ kPipeline ] . push ( { $skip } ) ;
183
- return this ;
187
+ return this . addStage ( { $skip } ) ;
184
188
}
185
189
186
190
/** Add a sort stage to the aggregation pipeline */
187
191
sort ( $sort : Sort ) : this {
188
- assertUninitialized ( this ) ;
189
- this [ kPipeline ] . push ( { $sort } ) ;
190
- return this ;
192
+ return this . addStage ( { $sort } ) ;
191
193
}
192
194
193
195
/** Add a unwind stage to the aggregation pipeline */
194
196
unwind ( $unwind : Document | string ) : this {
195
- assertUninitialized ( this ) ;
196
- this [ kPipeline ] . push ( { $unwind } ) ;
197
- return this ;
197
+ return this . addStage ( { $unwind } ) ;
198
198
}
199
199
200
200
/** Add a geoNear stage to the aggregation pipeline */
201
201
geoNear ( $geoNear : Document ) : this {
202
- assertUninitialized ( this ) ;
203
- this [ kPipeline ] . push ( { $geoNear } ) ;
204
- return this ;
202
+ return this . addStage ( { $geoNear } ) ;
205
203
}
206
204
}
0 commit comments