UNPKG

6.68 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 Omit<mongodb.AggregateOptions, 'session'>, SessionOption {
8 [key: string]: any;
9 }
10
11 class Aggregate<ResultType> implements SessionOperation {
12 /**
13 * Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js)
14 * You do not need to call this function explicitly, the JavaScript runtime
15 * will call it for you.
16 */
17 [Symbol.asyncIterator](): AsyncIterableIterator<Unpacked<ResultType>>;
18
19 // Returns a string representation of this aggregation.
20 [Symbol.toStringTag]: string;
21
22 options: AggregateOptions;
23
24 /**
25 * Sets an option on this aggregation. This function will be deprecated in a
26 * future release.
27 *
28 * @deprecated
29 */
30 addCursorFlag(flag: CursorFlag, value: boolean): this;
31
32 /**
33 * Appends a new $addFields operator to this aggregate pipeline.
34 * Requires MongoDB v3.4+ to work
35 */
36 addFields(arg: PipelineStage.AddFields['$addFields']): this;
37
38 /** Sets the allowDiskUse option for the aggregation query */
39 allowDiskUse(value: boolean): this;
40
41 /** Appends new operators to this aggregate pipeline */
42 append(...args: PipelineStage[]): this;
43
44 /**
45 * Executes the query returning a `Promise` which will be
46 * resolved with either the doc(s) or rejected with the error.
47 * Like [`.then()`](#query_Query-then), but only takes a rejection handler.
48 */
49 catch: Promise<ResultType>['catch'];
50
51 /** Set the collation. */
52 collation(options: mongodb.CollationOptions): this;
53
54 /** Appends a new $count operator to this aggregate pipeline. */
55 count(fieldName: PipelineStage.Count['$count']): this;
56
57 /** Appends a new $densify operator to this aggregate pipeline */
58 densify(arg: PipelineStage.Densify['$densify']): this;
59
60 /**
61 * Sets the cursor option for the aggregation query
62 */
63 cursor<DocType = any>(options?: Record<string, unknown>): Cursor<DocType>;
64
65
66 /** Executes the aggregate pipeline on the currently bound Model. */
67 exec(): Promise<ResultType>;
68
69 /** Execute the aggregation with explain */
70 explain(verbosity: mongodb.ExplainVerbosityLike): Promise<AnyObject>;
71 explain(): Promise<AnyObject>;
72
73 /** Combines multiple aggregation pipelines. */
74 facet(options: PipelineStage.Facet['$facet']): this;
75
76 /** Appends a new $fill operator to this aggregate pipeline */
77 fill(arg: PipelineStage.Fill['$fill']): this;
78
79 /**
80 * Executes the aggregation returning a `Promise` which will be
81 * resolved with `.finally()` chained.
82 */
83 finally: Promise<ResultType>['finally'];
84
85 /** Appends new custom $graphLookup operator(s) to this aggregate pipeline, performing a recursive search on a collection. */
86 graphLookup(options: PipelineStage.GraphLookup['$graphLookup']): this;
87
88 /** Appends new custom $group operator to this aggregate pipeline. */
89 group(arg: PipelineStage.Group['$group']): this;
90
91 /** Sets the hint option for the aggregation query */
92 hint(value: Record<string, unknown> | string): this;
93
94 /**
95 * Appends a new $limit operator to this aggregate pipeline.
96 * @param num maximum number of records to pass to the next stage
97 */
98 limit(num: PipelineStage.Limit['$limit']): this;
99
100 /** Appends new custom $lookup operator to this aggregate pipeline. */
101 lookup(options: PipelineStage.Lookup['$lookup']): this;
102
103 /**
104 * Appends a new custom $match operator to this aggregate pipeline.
105 * @param arg $match operator contents
106 */
107 match(arg: PipelineStage.Match['$match']): this;
108
109 /**
110 * Binds this aggregate to a model.
111 * @param model the model to which the aggregate is to be bound
112 */
113 model(model: Model<any>): this;
114
115 /**
116 * Returns the current model bound to this aggregate object
117 */
118 model(): Model<any>;
119
120 /** Appends a new $geoNear operator to this aggregate pipeline. */
121 near(arg: PipelineStage.GeoNear['$geoNear']): this;
122
123 /** Returns the current pipeline */
124 pipeline(): PipelineStage[];
125
126 /** Appends a new $project operator to this aggregate pipeline. */
127 project(arg: PipelineStage.Project['$project']): this;
128
129 /** Sets the readPreference option for the aggregation query. */
130 read(pref: mongodb.ReadPreferenceLike): this;
131
132 /** Sets the readConcern level for the aggregation query. */
133 readConcern(level: string): this;
134
135 /** Appends a new $redact operator to this aggregate pipeline. */
136 redact(expression: PipelineStage.Redact['$redact'], thenExpr: '$$DESCEND' | '$$PRUNE' | '$$KEEP' | AnyObject, elseExpr: '$$DESCEND' | '$$PRUNE' | '$$KEEP' | AnyObject): this;
137
138 /** Appends a new $replaceRoot operator to this aggregate pipeline. */
139 replaceRoot(newRoot: PipelineStage.ReplaceRoot['$replaceRoot']['newRoot'] | string): this;
140
141 /**
142 * Helper for [Atlas Text Search](https://www.mongodb.com/docs/atlas/atlas-search/tutorial/)'s
143 * `$search` stage.
144 */
145 search(options: PipelineStage.Search['$search']): this;
146
147 /** Lets you set arbitrary options, for middlewares or plugins. */
148 option(value: AggregateOptions): this;
149
150 /** Appends new custom $sample operator to this aggregate pipeline. */
151 sample(arg: PipelineStage.Sample['$sample']['size']): this;
152
153 /** Sets the session for this aggregation. Useful for [transactions](/docs/transactions.html). */
154 session(session: mongodb.ClientSession | null): this;
155
156 /**
157 * Appends a new $skip operator to this aggregate pipeline.
158 * @param num number of records to skip before next stage
159 */
160 skip(num: PipelineStage.Skip['$skip']): this;
161
162 /** Appends a new $sort operator to this aggregate pipeline. */
163 sort(arg: string | Record<string, SortValues> | PipelineStage.Sort['$sort']): this;
164
165 /** Provides promise for aggregate. */
166 then: Promise<ResultType>['then'];
167
168 /**
169 * Appends a new $sortByCount operator to this aggregate pipeline. Accepts either a string field name
170 * or a pipeline object.
171 */
172 sortByCount(arg: string | PipelineStage.SortByCount['$sortByCount']): this;
173
174 /** Appends new $unionWith operator to this aggregate pipeline. */
175 unionWith(options: PipelineStage.UnionWith['$unionWith']): this;
176
177 /** Appends new custom $unwind operator(s) to this aggregate pipeline. */
178 unwind(...args: PipelineStage.Unwind['$unwind'][]): this;
179 }
180}