UNPKG

36.6 kBTypeScriptView Raw
1declare module 'mongoose' {
2 import mongodb = require('mongodb');
3
4 export interface DiscriminatorOptions {
5 value?: string | number | ObjectId;
6 clone?: boolean;
7 overwriteModels?: boolean;
8 mergeHooks?: boolean;
9 mergePlugins?: boolean;
10 }
11
12 export interface AcceptsDiscriminator {
13 /** Adds a discriminator type. */
14 discriminator<D>(
15 name: string | number,
16 schema: Schema,
17 value?: string | number | ObjectId | DiscriminatorOptions
18 ): Model<D>;
19 discriminator<T, U>(
20 name: string | number,
21 schema: Schema<T, U>,
22 value?: string | number | ObjectId | DiscriminatorOptions
23 ): U;
24 }
25
26 interface MongooseBulkWriteOptions extends mongodb.BulkWriteOptions {
27 session?: ClientSession;
28 skipValidation?: boolean;
29 throwOnValidationError?: boolean;
30 strict?: boolean | 'throw';
31 /** When false, do not add timestamps to documents. Can be overridden at the operation level. */
32 timestamps?: boolean;
33 }
34
35 interface MongooseBulkSaveOptions extends mongodb.BulkWriteOptions {
36 timestamps?: boolean;
37 session?: ClientSession;
38 }
39
40 /**
41 * @deprecated use AnyBulkWriteOperation instead
42 */
43 interface MongooseBulkWritePerWriteOptions {
44 timestamps?: boolean;
45 strict?: boolean | 'throw';
46 session?: ClientSession;
47 skipValidation?: boolean;
48 }
49
50 interface HydrateOptions {
51 setters?: boolean;
52 hydratedPopulatedDocs?: boolean;
53 }
54
55 interface InsertManyOptions extends
56 PopulateOption,
57 SessionOption {
58 limit?: number;
59 // @deprecated, use includeResultMetadata instead
60 rawResult?: boolean;
61 includeResultMetadata?: boolean;
62 ordered?: boolean;
63 lean?: boolean;
64 throwOnValidationError?: boolean;
65 }
66
67 type InsertManyResult<T> = mongodb.InsertManyResult<T> & {
68 insertedIds: {
69 [key: number]: InferId<T>;
70 };
71 mongoose?: { validationErrors?: Array<Error.CastError | Error.ValidatorError> };
72 };
73
74 type UpdateWriteOpResult = mongodb.UpdateResult;
75
76 interface MapReduceOptions<T, K, R> {
77 map: Function | string;
78 reduce: (key: K, vals: T[]) => R;
79 /** query filter object. */
80 query?: any;
81 /** sort input objects using this key */
82 sort?: any;
83 /** max number of documents */
84 limit?: number;
85 /** keep temporary data default: false */
86 keeptemp?: boolean;
87 /** finalize function */
88 finalize?: (key: K, val: R) => R;
89 /** scope variables exposed to map/reduce/finalize during execution */
90 scope?: any;
91 /** it is possible to make the execution stay in JS. Provided in MongoDB > 2.0.X default: false */
92 jsMode?: boolean;
93 /** provide statistics on job execution time. default: false */
94 verbose?: boolean;
95 readPreference?: string;
96 /** sets the output target for the map reduce job. default: {inline: 1} */
97 out?: {
98 /** the results are returned in an array */
99 inline?: number;
100 /**
101 * {replace: 'collectionName'} add the results to collectionName: the
102 * results replace the collection
103 */
104 replace?: string;
105 /**
106 * {reduce: 'collectionName'} add the results to collectionName: if
107 * dups are detected, uses the reducer / finalize functions
108 */
109 reduce?: string;
110 /**
111 * {merge: 'collectionName'} add the results to collectionName: if
112 * dups exist the new docs overwrite the old
113 */
114 merge?: string;
115 };
116 }
117
118 interface GeoSearchOptions {
119 /** x,y point to search for */
120 near: number[];
121 /** the maximum distance from the point near that a result can be */
122 maxDistance: number;
123 /** The maximum number of results to return */
124 limit?: number;
125 /** return the raw object instead of the Mongoose Model */
126 lean?: boolean;
127 }
128
129 interface ModifyResult<T> {
130 value: Require_id<T> | null;
131 /** see https://www.mongodb.com/docs/manual/reference/command/findAndModify/#lasterrorobject */
132 lastErrorObject?: {
133 updatedExisting?: boolean;
134 upserted?: mongodb.ObjectId;
135 };
136 ok: 0 | 1;
137 }
138
139 type WriteConcern = mongodb.WriteConcern;
140
141 /** A list of paths to validate. If set, Mongoose will validate only the modified paths that are in the given list. */
142 type PathsToValidate = string[] | string;
143 /**
144 * @deprecated
145 */
146 type pathsToValidate = PathsToValidate;
147
148 interface SaveOptions extends
149 SessionOption {
150 checkKeys?: boolean;
151 j?: boolean;
152 safe?: boolean | WriteConcern;
153 timestamps?: boolean | QueryTimestampsConfig;
154 validateBeforeSave?: boolean;
155 validateModifiedOnly?: boolean;
156 w?: number | string;
157 wtimeout?: number;
158 }
159
160 interface CreateOptions extends SaveOptions {
161 ordered?: boolean;
162 aggregateErrors?: boolean;
163 }
164
165 interface RemoveOptions extends SessionOption, Omit<mongodb.DeleteOptions, 'session'> {}
166
167 const Model: Model<any>;
168
169 export type AnyBulkWriteOperation<TSchema = AnyObject> = {
170 insertOne: InsertOneModel<TSchema>;
171 } | {
172 replaceOne: ReplaceOneModel<TSchema>;
173 } | {
174 updateOne: UpdateOneModel<TSchema>;
175 } | {
176 updateMany: UpdateManyModel<TSchema>;
177 } | {
178 deleteOne: DeleteOneModel<TSchema>;
179 } | {
180 deleteMany: DeleteManyModel<TSchema>;
181 };
182
183 export interface InsertOneModel<TSchema> {
184 document: mongodb.OptionalId<TSchema>;
185 /** When false, do not add timestamps. When true, overrides the `timestamps` option set in the `bulkWrite` options. */
186 timestamps?: boolean;
187 }
188
189 export interface ReplaceOneModel<TSchema = AnyObject> {
190 /** The filter to limit the replaced document. */
191 filter: FilterQuery<TSchema>;
192 /** The document with which to replace the matched document. */
193 replacement: mongodb.WithoutId<TSchema>;
194 /** Specifies a collation. */
195 collation?: mongodb.CollationOptions;
196 /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
197 hint?: mongodb.Hint;
198 /** When true, creates a new document if no document matches the query. */
199 upsert?: boolean;
200 /** When false, do not add timestamps. When true, overrides the `timestamps` option set in the `bulkWrite` options. */
201 timestamps?: boolean;
202 }
203
204 export interface UpdateOneModel<TSchema = AnyObject> {
205 /** The filter to limit the updated documents. */
206 filter: FilterQuery<TSchema>;
207 /** A document or pipeline containing update operators. */
208 update: UpdateQuery<TSchema>;
209 /** A set of filters specifying to which array elements an update should apply. */
210 arrayFilters?: AnyObject[];
211 /** Specifies a collation. */
212 collation?: mongodb.CollationOptions;
213 /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
214 hint?: mongodb.Hint;
215 /** When true, creates a new document if no document matches the query. */
216 upsert?: boolean;
217 /** When false, do not add timestamps. When true, overrides the `timestamps` option set in the `bulkWrite` options. */
218 timestamps?: boolean;
219 }
220
221 export interface UpdateManyModel<TSchema = AnyObject> {
222 /** The filter to limit the updated documents. */
223 filter: FilterQuery<TSchema>;
224 /** A document or pipeline containing update operators. */
225 update: UpdateQuery<TSchema>;
226 /** A set of filters specifying to which array elements an update should apply. */
227 arrayFilters?: AnyObject[];
228 /** Specifies a collation. */
229 collation?: mongodb.CollationOptions;
230 /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
231 hint?: mongodb.Hint;
232 /** When true, creates a new document if no document matches the query. */
233 upsert?: boolean;
234 /** When false, do not add timestamps. When true, overrides the `timestamps` option set in the `bulkWrite` options. */
235 timestamps?: boolean;
236 }
237
238 export interface DeleteOneModel<TSchema = AnyObject> {
239 /** The filter to limit the deleted documents. */
240 filter: FilterQuery<TSchema>;
241 /** Specifies a collation. */
242 collation?: mongodb.CollationOptions;
243 /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
244 hint?: mongodb.Hint;
245 }
246
247 export interface DeleteManyModel<TSchema = AnyObject> {
248 /** The filter to limit the deleted documents. */
249 filter: FilterQuery<TSchema>;
250 /** Specifies a collation. */
251 collation?: mongodb.CollationOptions;
252 /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
253 hint?: mongodb.Hint;
254 }
255
256 /**
257 * Models are fancy constructors compiled from `Schema` definitions.
258 * An instance of a model is called a document.
259 * Models are responsible for creating and reading documents from the underlying MongoDB database
260 */
261 export interface Model<
262 TRawDocType,
263 TQueryHelpers = {},
264 TInstanceMethods = {},
265 TVirtuals = {},
266 THydratedDocumentType = HydratedDocument<TRawDocType, TVirtuals & TInstanceMethods, TQueryHelpers>,
267 TSchema = any> extends
268 NodeJS.EventEmitter,
269 AcceptsDiscriminator,
270 IndexManager,
271 SessionStarter {
272 new <DocType = Partial<TRawDocType>>(doc?: DocType, fields?: any | null, options?: boolean | AnyObject): THydratedDocumentType;
273
274 aggregate<R = any>(pipeline?: PipelineStage[], options?: AggregateOptions): Aggregate<Array<R>>;
275 aggregate<R = any>(pipeline: PipelineStage[]): Aggregate<Array<R>>;
276
277 /** Base Mongoose instance the model uses. */
278 base: Mongoose;
279
280 /**
281 * If this is a discriminator model, `baseModelName` is the name of
282 * the base model.
283 */
284 baseModelName: string | undefined;
285
286 /* Cast the given POJO to the model's schema */
287 castObject(obj: AnyObject, options?: { ignoreCastErrors?: boolean }): TRawDocType;
288
289 /* Apply defaults to the given document or POJO. */
290 applyDefaults(obj: AnyObject): AnyObject;
291 applyDefaults(obj: TRawDocType): TRawDocType;
292
293 /**
294 * Sends multiple `insertOne`, `updateOne`, `updateMany`, `replaceOne`,
295 * `deleteOne`, and/or `deleteMany` operations to the MongoDB server in one
296 * command. This is faster than sending multiple independent operations (e.g.
297 * if you use `create()`) because with `bulkWrite()` there is only one network
298 * round trip to the MongoDB server.
299 */
300 bulkWrite<DocContents = TRawDocType>(
301 writes: Array<AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
302 options: MongooseBulkWriteOptions & { ordered: false }
303 ): Promise<mongodb.BulkWriteResult & { mongoose?: { validationErrors: Error[] } }>;
304 bulkWrite<DocContents = TRawDocType>(
305 writes: Array<AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
306 options?: MongooseBulkWriteOptions
307 ): Promise<mongodb.BulkWriteResult>;
308
309 /**
310 * Sends multiple `save()` calls in a single `bulkWrite()`. This is faster than
311 * sending multiple `save()` calls because with `bulkSave()` there is only one
312 * network round trip to the MongoDB server.
313 */
314 bulkSave(documents: Array<Document>, options?: MongooseBulkSaveOptions): Promise<mongodb.BulkWriteResult>;
315
316 /** Collection the model uses. */
317 collection: Collection;
318
319 /** Creates a `countDocuments` query: counts the number of documents that match `filter`. */
320 countDocuments(
321 filter?: FilterQuery<TRawDocType>,
322 options?: (mongodb.CountOptions & MongooseBaseQueryOptions<TRawDocType>) | null
323 ): QueryWithHelpers<
324 number,
325 THydratedDocumentType,
326 TQueryHelpers,
327 TRawDocType,
328 'countDocuments',
329 TInstanceMethods
330 >;
331
332 /** Creates a new document or documents */
333 create<DocContents = AnyKeys<TRawDocType>>(docs: Array<TRawDocType | DocContents>, options: CreateOptions & { aggregateErrors: true }): Promise<(THydratedDocumentType | Error)[]>;
334 create<DocContents = AnyKeys<TRawDocType>>(docs: Array<TRawDocType | DocContents>, options?: CreateOptions): Promise<THydratedDocumentType[]>;
335 create<DocContents = AnyKeys<TRawDocType>>(doc: DocContents | TRawDocType): Promise<THydratedDocumentType>;
336 create<DocContents = AnyKeys<TRawDocType>>(...docs: Array<TRawDocType | DocContents>): Promise<THydratedDocumentType[]>;
337
338 /**
339 * Create the collection for this model. By default, if no indexes are specified,
340 * mongoose will not create the collection for the model until any documents are
341 * created. Use this method to create the collection explicitly.
342 */
343 createCollection<T extends mongodb.Document>(options?: mongodb.CreateCollectionOptions & Pick<SchemaOptions, 'expires'>): Promise<mongodb.Collection<T>>;
344
345 /**
346 * Create an [Atlas search index](https://www.mongodb.com/docs/atlas/atlas-search/create-index/).
347 * This function only works when connected to MongoDB Atlas.
348 */
349 createSearchIndex(description: SearchIndexDescription): Promise<string>;
350
351 /** Connection the model uses. */
352 db: Connection;
353
354 /**
355 * Deletes all of the documents that match `conditions` from the collection.
356 * Behaves like `remove()`, but deletes all documents that match `conditions`
357 * regardless of the `single` option.
358 */
359 deleteMany(
360 filter?: FilterQuery<TRawDocType>,
361 options?: (mongodb.DeleteOptions & MongooseBaseQueryOptions<TRawDocType>) | null
362 ): QueryWithHelpers<
363 mongodb.DeleteResult,
364 THydratedDocumentType,
365 TQueryHelpers,
366 TRawDocType,
367 'deleteMany',
368 TInstanceMethods
369 >;
370 deleteMany(
371 filter: FilterQuery<TRawDocType>
372 ): QueryWithHelpers<
373 mongodb.DeleteResult,
374 THydratedDocumentType,
375 TQueryHelpers,
376 TRawDocType,
377 'deleteMany',
378 TInstanceMethods
379 >;
380
381 /**
382 * Deletes the first document that matches `conditions` from the collection.
383 * Behaves like `remove()`, but deletes at most one document regardless of the
384 * `single` option.
385 */
386 deleteOne(
387 filter?: FilterQuery<TRawDocType>,
388 options?: (mongodb.DeleteOptions & MongooseBaseQueryOptions<TRawDocType>) | null
389 ): QueryWithHelpers<
390 mongodb.DeleteResult,
391 THydratedDocumentType,
392 TQueryHelpers,
393 TRawDocType,
394 'deleteOne',
395 TInstanceMethods
396 >;
397 deleteOne(
398 filter: FilterQuery<TRawDocType>
399 ): QueryWithHelpers<
400 mongodb.DeleteResult,
401 THydratedDocumentType,
402 TQueryHelpers,
403 TRawDocType,
404 'deleteOne',
405 TInstanceMethods
406 >;
407
408 /**
409 * Delete an existing [Atlas search index](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) by name.
410 * This function only works when connected to MongoDB Atlas.
411 */
412 dropSearchIndex(name: string): Promise<void>;
413
414 /**
415 * Event emitter that reports any errors that occurred. Useful for global error
416 * handling.
417 */
418 events: NodeJS.EventEmitter;
419
420 /**
421 * Finds a single document by its _id field. `findById(id)` is almost*
422 * equivalent to `findOne({ _id: id })`. If you want to query by a document's
423 * `_id`, use `findById()` instead of `findOne()`.
424 */
425 findById<ResultDoc = THydratedDocumentType>(
426 id: any,
427 projection: ProjectionType<TRawDocType> | null | undefined,
428 options: QueryOptions<TRawDocType> & { lean: true }
429 ): QueryWithHelpers<
430 GetLeanResultType<TRawDocType, TRawDocType, 'findOne'> | null,
431 ResultDoc,
432 TQueryHelpers,
433 TRawDocType,
434 'findOne',
435 TInstanceMethods
436 >;
437 findById<ResultDoc = THydratedDocumentType>(
438 id: any,
439 projection?: ProjectionType<TRawDocType> | null,
440 options?: QueryOptions<TRawDocType> | null
441 ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOne', TInstanceMethods>;
442 findById<ResultDoc = THydratedDocumentType>(
443 id: any,
444 projection?: ProjectionType<TRawDocType> | null
445 ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOne', TInstanceMethods>;
446
447 /** Finds one document. */
448 findOne<ResultDoc = THydratedDocumentType>(
449 filter: FilterQuery<TRawDocType>,
450 projection: ProjectionType<TRawDocType> | null | undefined,
451 options: QueryOptions<TRawDocType> & { lean: true }
452 ): QueryWithHelpers<
453 GetLeanResultType<TRawDocType, TRawDocType, 'findOne'> | null,
454 ResultDoc,
455 TQueryHelpers,
456 TRawDocType,
457 'findOne',
458 TInstanceMethods
459 >;
460 findOne<ResultDoc = THydratedDocumentType>(
461 filter?: FilterQuery<TRawDocType>,
462 projection?: ProjectionType<TRawDocType> | null,
463 options?: QueryOptions<TRawDocType> | null
464 ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOne', TInstanceMethods>;
465 findOne<ResultDoc = THydratedDocumentType>(
466 filter?: FilterQuery<TRawDocType>,
467 projection?: ProjectionType<TRawDocType> | null
468 ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOne', TInstanceMethods>;
469 findOne<ResultDoc = THydratedDocumentType>(
470 filter?: FilterQuery<TRawDocType>
471 ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOne', TInstanceMethods>;
472
473 /**
474 * Shortcut for creating a new Document from existing raw data, pre-saved in the DB.
475 * The document returned has no paths marked as modified initially.
476 */
477 hydrate(obj: any, projection?: AnyObject, options?: HydrateOptions): THydratedDocumentType;
478
479 /**
480 * This function is responsible for building [indexes](https://www.mongodb.com/docs/manual/indexes/),
481 * unless [`autoIndex`](http://mongoosejs.com/docs/guide.html#autoIndex) is turned off.
482 * Mongoose calls this function automatically when a model is created using
483 * [`mongoose.model()`](/docs/api/mongoose.html#mongoose_Mongoose-model) or
484 * [`connection.model()`](/docs/api/connection.html#connection_Connection-model), so you
485 * don't need to call it.
486 */
487 init(): Promise<THydratedDocumentType>;
488
489 /** Inserts one or more new documents as a single `insertMany` call to the MongoDB server. */
490 insertMany(
491 docs: Array<TRawDocType>
492 ): Promise<Array<THydratedDocumentType>>;
493 insertMany(
494 docs: Array<TRawDocType>,
495 options: InsertManyOptions & { lean: true; }
496 ): Promise<Array<Require_id<TRawDocType>>>;
497 insertMany(
498 doc: Array<TRawDocType>,
499 options: InsertManyOptions & { ordered: false; rawResult: true; }
500 ): Promise<mongodb.InsertManyResult<Require_id<TRawDocType>> & {
501 mongoose: {
502 validationErrors: (CastError | Error.ValidatorError)[];
503 results: Array<
504 Error |
505 Object |
506 THydratedDocumentType
507 >
508 }
509 }>;
510 insertMany(
511 docs: Array<TRawDocType>,
512 options: InsertManyOptions & { lean: true, rawResult: true; }
513 ): Promise<mongodb.InsertManyResult<Require_id<TRawDocType>>>;
514 insertMany(
515 docs: Array<TRawDocType>,
516 options: InsertManyOptions & { rawResult: true; }
517 ): Promise<mongodb.InsertManyResult<Require_id<THydratedDocumentType>>>;
518 insertMany(
519 doc: Array<TRawDocType>,
520 options: InsertManyOptions
521 ): Promise<Array<THydratedDocumentType>>;
522 insertMany<DocContents = TRawDocType>(
523 docs: Array<DocContents | TRawDocType>,
524 options: InsertManyOptions & { lean: true; }
525 ): Promise<Array<Require_id<DocContents>>>;
526 insertMany<DocContents = TRawDocType>(
527 docs: DocContents | TRawDocType,
528 options: InsertManyOptions & { lean: true; }
529 ): Promise<Array<Require_id<DocContents>>>;
530 insertMany<DocContents = TRawDocType>(
531 doc: DocContents | TRawDocType,
532 options: InsertManyOptions & { ordered: false; rawResult: true; }
533 ): Promise<mongodb.InsertManyResult<Require_id<DocContents>> & {
534 mongoose: {
535 validationErrors: (CastError | Error.ValidatorError)[];
536 results: Array<
537 Error |
538 Object |
539 MergeType<THydratedDocumentType, DocContents>
540 >
541 }
542 }>;
543 insertMany<DocContents = TRawDocType>(
544 docs: Array<DocContents | TRawDocType>,
545 options: InsertManyOptions & { rawResult: true; }
546 ): Promise<mongodb.InsertManyResult<Require_id<DocContents>>>;
547 insertMany<DocContents = TRawDocType>(
548 docs: Array<DocContents | TRawDocType>
549 ): Promise<Array<MergeType<THydratedDocumentType, Omit<DocContents, '_id'>>>>;
550 insertMany<DocContents = TRawDocType>(
551 doc: DocContents,
552 options: InsertManyOptions & { lean: true; }
553 ): Promise<Array<Require_id<DocContents>>>;
554 insertMany<DocContents = TRawDocType>(
555 doc: DocContents,
556 options: InsertManyOptions & { rawResult: true; }
557 ): Promise<mongodb.InsertManyResult<Require_id<DocContents>>>;
558 insertMany<DocContents = TRawDocType>(
559 doc: DocContents,
560 options: InsertManyOptions
561 ): Promise<Array<MergeType<THydratedDocumentType, Omit<DocContents, '_id'>>>>;
562 insertMany<DocContents = TRawDocType>(
563 docs: Array<DocContents | TRawDocType>,
564 options: InsertManyOptions
565 ): Promise<Array<MergeType<THydratedDocumentType, Omit<DocContents, '_id'>>>>;
566 insertMany<DocContents = TRawDocType>(
567 doc: DocContents
568 ): Promise<
569 Array<MergeType<THydratedDocumentType, Omit<DocContents, '_id'>>>
570 >;
571
572 /**
573 * List all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) on this model's collection.
574 * This function only works when connected to MongoDB Atlas.
575 */
576 listSearchIndexes(options?: mongodb.ListSearchIndexesOptions): Promise<Array<{ name: string }>>;
577
578 /** The name of the model */
579 modelName: string;
580
581 /** Populates document references. */
582 populate(
583 docs: Array<any>,
584 options: PopulateOptions | Array<PopulateOptions> | string
585 ): Promise<Array<THydratedDocumentType>>;
586 populate(
587 doc: any, options: PopulateOptions | Array<PopulateOptions> | string
588 ): Promise<THydratedDocumentType>;
589 populate<Paths>(
590 docs: Array<any>,
591 options: PopulateOptions | Array<PopulateOptions> | string
592 ): Promise<Array<MergeType<THydratedDocumentType, Paths>>>;
593 populate<Paths>(
594 doc: any, options: PopulateOptions | Array<PopulateOptions> | string
595 ): Promise<MergeType<THydratedDocumentType, Paths>>;
596
597 /**
598 * Update an existing [Atlas search index](https://www.mongodb.com/docs/atlas/atlas-search/create-index/).
599 * This function only works when connected to MongoDB Atlas.
600 */
601 updateSearchIndex(name: string, definition: AnyObject): Promise<void>;
602
603 /** Casts and validates the given object against this model's schema, passing the given `context` to custom validators. */
604 validate(): Promise<void>;
605 validate(obj: any): Promise<void>;
606 validate(obj: any, pathsOrOptions: PathsToValidate): Promise<void>;
607 validate(obj: any, pathsOrOptions: { pathsToSkip?: pathsToSkip }): Promise<void>;
608
609 /** Watches the underlying collection for changes using [MongoDB change streams](https://www.mongodb.com/docs/manual/changeStreams/). */
610 watch<ResultType extends mongodb.Document = any, ChangeType extends mongodb.ChangeStreamDocument = any>(pipeline?: Array<Record<string, unknown>>, options?: mongodb.ChangeStreamOptions & { hydrate?: boolean }): mongodb.ChangeStream<ResultType, ChangeType>;
611
612 /** Adds a `$where` clause to this query */
613 $where(argument: string | Function): QueryWithHelpers<Array<THydratedDocumentType>, THydratedDocumentType, TQueryHelpers, TRawDocType, 'find', TInstanceMethods>;
614
615 /** Registered discriminators for this model. */
616 discriminators: { [name: string]: Model<any> } | undefined;
617
618 /** Translate any aliases fields/conditions so the final query or document object is pure */
619 translateAliases(raw: any): any;
620
621 /** Creates a `distinct` query: returns the distinct values of the given `field` that match `filter`. */
622 distinct<DocKey extends string, ResultType = unknown>(
623 field: DocKey,
624 filter?: FilterQuery<TRawDocType>
625 ): QueryWithHelpers<
626 Array<
627 DocKey extends keyof WithLevel1NestedPaths<TRawDocType>
628 ? WithoutUndefined<Unpacked<WithLevel1NestedPaths<TRawDocType>[DocKey]>>
629 : ResultType
630 >,
631 THydratedDocumentType,
632 TQueryHelpers,
633 TRawDocType,
634 'distinct',
635 TInstanceMethods
636 >;
637
638 /** Creates a `estimatedDocumentCount` query: counts the number of documents in the collection. */
639 estimatedDocumentCount(options?: QueryOptions<TRawDocType>): QueryWithHelpers<
640 number,
641 THydratedDocumentType,
642 TQueryHelpers,
643 TRawDocType,
644 'estimatedDocumentCount',
645 TInstanceMethods
646 >;
647
648 /**
649 * Returns a document with its `_id` if at least one document exists in the database that matches
650 * the given `filter`, and `null` otherwise.
651 */
652 exists(
653 filter: FilterQuery<TRawDocType>
654 ): QueryWithHelpers<
655 { _id: InferId<TRawDocType> } | null,
656 THydratedDocumentType,
657 TQueryHelpers,
658 TRawDocType,
659 'findOne',
660 TInstanceMethods
661 >;
662
663 /** Creates a `find` query: gets a list of documents that match `filter`. */
664 find<ResultDoc = THydratedDocumentType>(
665 filter: FilterQuery<TRawDocType>,
666 projection: ProjectionType<TRawDocType> | null | undefined,
667 options: QueryOptions<TRawDocType> & { lean: true }
668 ): QueryWithHelpers<
669 GetLeanResultType<TRawDocType, TRawDocType[], 'find'>,
670 ResultDoc,
671 TQueryHelpers,
672 TRawDocType,
673 'find',
674 TInstanceMethods
675 >;
676 find<ResultDoc = THydratedDocumentType>(
677 filter: FilterQuery<TRawDocType>,
678 projection?: ProjectionType<TRawDocType> | null | undefined,
679 options?: QueryOptions<TRawDocType> | null | undefined
680 ): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'find', TInstanceMethods>;
681 find<ResultDoc = THydratedDocumentType>(
682 filter: FilterQuery<TRawDocType>,
683 projection?: ProjectionType<TRawDocType> | null | undefined
684 ): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'find', TInstanceMethods>;
685 find<ResultDoc = THydratedDocumentType>(
686 filter: FilterQuery<TRawDocType>
687 ): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'find', TInstanceMethods>;
688 find<ResultDoc = THydratedDocumentType>(
689 ): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'find', TInstanceMethods>;
690
691 /** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */
692 findByIdAndDelete<ResultDoc = THydratedDocumentType>(
693 id: mongodb.ObjectId | any,
694 options: QueryOptions<TRawDocType> & { lean: true }
695 ): QueryWithHelpers<
696 GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndDelete'> | null,
697 ResultDoc,
698 TQueryHelpers,
699 TRawDocType,
700 'findOneAndDelete',
701 TInstanceMethods
702 >;
703 findByIdAndDelete<ResultDoc = THydratedDocumentType>(
704 id: mongodb.ObjectId | any,
705 options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
706 ): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete', TInstanceMethods>;
707 findByIdAndDelete<ResultDoc = THydratedDocumentType>(
708 id?: mongodb.ObjectId | any,
709 options?: QueryOptions<TRawDocType> | null
710 ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete', TInstanceMethods>;
711
712 /** Creates a `findOneAndUpdate` query, filtering by the given `_id`. */
713 findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
714 filter: FilterQuery<TRawDocType>,
715 update: UpdateQuery<TRawDocType>,
716 options: QueryOptions<TRawDocType> & { includeResultMetadata: true, lean: true }
717 ): QueryWithHelpers<
718 ModifyResult<TRawDocType>,
719 ResultDoc,
720 TQueryHelpers,
721 TRawDocType,
722 'findOneAndUpdate',
723 TInstanceMethods
724 >;
725 findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
726 id: mongodb.ObjectId | any,
727 update: UpdateQuery<TRawDocType>,
728 options: QueryOptions<TRawDocType> & { lean: true }
729 ): QueryWithHelpers<
730 GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndUpdate'> | null,
731 ResultDoc,
732 TQueryHelpers,
733 TRawDocType,
734 'findOneAndUpdate',
735 TInstanceMethods
736 >;
737 findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
738 id: mongodb.ObjectId | any,
739 update: UpdateQuery<TRawDocType>,
740 options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
741 ): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate', TInstanceMethods>;
742 findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
743 id: mongodb.ObjectId | any,
744 update: UpdateQuery<TRawDocType>,
745 options: QueryOptions<TRawDocType> & { upsert: true } & ReturnsNewDoc
746 ): QueryWithHelpers<ResultDoc, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate', TInstanceMethods>;
747 findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
748 id?: mongodb.ObjectId | any,
749 update?: UpdateQuery<TRawDocType>,
750 options?: QueryOptions<TRawDocType> | null
751 ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate', TInstanceMethods>;
752 findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
753 id: mongodb.ObjectId | any,
754 update: UpdateQuery<TRawDocType>
755 ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate', TInstanceMethods>;
756
757 /** Creates a `findOneAndDelete` query: atomically finds the given document, deletes it, and returns the document as it was before deletion. */
758 findOneAndDelete<ResultDoc = THydratedDocumentType>(
759 filter: FilterQuery<TRawDocType>,
760 options: QueryOptions<TRawDocType> & { lean: true }
761 ): QueryWithHelpers<
762 GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndDelete'> | null,
763 ResultDoc,
764 TQueryHelpers,
765 TRawDocType,
766 'findOneAndDelete',
767 TInstanceMethods
768 >;
769 findOneAndDelete<ResultDoc = THydratedDocumentType>(
770 filter: FilterQuery<TRawDocType>,
771 options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
772 ): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete', TInstanceMethods>;
773 findOneAndDelete<ResultDoc = THydratedDocumentType>(
774 filter?: FilterQuery<TRawDocType> | null,
775 options?: QueryOptions<TRawDocType> | null
776 ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete', TInstanceMethods>;
777
778 /** Creates a `findOneAndReplace` query: atomically finds the given document and replaces it with `replacement`. */
779 findOneAndReplace<ResultDoc = THydratedDocumentType>(
780 filter: FilterQuery<TRawDocType>,
781 replacement: TRawDocType | AnyObject,
782 options: QueryOptions<TRawDocType> & { lean: true }
783 ): QueryWithHelpers<
784 GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndReplace'> | null,
785 ResultDoc,
786 TQueryHelpers,
787 TRawDocType,
788 'findOneAndReplace',
789 TInstanceMethods
790 >;
791 findOneAndReplace<ResultDoc = THydratedDocumentType>(
792 filter: FilterQuery<TRawDocType>,
793 replacement: TRawDocType | AnyObject,
794 options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
795 ): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace', TInstanceMethods>;
796 findOneAndReplace<ResultDoc = THydratedDocumentType>(
797 filter: FilterQuery<TRawDocType>,
798 replacement: TRawDocType | AnyObject,
799 options: QueryOptions<TRawDocType> & { upsert: true } & ReturnsNewDoc
800 ): QueryWithHelpers<ResultDoc, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace', TInstanceMethods>;
801 findOneAndReplace<ResultDoc = THydratedDocumentType>(
802 filter?: FilterQuery<TRawDocType>,
803 replacement?: TRawDocType | AnyObject,
804 options?: QueryOptions<TRawDocType> | null
805 ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace', TInstanceMethods>;
806
807 /** Creates a `findOneAndUpdate` query: atomically find the first document that matches `filter` and apply `update`. */
808 findOneAndUpdate<ResultDoc = THydratedDocumentType>(
809 filter: FilterQuery<TRawDocType>,
810 update: UpdateQuery<TRawDocType>,
811 options: QueryOptions<TRawDocType> & { includeResultMetadata: true, lean: true }
812 ): QueryWithHelpers<
813 ModifyResult<TRawDocType>,
814 ResultDoc,
815 TQueryHelpers,
816 TRawDocType,
817 'findOneAndUpdate',
818 TInstanceMethods
819 >;
820 findOneAndUpdate<ResultDoc = THydratedDocumentType>(
821 filter: FilterQuery<TRawDocType>,
822 update: UpdateQuery<TRawDocType>,
823 options: QueryOptions<TRawDocType> & { lean: true }
824 ): QueryWithHelpers<
825 GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndUpdate'> | null,
826 ResultDoc,
827 TQueryHelpers,
828 TRawDocType,
829 'findOneAndUpdate',
830 TInstanceMethods
831 >;
832 findOneAndUpdate<ResultDoc = THydratedDocumentType>(
833 filter: FilterQuery<TRawDocType>,
834 update: UpdateQuery<TRawDocType>,
835 options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
836 ): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate', TInstanceMethods>;
837 findOneAndUpdate<ResultDoc = THydratedDocumentType>(
838 filter: FilterQuery<TRawDocType>,
839 update: UpdateQuery<TRawDocType>,
840 options: QueryOptions<TRawDocType> & { upsert: true } & ReturnsNewDoc
841 ): QueryWithHelpers<ResultDoc, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate', TInstanceMethods>;
842 findOneAndUpdate<ResultDoc = THydratedDocumentType>(
843 filter?: FilterQuery<TRawDocType>,
844 update?: UpdateQuery<TRawDocType>,
845 options?: QueryOptions<TRawDocType> | null
846 ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate', TInstanceMethods>;
847
848 /** Creates a `replaceOne` query: finds the first document that matches `filter` and replaces it with `replacement`. */
849 replaceOne<ResultDoc = THydratedDocumentType>(
850 filter?: FilterQuery<TRawDocType>,
851 replacement?: TRawDocType | AnyObject,
852 options?: (mongodb.ReplaceOptions & MongooseQueryOptions<TRawDocType>) | null
853 ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'replaceOne', TInstanceMethods>;
854
855 /** Apply changes made to this model's schema after this model was compiled. */
856 recompileSchema(): void;
857
858 /** Schema the model uses. */
859 schema: Schema<TRawDocType>;
860
861 /** Creates a `updateMany` query: updates all documents that match `filter` with `update`. */
862 updateMany<ResultDoc = THydratedDocumentType>(
863 filter?: FilterQuery<TRawDocType>,
864 update?: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline,
865 options?: (mongodb.UpdateOptions & MongooseUpdateQueryOptions<TRawDocType>) | null
866 ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'updateMany', TInstanceMethods>;
867
868 /** Creates a `updateOne` query: updates the first document that matches `filter` with `update`. */
869 updateOne<ResultDoc = THydratedDocumentType>(
870 filter?: FilterQuery<TRawDocType>,
871 update?: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline,
872 options?: (mongodb.UpdateOptions & MongooseUpdateQueryOptions<TRawDocType>) | null
873 ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'updateOne', TInstanceMethods>;
874
875 /** Creates a Query, applies the passed conditions, and returns the Query. */
876 where<ResultDoc = THydratedDocumentType>(
877 path: string,
878 val?: any
879 ): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'find', TInstanceMethods>;
880 where<ResultDoc = THydratedDocumentType>(obj: object): QueryWithHelpers<
881 Array<ResultDoc>,
882 ResultDoc,
883 TQueryHelpers,
884 TRawDocType,
885 'find',
886 TInstanceMethods
887 >;
888 where<ResultDoc = THydratedDocumentType>(): QueryWithHelpers<
889 Array<ResultDoc>,
890 ResultDoc,
891 TQueryHelpers,
892 TRawDocType,
893 'find',
894 TInstanceMethods
895 >;
896 }
897}