UNPKG

8.73 kBTypeScriptView Raw
1declare module 'mongoose' {
2 import mongodb = require('mongodb');
3
4 /** Extract generic type from Aggregate class */
5 type AggregateExtract<P> = P extends Aggregate<infer T> ? T : never;
6
7 interface AggregateOptions extends
8 SessionOption {
9 /**
10 * If true, the MongoDB server will use the hard drive to store data during this aggregation.
11 */
12 allowDiskUse?: boolean;
13 /**
14 * Applicable only if you specify the $out or $merge aggregation stages.
15 *
16 * Enables db.collection.aggregate() to bypass document validation during the operation. This lets you insert documents that do not meet the validation requirements.
17 */
18 bypassDocumentValidation?: boolean;
19 /**
20 * The BSON-serializer will check if keys are valid
21 */
22 collation?: mongodb.CollationOptions;
23 /**
24 * Users can specify an arbitrary string to help trace the operation through the database profiler, currentOp, and logs.
25 */
26 comment?: string;
27 /**
28 * Specifies the initial batch size for the cursor. The value of the cursor field is a document with the field batchSize.
29 */
30 cursor?: { batchSize?: number; };
31
32 /**
33 * Specifies to return the information on the processing of the pipeline. See Return Information on Aggregation Pipeline Operation for an example.
34 *
35 * Not available in multi-document transactions.
36 */
37 explain?: mongodb.ExplainVerbosityLike;
38 /**
39 * The index to use for the aggregation. The index is on the initial collection/view against which the aggregation is run.
40 */
41 hint?: string | AnyObject;
42 /**
43 * Specifies a document with a list of variables. This allows you to improve command readability by separating the variables from the query text.
44 */
45 let?: AnyObject;
46 /**
47 * Specifies a time limit in milliseconds for processing operations on a cursor. If you do not specify a value for maxTimeMS, operations will not time out. A value of 0 explicitly specifies the default unbounded behavior.
48 *
49 * @see https://docs.mongodb.com/manual/reference/operator/meta/maxTimeMS/
50 */
51 maxTimeMS?: number;
52 /**
53 * Return BSON filled buffers from operations.
54 */
55 raw?: boolean;
56 /**
57 * Specifies the read concern.
58 */
59 readConcern?: mongodb.ReadConcernLike;
60 /**
61 * The preferred read preference.
62 */
63 readPreference?: mongodb.ReadPreferenceLike;
64 /**
65 * Specifies the write concern.
66 */
67 writeConcern?: mongodb.WriteConcern;
68 [key: string]: any;
69 }
70
71 class Aggregate<R> implements SessionOperation {
72 /**
73 * Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js)
74 * You do not need to call this function explicitly, the JavaScript runtime
75 * will call it for you.
76 */
77 [Symbol.asyncIterator](): AsyncIterableIterator<Unpacked<R>>;
78
79 options: AggregateOptions;
80
81 /**
82 * Sets an option on this aggregation. This function will be deprecated in a
83 * future release.
84 *
85 * @deprecated
86 */
87 addCursorFlag(flag: CursorFlag, value: boolean): this;
88
89 /**
90 * Appends a new $addFields operator to this aggregate pipeline.
91 * Requires MongoDB v3.4+ to work
92 */
93 addFields(arg: PipelineStage.AddFields['$addFields']): this;
94
95 /** Sets the allowDiskUse option for the aggregation query */
96 allowDiskUse(value: boolean): this;
97
98 /** Appends new operators to this aggregate pipeline */
99 append(...args: PipelineStage[]): this;
100
101 /**
102 * Executes the query returning a `Promise` which will be
103 * resolved with either the doc(s) or rejected with the error.
104 * Like [`.then()`](#query_Query-then), but only takes a rejection handler.
105 */
106 catch: Promise<R>['catch'];
107
108 /** Set the collation. */
109 collation(options: mongodb.CollationOptions): this;
110
111 /** Appends a new $count operator to this aggregate pipeline. */
112 count(fieldName: PipelineStage.Count['$count']): this;
113
114 /** Appends a new $densify operator to this aggregate pipeline */
115 densify(arg: PipelineStage.Densify['$densify']): this;
116
117 /**
118 * Sets the cursor option for the aggregation query
119 */
120 cursor<DocType = any>(options?: Record<string, unknown>): Cursor<DocType>;
121
122
123 /** Executes the aggregate pipeline on the currently bound Model. */
124 exec(callback: Callback<R>): void;
125 exec(): Promise<R>;
126
127 /** Execute the aggregation with explain */
128 explain(verbosity: mongodb.ExplainVerbosityLike, callback: Callback<AnyObject>): void;
129 explain(verbosity: mongodb.ExplainVerbosityLike): Promise<AnyObject>;
130 explain(callback: Callback<AnyObject>): void;
131 explain(): Promise<AnyObject>;
132
133 /** Combines multiple aggregation pipelines. */
134 facet(options: PipelineStage.Facet['$facet']): this;
135
136 /** Appends new custom $graphLookup operator(s) to this aggregate pipeline, performing a recursive search on a collection. */
137 graphLookup(options: PipelineStage.GraphLookup['$graphLookup']): this;
138
139 /** Appends new custom $group operator to this aggregate pipeline. */
140 group(arg: PipelineStage.Group['$group']): this;
141
142 /** Sets the hint option for the aggregation query */
143 hint(value: Record<string, unknown> | string): this;
144
145 /**
146 * Appends a new $limit operator to this aggregate pipeline.
147 * @param num maximum number of records to pass to the next stage
148 */
149 limit(num: PipelineStage.Limit['$limit']): this;
150
151 /** Appends new custom $lookup operator to this aggregate pipeline. */
152 lookup(options: PipelineStage.Lookup['$lookup']): this;
153
154 /**
155 * Appends a new custom $match operator to this aggregate pipeline.
156 * @param arg $match operator contents
157 */
158 match(arg: PipelineStage.Match['$match']): this;
159
160 /**
161 * Binds this aggregate to a model.
162 * @param model the model to which the aggregate is to be bound
163 */
164 model(model: Model<any>): this;
165
166 /**
167 * Append a new $near operator to this aggregation pipeline
168 * @param arg $near operator contents
169 */
170 near(arg: { near?: number[]; distanceField: string; maxDistance?: number; query?: Record<string, any>; includeLocs?: string; num?: number; uniqueDocs?: boolean }): this;
171
172 /** Returns the current pipeline */
173 pipeline(): PipelineStage[];
174
175 /** Appends a new $project operator to this aggregate pipeline. */
176 project(arg: PipelineStage.Project['$project']): this;
177
178 /** Sets the readPreference option for the aggregation query. */
179 read(pref: mongodb.ReadPreferenceLike): this;
180
181 /** Sets the readConcern level for the aggregation query. */
182 readConcern(level: string): this;
183
184 /** Appends a new $redact operator to this aggregate pipeline. */
185 redact(expression: PipelineStage.Redact['$redact'], thenExpr: '$$DESCEND' | '$$PRUNE' | '$$KEEP' | AnyObject, elseExpr: '$$DESCEND' | '$$PRUNE' | '$$KEEP' | AnyObject): this;
186
187 /** Appends a new $replaceRoot operator to this aggregate pipeline. */
188 replaceRoot(newRoot: PipelineStage.ReplaceRoot['$replaceRoot']['newRoot'] | string): this;
189
190 /**
191 * Helper for [Atlas Text Search](https://docs.atlas.mongodb.com/reference/atlas-search/tutorial/)'s
192 * `$search` stage.
193 */
194 search(options: PipelineStage.Search['$search']): this;
195
196 /** Lets you set arbitrary options, for middlewares or plugins. */
197 option(value: AggregateOptions): this;
198
199 /** Appends new custom $sample operator to this aggregate pipeline. */
200 sample(arg: PipelineStage.Sample['$sample']['size']): this;
201
202 /** Sets the session for this aggregation. Useful for [transactions](/docs/transactions.html). */
203 session(session: mongodb.ClientSession | null): this;
204
205 /**
206 * Appends a new $skip operator to this aggregate pipeline.
207 * @param num number of records to skip before next stage
208 */
209 skip(num: PipelineStage.Skip['$skip']): this;
210
211 /** Appends a new $sort operator to this aggregate pipeline. */
212 sort(arg: string | Record<string, SortValues> | PipelineStage.Sort['$sort']): this;
213
214 /** Provides promise for aggregate. */
215 then: Promise<R>['then'];
216
217 /**
218 * Appends a new $sortByCount operator to this aggregate pipeline. Accepts either a string field name
219 * or a pipeline object.
220 */
221 sortByCount(arg: string | PipelineStage.SortByCount['$sortByCount']): this;
222
223 /** Appends new $unionWith operator to this aggregate pipeline. */
224 unionWith(options: PipelineStage.UnionWith['$unionWith']): this;
225
226 /** Appends new custom $unwind operator(s) to this aggregate pipeline. */
227 unwind(...args: PipelineStage.Unwind['$unwind'][]): this;
228 }
229}