mongoose 6.2.10 → 6.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/.eslintrc.json +157 -157
  2. package/dist/browser.umd.js +2 -1693
  3. package/lib/aggregate.js +23 -31
  4. package/lib/collection.js +0 -7
  5. package/lib/cursor/ChangeStream.js +42 -2
  6. package/lib/cursor/QueryCursor.js +2 -0
  7. package/lib/document.js +13 -19
  8. package/lib/error/cast.js +7 -2
  9. package/lib/error/eachAsyncMultiError.js +41 -0
  10. package/lib/helpers/cursor/eachAsync.js +44 -12
  11. package/lib/helpers/indexes/applySchemaCollation.js +13 -0
  12. package/lib/helpers/indexes/isTextIndex.js +16 -0
  13. package/lib/helpers/model/discriminator.js +1 -3
  14. package/lib/helpers/populate/getModelsMapForPopulate.js +13 -10
  15. package/lib/helpers/query/applyGlobalOption.js +29 -0
  16. package/lib/helpers/query/castUpdate.js +3 -1
  17. package/lib/helpers/schematype/handleImmutable.js +4 -1
  18. package/lib/helpers/timestamps/setupTimestamps.js +2 -2
  19. package/lib/helpers/update/applyTimestampsToChildren.js +2 -2
  20. package/lib/helpers/update/applyTimestampsToUpdate.js +0 -1
  21. package/lib/index.js +11 -4
  22. package/lib/model.js +36 -36
  23. package/lib/query.js +59 -26
  24. package/lib/queryhelpers.js +11 -1
  25. package/lib/schema/SubdocumentPath.js +2 -1
  26. package/lib/schema/documentarray.js +2 -4
  27. package/lib/schema/objectid.js +0 -3
  28. package/lib/schema/string.js +24 -2
  29. package/lib/schema.js +117 -3
  30. package/lib/schematype.js +2 -3
  31. package/lib/types/DocumentArray/methods/index.js +1 -1
  32. package/lib/types/array/methods/index.js +9 -10
  33. package/lib/types/subdocument.js +29 -0
  34. package/lib/validoptions.js +1 -0
  35. package/package.json +13 -7
  36. package/tools/repl.js +2 -1
  37. package/tsconfig.json +2 -1
  38. package/types/aggregate.d.ts +223 -0
  39. package/types/connection.d.ts +2 -2
  40. package/types/cursor.d.ts +10 -4
  41. package/types/document.d.ts +3 -3
  42. package/types/index.d.ts +200 -215
  43. package/types/mongooseoptions.d.ts +10 -4
  44. package/CHANGELOG.md +0 -7227
  45. package/History.md +0 -1
  46. package/lib/helpers/query/applyGlobalMaxTimeMS.js +0 -15
package/types/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference path="./aggregate.d.ts" />
1
2
  /// <reference path="./connection.d.ts" />
2
3
  /// <reference path="./cursor.d.ts" />
3
4
  /// <reference path="./document.d.ts" />
@@ -182,7 +183,7 @@ declare module 'mongoose' {
182
183
  * section collection.js
183
184
  * http://mongoosejs.com/docs/api.html#collection-js
184
185
  */
185
- interface CollectionBase<T> extends mongodb.Collection<T> {
186
+ interface CollectionBase<T extends mongodb.Document> extends mongodb.Collection<T> {
186
187
  /*
187
188
  * Abstract methods. Some of these are already defined on the
188
189
  * mongodb.Collection interface so they've been commented out.
@@ -204,7 +205,7 @@ declare module 'mongoose' {
204
205
  * http://mongoosejs.com/docs/api.html#drivers-node-mongodb-native-collection-js
205
206
  */
206
207
  let Collection: Collection;
207
- interface Collection<T = AnyObject> extends CollectionBase<T> {
208
+ interface Collection<T extends mongodb.Document = mongodb.Document> extends CollectionBase<T> {
208
209
  /**
209
210
  * Collection constructor
210
211
  * @param name name of the collection
@@ -298,7 +299,7 @@ declare module 'mongoose' {
298
299
 
299
300
  /** Creates a `countDocuments` query: counts the number of documents that match `filter`. */
300
301
  countDocuments(callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
301
- countDocuments(filter: FilterQuery<T>, options?: QueryOptions, callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
302
+ countDocuments(filter: FilterQuery<T>, options?: QueryOptions<T>, callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
302
303
 
303
304
  /** Creates a new document or documents */
304
305
  create(docs: (AnyKeys<T> | AnyObject)[], options?: SaveOptions): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>[]>;
@@ -316,8 +317,8 @@ declare module 'mongoose' {
316
317
  * mongoose will not create the collection for the model until any documents are
317
318
  * created. Use this method to create the collection explicitly.
318
319
  */
319
- createCollection<T>(options?: mongodb.CreateCollectionOptions & Pick<SchemaOptions, 'expires'>): Promise<mongodb.Collection<T>>;
320
- createCollection<T>(options: mongodb.CreateCollectionOptions & Pick<SchemaOptions, 'expires'> | null, callback: Callback<mongodb.Collection<T>>): void;
320
+ createCollection<T extends mongodb.Document>(options?: mongodb.CreateCollectionOptions & Pick<SchemaOptions, 'expires'>): Promise<mongodb.Collection<T>>;
321
+ createCollection<T extends mongodb.Document>(options: mongodb.CreateCollectionOptions & Pick<SchemaOptions, 'expires'> | null, callback: Callback<mongodb.Collection<T>>): void;
321
322
 
322
323
  /**
323
324
  * Similar to `ensureIndexes()`, except for it uses the [`createIndex`](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#createIndex)
@@ -334,7 +335,7 @@ declare module 'mongoose' {
334
335
  * Behaves like `remove()`, but deletes all documents that match `conditions`
335
336
  * regardless of the `single` option.
336
337
  */
337
- deleteMany(filter?: FilterQuery<T>, options?: QueryOptions, callback?: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
338
+ deleteMany(filter?: FilterQuery<T>, options?: QueryOptions<T>, callback?: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
338
339
  deleteMany(filter: FilterQuery<T>, callback: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
339
340
  deleteMany(callback: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
340
341
 
@@ -343,7 +344,7 @@ declare module 'mongoose' {
343
344
  * Behaves like `remove()`, but deletes at most one document regardless of the
344
345
  * `single` option.
345
346
  */
346
- deleteOne(filter?: FilterQuery<T>, options?: QueryOptions, callback?: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
347
+ deleteOne(filter?: FilterQuery<T>, options?: QueryOptions<T>, callback?: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
347
348
  deleteOne(filter: FilterQuery<T>, callback: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
348
349
  deleteOne(callback: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
349
350
 
@@ -365,10 +366,34 @@ declare module 'mongoose' {
365
366
  * equivalent to `findOne({ _id: id })`. If you want to query by a document's
366
367
  * `_id`, use `findById()` instead of `findOne()`.
367
368
  */
368
- findById<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id: any, projection?: any | null, options?: QueryOptions | null, callback?: Callback<ResultDoc | null>): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
369
+ findById<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
370
+ id: any,
371
+ projection?: ProjectionType<T> | null,
372
+ options?: QueryOptions<T> | null,
373
+ callback?: Callback<ResultDoc | null>
374
+ ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
375
+ findById<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
376
+ id: any,
377
+ projection?: ProjectionType<T> | null,
378
+ callback?: Callback<ResultDoc | null>
379
+ ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
369
380
 
370
381
  /** Finds one document. */
371
- findOne<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, projection?: any | null, options?: QueryOptions | null, callback?: Callback<ResultDoc | null>): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
382
+ findOne<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
383
+ filter?: FilterQuery<T>,
384
+ projection?: ProjectionType<T> | null,
385
+ options?: QueryOptions<T> | null,
386
+ callback?: Callback<ResultDoc | null>
387
+ ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
388
+ findOne<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
389
+ filter?: FilterQuery<T>,
390
+ projection?: ProjectionType<T> | null,
391
+ callback?: Callback<ResultDoc | null>
392
+ ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
393
+ findOne<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
394
+ filter?: FilterQuery<T>,
395
+ callback?: Callback<ResultDoc | null>
396
+ ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
372
397
 
373
398
  /**
374
399
  * Shortcut for creating a new Document from existing raw data, pre-saved in the DB.
@@ -442,7 +467,7 @@ declare module 'mongoose' {
442
467
  validate(optional: any, pathsToValidate: string[], callback?: CallbackWithoutResult): Promise<void>;
443
468
 
444
469
  /** Watches the underlying collection for changes using [MongoDB change streams](https://docs.mongodb.com/manual/changeStreams/). */
445
- watch<ResultType = any>(pipeline?: Array<Record<string, unknown>>, options?: mongodb.ChangeStreamOptions): mongodb.ChangeStream<ResultType>;
470
+ watch<ResultType extends mongodb.Document = any>(pipeline?: Array<Record<string, unknown>>, options?: mongodb.ChangeStreamOptions): mongodb.ChangeStream<ResultType>;
446
471
 
447
472
  /** Adds a `$where` clause to this query */
448
473
  $where(argument: string | Function): QueryWithHelpers<Array<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
@@ -457,7 +482,7 @@ declare module 'mongoose' {
457
482
  distinct<ReturnType = any>(field: string, filter?: FilterQuery<T>, callback?: Callback<number>): QueryWithHelpers<Array<ReturnType>, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
458
483
 
459
484
  /** Creates a `estimatedDocumentCount` query: counts the number of documents in the collection. */
460
- estimatedDocumentCount(options?: QueryOptions, callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
485
+ estimatedDocumentCount(options?: QueryOptions<T>, callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
461
486
 
462
487
  /**
463
488
  * Returns a document with its `_id` if at least one document exists in the database that matches
@@ -468,37 +493,61 @@ declare module 'mongoose' {
468
493
 
469
494
  /** Creates a `find` query: gets a list of documents that match `filter`. */
470
495
  find<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(callback?: Callback<ResultDoc[]>): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
496
+ find<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
497
+ filter: FilterQuery<T>,
498
+ projection?: ProjectionType<T> | null,
499
+ callback?: Callback<ResultDoc[]>
500
+ ): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
471
501
  find<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter: FilterQuery<T>, callback?: Callback<ResultDoc[]>): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
472
- find<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter: FilterQuery<T>, projection?: any | null, options?: QueryOptions | null, callback?: Callback<ResultDoc[]>): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
502
+ find<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter: FilterQuery<T>, projection?: ProjectionType<T> | null, options?: QueryOptions<T> | null, callback?: Callback<ResultDoc[]>): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
473
503
 
474
504
  /** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */
475
- findByIdAndDelete<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
505
+ findByIdAndDelete<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id?: mongodb.ObjectId | any, options?: QueryOptions<T> | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
476
506
 
477
507
  /** Creates a `findByIdAndRemove` query, filtering by the given `_id`. */
478
- findByIdAndRemove<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
508
+ findByIdAndRemove<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id?: mongodb.ObjectId | any, options?: QueryOptions<T> | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
479
509
 
480
510
  /** Creates a `findOneAndUpdate` query, filtering by the given `_id`. */
481
- findByIdAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id: mongodb.ObjectId | any, update: UpdateQuery<T>, options: QueryOptions & { rawResult: true }, callback?: (err: CallbackError, doc: any, res: any) => void): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, T>;
482
- findByIdAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id: mongodb.ObjectId | any, update: UpdateQuery<T>, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: ResultDoc, res: any) => void): QueryWithHelpers<ResultDoc, ResultDoc, TQueryHelpers, T>;
483
- findByIdAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id?: mongodb.ObjectId | any, update?: UpdateQuery<T>, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
511
+ findByIdAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id: mongodb.ObjectId | any, update: UpdateQuery<T>, options: QueryOptions<T> & { rawResult: true }, callback?: (err: CallbackError, doc: any, res: any) => void): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, T>;
512
+ findByIdAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id: mongodb.ObjectId | any, update: UpdateQuery<T>, options: QueryOptions<T> & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: ResultDoc, res: any) => void): QueryWithHelpers<ResultDoc, ResultDoc, TQueryHelpers, T>;
513
+ findByIdAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id?: mongodb.ObjectId | any, update?: UpdateQuery<T>, options?: QueryOptions<T> | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
484
514
  findByIdAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id: mongodb.ObjectId | any, update: UpdateQuery<T>, callback: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
485
515
 
486
516
  /** Creates a `findOneAndDelete` query: atomically finds the given document, deletes it, and returns the document as it was before deletion. */
487
- findOneAndDelete<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
517
+ findOneAndDelete<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, options?: QueryOptions<T> | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
488
518
 
489
519
  /** Creates a `findOneAndRemove` query: atomically finds the given document and deletes it. */
490
- findOneAndRemove<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
520
+ findOneAndRemove<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, options?: QueryOptions<T> | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
491
521
 
492
522
  /** Creates a `findOneAndReplace` query: atomically finds the given document and replaces it with `replacement`. */
493
- findOneAndReplace<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter: FilterQuery<T>, replacement: T | AnyObject, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: ResultDoc, res: any) => void): QueryWithHelpers<ResultDoc, ResultDoc, TQueryHelpers, T>;
494
- findOneAndReplace<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, replacement?: T | AnyObject, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
523
+ findOneAndReplace<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter: FilterQuery<T>, replacement: T | AnyObject, options: QueryOptions<T> & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: ResultDoc, res: any) => void): QueryWithHelpers<ResultDoc, ResultDoc, TQueryHelpers, T>;
524
+ findOneAndReplace<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, replacement?: T | AnyObject, options?: QueryOptions<T> | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
495
525
 
496
526
  /** Creates a `findOneAndUpdate` query: atomically find the first document that matches `filter` and apply `update`. */
497
- findOneAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter: FilterQuery<T>, update: UpdateQuery<T>, options: QueryOptions & { rawResult: true }, callback?: (err: CallbackError, doc: any, res: any) => void): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, T>;
498
- findOneAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter: FilterQuery<T>, update: UpdateQuery<T>, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: ResultDoc, res: any) => void): QueryWithHelpers<ResultDoc, ResultDoc, TQueryHelpers, T>;
499
- findOneAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, update?: UpdateQuery<T>, options?: QueryOptions | null, callback?: (err: CallbackError, doc: T | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
500
-
501
- geoSearch<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, options?: GeoSearchOptions, callback?: Callback<Array<ResultDoc>>): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
527
+ findOneAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
528
+ filter: FilterQuery<T>,
529
+ update: UpdateQuery<T>,
530
+ options: QueryOptions<T> & { rawResult: true },
531
+ callback?: (err: CallbackError, doc: any, res: any) => void
532
+ ): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, T>;
533
+ findOneAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
534
+ filter: FilterQuery<T>,
535
+ update: UpdateQuery<T>,
536
+ options: QueryOptions<T> & { upsert: true } & ReturnsNewDoc,
537
+ callback?: (err: CallbackError, doc: ResultDoc, res: any) => void
538
+ ): QueryWithHelpers<ResultDoc, ResultDoc, TQueryHelpers, T>;
539
+ findOneAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
540
+ filter?: FilterQuery<T>,
541
+ update?: UpdateQuery<T>,
542
+ options?: QueryOptions<T> | null,
543
+ callback?: (err: CallbackError, doc: T | null, res: any) => void
544
+ ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
545
+
546
+ geoSearch<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
547
+ filter?: FilterQuery<T>,
548
+ options?: GeoSearchOptions,
549
+ callback?: Callback<Array<ResultDoc>>
550
+ ): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
502
551
 
503
552
  /** Executes a mapReduce command. */
504
553
  mapReduce<Key, Value>(
@@ -509,8 +558,18 @@ declare module 'mongoose' {
509
558
  remove<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: any, callback?: CallbackWithoutResult): QueryWithHelpers<any, ResultDoc, TQueryHelpers, T>;
510
559
 
511
560
  /** Creates a `replaceOne` query: finds the first document that matches `filter` and replaces it with `replacement`. */
512
- replaceOne<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, replacement?: T | AnyObject, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<any, ResultDoc, TQueryHelpers, T>;
513
- replaceOne<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, replacement?: T | AnyObject, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<any, ResultDoc, TQueryHelpers, T>;
561
+ replaceOne<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
562
+ filter?: FilterQuery<T>,
563
+ replacement?: T | AnyObject,
564
+ options?: QueryOptions<T> | null,
565
+ callback?: Callback
566
+ ): QueryWithHelpers<any, ResultDoc, TQueryHelpers, T>;
567
+ replaceOne<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
568
+ filter?: FilterQuery<T>,
569
+ replacement?: T | AnyObject,
570
+ options?: QueryOptions<T> | null,
571
+ callback?: Callback
572
+ ): QueryWithHelpers<any, ResultDoc, TQueryHelpers, T>;
514
573
 
515
574
  /** Schema the model uses. */
516
575
  schema: Schema<T>;
@@ -519,13 +578,28 @@ declare module 'mongoose' {
519
578
  * @deprecated use `updateOne` or `updateMany` instead.
520
579
  * Creates a `update` query: updates one or many documents that match `filter` with `update`, based on the `multi` option.
521
580
  */
522
- update<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, update?: UpdateQuery<T> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, T>;
581
+ update<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
582
+ filter?: FilterQuery<T>,
583
+ update?: UpdateQuery<T> | UpdateWithAggregationPipeline,
584
+ options?: QueryOptions<T> | null,
585
+ callback?: Callback
586
+ ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, T>;
523
587
 
524
588
  /** Creates a `updateMany` query: updates all documents that match `filter` with `update`. */
525
- updateMany<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, update?: UpdateQuery<T> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, T>;
589
+ updateMany<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
590
+ filter?: FilterQuery<T>,
591
+ update?: UpdateQuery<T> | UpdateWithAggregationPipeline,
592
+ options?: QueryOptions<T> | null,
593
+ callback?: Callback
594
+ ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, T>;
526
595
 
527
596
  /** Creates a `updateOne` query: updates the first document that matches `filter` with `update`. */
528
- updateOne<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, update?: UpdateQuery<T> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, T>;
597
+ updateOne<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
598
+ filter?: FilterQuery<T>,
599
+ update?: UpdateQuery<T> | UpdateWithAggregationPipeline,
600
+ options?: QueryOptions<T> | null,
601
+ callback?: Callback
602
+ ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, T>;
529
603
 
530
604
  /** Creates a Query, applies the passed conditions, and returns the Query. */
531
605
  where<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(path: string, val?: any): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
@@ -535,7 +609,7 @@ declare module 'mongoose' {
535
609
 
536
610
  type UpdateWriteOpResult = mongodb.UpdateResult;
537
611
 
538
- interface QueryOptions {
612
+ interface QueryOptions<DocType = unknown> {
539
613
  arrayFilters?: { [key: string]: any }[];
540
614
  batchSize?: number;
541
615
  collation?: mongodb.CollationOptions;
@@ -562,7 +636,7 @@ declare module 'mongoose' {
562
636
  overwrite?: boolean;
563
637
  overwriteDiscriminatorKey?: boolean;
564
638
  populate?: string | string[] | PopulateOptions | PopulateOptions[];
565
- projection?: any;
639
+ projection?: ProjectionType<DocType>;
566
640
  /**
567
641
  * if true, returns the raw result from the MongoDB driver
568
642
  */
@@ -610,7 +684,7 @@ declare module 'mongoose' {
610
684
  [other: string]: any;
611
685
  }
612
686
 
613
- type MongooseQueryOptions = Pick<QueryOptions, 'populate' | 'lean' | 'strict' | 'sanitizeProjection' | 'sanitizeFilter'>;
687
+ type MongooseQueryOptions<DocType = unknown> = Pick<QueryOptions<DocType>, 'populate' | 'lean' | 'strict' | 'sanitizeProjection' | 'sanitizeFilter'>;
614
688
 
615
689
  interface SaveOptions {
616
690
  checkKeys?: boolean;
@@ -715,6 +789,8 @@ declare module 'mongoose' {
715
789
  model?: string | Model<any>;
716
790
  /** optional query options like sort, limit, etc */
717
791
  options?: any;
792
+ /** correct limit on populated array */
793
+ perDocumentLimit?: number;
718
794
  /** optional boolean, set to `false` to allow populating paths that aren't in the schema */
719
795
  strictPopulate?: boolean;
720
796
  /** deep populate */
@@ -779,6 +855,9 @@ declare module 'mongoose' {
779
855
  */
780
856
  childSchemas: { schema: Schema, model: any }[];
781
857
 
858
+ /** Removes all indexes on this schema */
859
+ clearIndexes(): this;
860
+
782
861
  /** Returns a copy of this schema */
783
862
  clone<T = this>(): T;
784
863
 
@@ -878,6 +957,9 @@ declare module 'mongoose' {
878
957
  /** Removes the given `path` (or [`paths`]). */
879
958
  remove(paths: string | Array<string>): this;
880
959
 
960
+ /** Removes index by name or index spec */
961
+ remove(index: string | AnyObject): this;
962
+
881
963
  /** Returns an Array of path strings that are required by this schema. */
882
964
  requiredPaths(invalidate?: boolean): string[];
883
965
 
@@ -892,13 +974,16 @@ declare module 'mongoose' {
892
974
  statics: { [name: string]: (this: M, ...args: any[]) => any };
893
975
 
894
976
  /** Creates a virtual type with the given name. */
895
- virtual<T = HydratedDocument<DocType, TInstanceMethods>>(name: string, options?: VirtualTypeOptions<T>): VirtualType;
977
+ virtual<T = HydratedDocument<DocType, TInstanceMethods>>(
978
+ name: string,
979
+ options?: VirtualTypeOptions<T, DocType>
980
+ ): VirtualType<T>;
896
981
 
897
982
  /** Object of currently defined virtuals on this schema */
898
983
  virtuals: any;
899
984
 
900
985
  /** Returns the virtual type with the given `name`. */
901
- virtualpath(name: string): VirtualType | null;
986
+ virtualpath<T = HydratedDocument<DocType, TInstanceMethods>>(name: string): VirtualType<T> | null;
902
987
  }
903
988
 
904
989
  type NumberSchemaDefinition = typeof Number | 'number' | 'Number' | typeof Schema.Types.Number;
@@ -926,7 +1011,8 @@ declare module 'mongoose' {
926
1011
  Function[] |
927
1012
  SchemaDefinition<T> |
928
1013
  SchemaDefinition<Unpacked<T>>[] |
929
- typeof SchemaTypes.Mixed;
1014
+ typeof Schema.Types.Mixed |
1015
+ MixedSchemaTypeOptions;
930
1016
 
931
1017
  type SchemaDefinition<T = undefined> = T extends undefined
932
1018
  ? { [path: string]: SchemaDefinitionProperty; }
@@ -941,6 +1027,10 @@ declare module 'mongoose' {
941
1027
 
942
1028
  type ExtractMongooseArray<T> = T extends Types.Array<any> ? AnyArray<Unpacked<T>> : T;
943
1029
 
1030
+ class MixedSchemaTypeOptions extends SchemaTypeOptions<Schema.Types.Mixed> {
1031
+ type: typeof Schema.Types.Mixed;
1032
+ }
1033
+
944
1034
  export class SchemaTypeOptions<T> {
945
1035
  type?:
946
1036
  T extends string ? StringSchemaDefinition :
@@ -978,7 +1068,7 @@ declare module 'mongoose' {
978
1068
  * The default value for this path. If a function, Mongoose executes the function
979
1069
  * and uses the return value as the default.
980
1070
  */
981
- default?: ExtractMongooseArray<T> | ((this: any, doc: any) => Partial<ExtractMongooseArray<T>>);
1071
+ default?: T extends Schema.Types.Mixed ? ({} | ((this: any, doc: any) => any)) : (ExtractMongooseArray<T> | ((this: any, doc: any) => Partial<ExtractMongooseArray<T>>));
982
1072
 
983
1073
  /**
984
1074
  * The model that `populate()` should use if populating this path.
@@ -1158,7 +1248,7 @@ declare module 'mongoose' {
1158
1248
 
1159
1249
  type InferId<T> = T extends { _id?: any } ? T['_id'] : Types.ObjectId;
1160
1250
 
1161
- interface VirtualTypeOptions<HydratedDocType = Document> {
1251
+ interface VirtualTypeOptions<HydratedDocType = Document, DocType = unknown> {
1162
1252
  /** If `ref` is not nullish, this becomes a populated virtual. */
1163
1253
  ref?: string | Function;
1164
1254
 
@@ -1202,13 +1292,13 @@ declare module 'mongoose' {
1202
1292
  perDocumentLimit?: number;
1203
1293
 
1204
1294
  /** Additional options like `limit` and `lean`. */
1205
- options?: QueryOptions & { match?: AnyObject };
1295
+ options?: QueryOptions<DocType> & { match?: AnyObject };
1206
1296
 
1207
1297
  /** Additional options for plugins */
1208
1298
  [extra: string]: any;
1209
1299
  }
1210
1300
 
1211
- class VirtualType {
1301
+ class VirtualType<HydratedDocType> {
1212
1302
  /** Applies getters to `value`. */
1213
1303
  applyGetters(value: any, doc: Document): any;
1214
1304
 
@@ -1216,10 +1306,10 @@ declare module 'mongoose' {
1216
1306
  applySetters(value: any, doc: Document): any;
1217
1307
 
1218
1308
  /** Adds a custom getter to this virtual. */
1219
- get(fn: Function): this;
1309
+ get<T = HydratedDocType>(fn: (this: T, value: any, virtualType: VirtualType<T>, doc: T) => any): this;
1220
1310
 
1221
1311
  /** Adds a custom setter to this virtual. */
1222
- set(fn: Function): this;
1312
+ set<T = HydratedDocType>(fn: (this: T, value: any, virtualType: VirtualType<T>, doc: T) => void): this;
1223
1313
  }
1224
1314
 
1225
1315
  namespace Schema {
@@ -1482,7 +1572,7 @@ declare module 'mongoose' {
1482
1572
  type ProjectionFields<DocType> = { [Key in keyof Omit<LeanDocument<DocType>, '__v'>]?: any } & Record<string, any>;
1483
1573
 
1484
1574
  class Query<ResultType, DocType, THelpers = {}, RawDocType = DocType> {
1485
- _mongooseOptions: MongooseQueryOptions;
1575
+ _mongooseOptions: MongooseQueryOptions<DocType>;
1486
1576
 
1487
1577
  /**
1488
1578
  * Returns a wrapper around a [mongodb driver cursor](http://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html).
@@ -1551,20 +1641,24 @@ declare module 'mongoose' {
1551
1641
 
1552
1642
  /** Specifies this query as a `countDocuments` query. */
1553
1643
  countDocuments(callback?: Callback<number>): QueryWithHelpers<number, DocType, THelpers, RawDocType>;
1554
- countDocuments(criteria: FilterQuery<DocType>, options?: QueryOptions, callback?: Callback<number>): QueryWithHelpers<number, DocType, THelpers, RawDocType>;
1644
+ countDocuments(
1645
+ criteria: FilterQuery<DocType>,
1646
+ options?: QueryOptions<DocType>,
1647
+ callback?: Callback<number>
1648
+ ): QueryWithHelpers<number, DocType, THelpers, RawDocType>;
1555
1649
 
1556
1650
  /**
1557
1651
  * Returns a wrapper around a [mongodb driver cursor](http://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html).
1558
1652
  * A QueryCursor exposes a Streams3 interface, as well as a `.next()` function.
1559
1653
  */
1560
- cursor(options?: QueryOptions): Cursor<DocType, QueryOptions>;
1654
+ cursor(options?: QueryOptions<DocType>): Cursor<DocType, QueryOptions<DocType>>;
1561
1655
 
1562
1656
  /**
1563
1657
  * Declare and/or execute this query as a `deleteMany()` operation. Works like
1564
1658
  * remove, except it deletes _every_ document that matches `filter` in the
1565
1659
  * collection, regardless of the value of `single`.
1566
1660
  */
1567
- deleteMany(filter?: FilterQuery<DocType>, options?: QueryOptions, callback?: Callback): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
1661
+ deleteMany(filter?: FilterQuery<DocType>, options?: QueryOptions<DocType>, callback?: Callback): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
1568
1662
  deleteMany(filter: FilterQuery<DocType>, callback: Callback): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
1569
1663
  deleteMany(callback: Callback): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
1570
1664
 
@@ -1573,7 +1667,7 @@ declare module 'mongoose' {
1573
1667
  * remove, except it deletes at most one document regardless of the `single`
1574
1668
  * option.
1575
1669
  */
1576
- deleteOne(filter?: FilterQuery<DocType>, options?: QueryOptions, callback?: Callback): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
1670
+ deleteOne(filter?: FilterQuery<DocType>, options?: QueryOptions<DocType>, callback?: Callback): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
1577
1671
  deleteOne(filter: FilterQuery<DocType>, callback: Callback): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
1578
1672
  deleteOne(callback: Callback): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
1579
1673
 
@@ -1595,7 +1689,7 @@ declare module 'mongoose' {
1595
1689
  equals(val: any): this;
1596
1690
 
1597
1691
  /** Creates a `estimatedDocumentCount` query: counts the number of documents in the collection. */
1598
- estimatedDocumentCount(options?: QueryOptions, callback?: Callback<number>): QueryWithHelpers<number, DocType, THelpers, RawDocType>;
1692
+ estimatedDocumentCount(options?: QueryOptions<DocType>, callback?: Callback<number>): QueryWithHelpers<number, DocType, THelpers, RawDocType>;
1599
1693
 
1600
1694
  /** Specifies a `$exists` query condition. When called with one argument, the most recent path passed to `where()` is used. */
1601
1695
  exists(val: boolean): this;
@@ -1611,45 +1705,80 @@ declare module 'mongoose' {
1611
1705
 
1612
1706
  /** Creates a `find` query: gets a list of documents that match `filter`. */
1613
1707
  find(callback?: Callback<DocType[]>): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType>;
1614
- find(filter: FilterQuery<DocType>, callback?: Callback<DocType[]>): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType>;
1615
- find(filter: FilterQuery<DocType>, projection?: any | null, options?: QueryOptions | null, callback?: Callback<DocType[]>): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType>;
1708
+ find(
1709
+ filter: FilterQuery<DocType>,
1710
+ callback?: Callback<DocType[]>
1711
+ ): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType>;
1712
+ find(
1713
+ filter: FilterQuery<DocType>,
1714
+ projection?: ProjectionType<DocType> | null,
1715
+ callback?: Callback<DocType[]>
1716
+ ): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType>;
1717
+ find(
1718
+ filter: FilterQuery<DocType>,
1719
+ projection?: ProjectionType<DocType> | null,
1720
+ options?: QueryOptions<DocType> | null,
1721
+ callback?: Callback<DocType[]>
1722
+ ): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType>;
1616
1723
 
1617
1724
  /** Declares the query a findOne operation. When executed, the first found document is passed to the callback. */
1618
- findOne(filter?: FilterQuery<DocType>, projection?: any | null, options?: QueryOptions | null, callback?: Callback<DocType | null>): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
1725
+ findOne(
1726
+ filter?: FilterQuery<DocType>,
1727
+ projection?: ProjectionType<DocType> | null,
1728
+ options?: QueryOptions<DocType> | null,
1729
+ callback?: Callback<DocType | null>
1730
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
1731
+ findOne(
1732
+ filter?: FilterQuery<DocType>,
1733
+ projection?: ProjectionType<DocType> | null,
1734
+ callback?: Callback<DocType | null>
1735
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
1736
+ findOne(
1737
+ filter?: FilterQuery<DocType>,
1738
+ callback?: Callback<DocType | null>
1739
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
1619
1740
 
1620
1741
  /** Creates a `findOneAndDelete` query: atomically finds the given document, deletes it, and returns the document as it was before deletion. */
1621
- findOneAndDelete(filter?: FilterQuery<DocType>, options?: QueryOptions | null, callback?: (err: CallbackError, doc: DocType | null, res: any) => void): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
1742
+ findOneAndDelete(
1743
+ filter?: FilterQuery<DocType>,
1744
+ options?: QueryOptions<DocType> | null,
1745
+ callback?: (err: CallbackError, doc: DocType | null, res: any) => void
1746
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
1622
1747
 
1623
1748
  /** Creates a `findOneAndRemove` query: atomically finds the given document and deletes it. */
1624
- findOneAndRemove(filter?: FilterQuery<DocType>, options?: QueryOptions | null, callback?: (err: CallbackError, doc: DocType | null, res: any) => void): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
1749
+ findOneAndRemove(
1750
+ filter?: FilterQuery<DocType>,
1751
+ options?: QueryOptions<DocType> | null,
1752
+ callback?: (err: CallbackError, doc: DocType | null, res: any) => void
1753
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
1625
1754
 
1626
1755
  /** Creates a `findOneAndUpdate` query: atomically find the first document that matches `filter` and apply `update`. */
1627
1756
  findOneAndUpdate(
1628
1757
  filter: FilterQuery<DocType>,
1629
1758
  update: UpdateQuery<DocType>,
1630
- options: QueryOptions & { rawResult: true },
1759
+ options: QueryOptions<DocType> & { rawResult: true },
1631
1760
  callback?: (err: CallbackError, doc: DocType | null, res: ModifyResult<DocType>) => void
1632
1761
  ): QueryWithHelpers<ModifyResult<DocType>, DocType, THelpers, RawDocType>;
1633
1762
  findOneAndUpdate(
1634
1763
  filter: FilterQuery<DocType>,
1635
1764
  update: UpdateQuery<DocType>,
1636
- options: QueryOptions & { upsert: true } & ReturnsNewDoc,
1765
+ options: QueryOptions<DocType> & { upsert: true } & ReturnsNewDoc,
1637
1766
  callback?: (err: CallbackError, doc: DocType, res: ModifyResult<DocType>) => void
1638
1767
  ): QueryWithHelpers<DocType, DocType, THelpers, RawDocType>;
1639
1768
  findOneAndUpdate(
1640
1769
  filter?: FilterQuery<DocType>,
1641
1770
  update?: UpdateQuery<DocType>,
1642
- options?: QueryOptions | null,
1771
+ options?: QueryOptions<DocType> | null,
1643
1772
  callback?: (err: CallbackError, doc: DocType | null, res: ModifyResult<DocType>) => void
1644
1773
  ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
1645
1774
 
1646
1775
  /** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */
1647
- findByIdAndDelete(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: CallbackError, doc: DocType | null, res: any) => void): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
1776
+ findByIdAndDelete(id?: mongodb.ObjectId | any, options?: QueryOptions<DocType> | null, callback?: (err: CallbackError, doc: DocType | null, res: any) => void): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
1648
1777
 
1649
1778
  /** Creates a `findOneAndUpdate` query, filtering by the given `_id`. */
1650
- findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<DocType>, options: QueryOptions & { rawResult: true }, callback?: (err: CallbackError, doc: any, res?: any) => void): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
1651
- findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<DocType>, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: DocType, res?: any) => void): QueryWithHelpers<DocType, DocType, THelpers, RawDocType>;
1652
- findByIdAndUpdate(id?: mongodb.ObjectId | any, update?: UpdateQuery<DocType>, options?: QueryOptions | null, callback?: (CallbackError: any, doc: DocType | null, res?: any) => void): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
1779
+ findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<DocType>, options: QueryOptions<DocType> & { rawResult: true }, callback?: (err: CallbackError, doc: any, res?: any) => void): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
1780
+ findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<DocType>, options: QueryOptions<DocType> & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: DocType, res?: any) => void): QueryWithHelpers<DocType, DocType, THelpers, RawDocType>;
1781
+ findByIdAndUpdate(id?: mongodb.ObjectId | any, update?: UpdateQuery<DocType>, options?: QueryOptions<DocType> | null, callback?: (CallbackError: any, doc: DocType | null, res?: any) => void): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
1653
1782
  findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<DocType>, callback: (CallbackError: any, doc: DocType | null, res?: any) => void): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
1654
1783
 
1655
1784
  /** Specifies a `$geometry` condition */
@@ -1666,7 +1795,7 @@ declare module 'mongoose' {
1666
1795
  getFilter(): FilterQuery<DocType>;
1667
1796
 
1668
1797
  /** Gets query options. */
1669
- getOptions(): QueryOptions;
1798
+ getOptions(): QueryOptions<DocType>;
1670
1799
 
1671
1800
  /** Gets a list of paths to be populated by this query */
1672
1801
  getPopulatedPaths(): Array<string>;
@@ -1808,8 +1937,8 @@ declare module 'mongoose' {
1808
1937
  * `update()`, except MongoDB will replace the existing document and will
1809
1938
  * not accept any [atomic](https://docs.mongodb.com/manual/tutorial/model-data-for-atomic-operations/#pattern) operators (`$set`, etc.)
1810
1939
  */
1811
- replaceOne(filter?: FilterQuery<DocType>, replacement?: DocType | AnyObject, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
1812
- replaceOne(filter?: FilterQuery<DocType>, replacement?: DocType | AnyObject, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
1940
+ replaceOne(filter?: FilterQuery<DocType>, replacement?: DocType | AnyObject, options?: QueryOptions<DocType> | null, callback?: Callback): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
1941
+ replaceOne(filter?: FilterQuery<DocType>, replacement?: DocType | AnyObject, options?: QueryOptions<DocType> | null, callback?: Callback): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
1813
1942
 
1814
1943
  /** Specifies which document fields to include or exclude (also known as the query "projection") */
1815
1944
  select(arg: string | any): this;
@@ -1838,7 +1967,7 @@ declare module 'mongoose' {
1838
1967
  set(path: string | Record<string, unknown>, value?: any): this;
1839
1968
 
1840
1969
  /** Sets query options. Some options only make sense for certain operations. */
1841
- setOptions(options: QueryOptions, overwrite?: boolean): this;
1970
+ setOptions(options: QueryOptions<DocType>, overwrite?: boolean): this;
1842
1971
 
1843
1972
  /** Sets the query conditions to the provided JSON object. */
1844
1973
  setQuery(val: FilterQuery<DocType> | null): void;
@@ -1878,7 +2007,7 @@ declare module 'mongoose' {
1878
2007
  toConstructor(): new (...args: any[]) => QueryWithHelpers<ResultType, DocType, THelpers, RawDocType>;
1879
2008
 
1880
2009
  /** Declare and/or execute this query as an update() operation. */
1881
- update(filter?: FilterQuery<DocType>, update?: UpdateQuery<DocType> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback<UpdateWriteOpResult>): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType>;
2010
+ update(filter?: FilterQuery<DocType>, update?: UpdateQuery<DocType> | UpdateWithAggregationPipeline, options?: QueryOptions<DocType> | null, callback?: Callback<UpdateWriteOpResult>): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType>;
1882
2011
 
1883
2012
  /**
1884
2013
  * Declare and/or execute this query as an updateMany() operation. Same as
@@ -1886,13 +2015,13 @@ declare module 'mongoose' {
1886
2015
  * `filter` (as opposed to just the first one) regardless of the value of
1887
2016
  * the `multi` option.
1888
2017
  */
1889
- updateMany(filter?: FilterQuery<DocType>, update?: UpdateQuery<DocType> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback<UpdateWriteOpResult>): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType>;
2018
+ updateMany(filter?: FilterQuery<DocType>, update?: UpdateQuery<DocType> | UpdateWithAggregationPipeline, options?: QueryOptions<DocType> | null, callback?: Callback<UpdateWriteOpResult>): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType>;
1890
2019
 
1891
2020
  /**
1892
2021
  * Declare and/or execute this query as an updateOne() operation. Same as
1893
2022
  * `update()`, except it does not support the `multi` or `overwrite` options.
1894
2023
  */
1895
- updateOne(filter?: FilterQuery<DocType>, update?: UpdateQuery<DocType> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback<UpdateWriteOpResult>): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType>;
2024
+ updateOne(filter?: FilterQuery<DocType>, update?: UpdateQuery<DocType> | UpdateWithAggregationPipeline, options?: QueryOptions<DocType> | null, callback?: Callback<UpdateWriteOpResult>): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType>;
1896
2025
 
1897
2026
  /**
1898
2027
  * Sets the specified number of `mongod` servers, or tag set of `mongod` servers,
@@ -1916,6 +2045,9 @@ declare module 'mongoose' {
1916
2045
  wtimeout(ms: number): this;
1917
2046
  }
1918
2047
 
2048
+ type ProjectionElementType = number | string;
2049
+ export type ProjectionType<T> = { [P in keyof T]?: ProjectionElementType } | AnyObject | string;
2050
+
1919
2051
  export type QuerySelector<T> = {
1920
2052
  // Comparison
1921
2053
  $eq?: T;
@@ -2122,153 +2254,6 @@ declare module 'mongoose' {
2122
2254
  T extends Document ? RawDocType :
2123
2255
  T;
2124
2256
 
2125
- class Aggregate<R> {
2126
- /**
2127
- * Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js
2128
- * You do not need to call this function explicitly, the JavaScript runtime
2129
- * will call it for you.
2130
- */
2131
- [Symbol.asyncIterator](): AsyncIterableIterator<Unpacked<R>>;
2132
-
2133
- /**
2134
- * Sets an option on this aggregation. This function will be deprecated in a
2135
- * future release. */
2136
- addCursorFlag(flag: string, value: boolean): this;
2137
-
2138
- /**
2139
- * Appends a new $addFields operator to this aggregate pipeline.
2140
- * Requires MongoDB v3.4+ to work
2141
- */
2142
- addFields(arg: PipelineStage.AddFields['$addFields']): this;
2143
-
2144
- /** Sets the allowDiskUse option for the aggregation query (ignored for < 2.6.0) */
2145
- allowDiskUse(value: boolean): this;
2146
-
2147
- /** Appends new operators to this aggregate pipeline */
2148
- append(...args: any[]): this;
2149
-
2150
- /**
2151
- * Executes the query returning a `Promise` which will be
2152
- * resolved with either the doc(s) or rejected with the error.
2153
- * Like [`.then()`](#query_Query-then), but only takes a rejection handler.
2154
- */
2155
- catch: Promise<R>['catch'];
2156
-
2157
- /** Adds a collation. */
2158
- collation(options: mongodb.CollationOptions): this;
2159
-
2160
- /** Appends a new $count operator to this aggregate pipeline. */
2161
- count(countName: PipelineStage.Count['$count']): this;
2162
-
2163
- /**
2164
- * Sets the cursor option for the aggregation query (ignored for < 2.6.0).
2165
- */
2166
- cursor<DocType = any>(options?: Record<string, unknown>): Cursor<DocType>;
2167
-
2168
- /** Executes the aggregate pipeline on the currently bound Model. */
2169
- exec(callback?: Callback<R>): Promise<R>;
2170
-
2171
- /** Execute the aggregation with explain */
2172
- explain(callback?: Callback): Promise<any>;
2173
- explain(verbosity?: string, callback?: Callback): Promise<any>;
2174
-
2175
- /** Combines multiple aggregation pipelines. */
2176
- facet(options: PipelineStage.Facet['$facet']): this;
2177
-
2178
- /** Appends new custom $graphLookup operator(s) to this aggregate pipeline, performing a recursive search on a collection. */
2179
- graphLookup(options: PipelineStage.GraphLookup['$graphLookup']): this;
2180
-
2181
- /** Appends new custom $group operator to this aggregate pipeline. */
2182
- group(arg: PipelineStage.Group['$group']): this;
2183
-
2184
- /** Sets the hint option for the aggregation query (ignored for < 3.6.0) */
2185
- hint(value: Record<string, unknown> | string): this;
2186
-
2187
- /**
2188
- * Appends a new $limit operator to this aggregate pipeline.
2189
- * @param num maximum number of records to pass to the next stage
2190
- */
2191
- limit(num: PipelineStage.Limit['$limit']): this;
2192
-
2193
- /** Appends new custom $lookup operator to this aggregate pipeline. */
2194
- lookup(options: PipelineStage.Lookup['$lookup']): this;
2195
-
2196
- /**
2197
- * Appends a new custom $match operator to this aggregate pipeline.
2198
- * @param arg $match operator contents
2199
- */
2200
- match(arg: PipelineStage.Match['$match']): this;
2201
-
2202
- /**
2203
- * Binds this aggregate to a model.
2204
- * @param model the model to which the aggregate is to be bound
2205
- */
2206
- model(model: any): this;
2207
-
2208
- /**
2209
- * Append a new $near operator to this aggregation pipeline
2210
- * @param arg $near operator contents
2211
- */
2212
- near(arg: { near?: number[]; distanceField: string; maxDistance?: number; query?: Record<string, any>; includeLocs?: string; num?: number; uniqueDocs?: boolean }): this;
2213
-
2214
- /** Returns the current pipeline */
2215
- pipeline(): any[];
2216
-
2217
- /** Appends a new $project operator to this aggregate pipeline. */
2218
- project(arg: PipelineStage.Project['$project']): this;
2219
-
2220
- /** Sets the readPreference option for the aggregation query. */
2221
- read(pref: string | mongodb.ReadPreferenceMode, tags?: any[]): this;
2222
-
2223
- /** Sets the readConcern level for the aggregation query. */
2224
- readConcern(level: string): this;
2225
-
2226
- /** Appends a new $redact operator to this aggregate pipeline. */
2227
- redact(expression: any, thenExpr: string | any, elseExpr: string | any): this;
2228
-
2229
- /** Appends a new $replaceRoot operator to this aggregate pipeline. */
2230
- replaceRoot(newRoot: PipelineStage.ReplaceRoot['$replaceRoot']['newRoot'] | string): this;
2231
-
2232
- /**
2233
- * Helper for [Atlas Text Search](https://docs.atlas.mongodb.com/reference/atlas-search/tutorial/)'s
2234
- * `$search` stage.
2235
- */
2236
- search(options: PipelineStage.Search['$search']): this;
2237
-
2238
- /** Lets you set arbitrary options, for middleware or plugins. */
2239
- option(value: Record<string, unknown>): this;
2240
-
2241
- /** Appends new custom $sample operator to this aggregate pipeline. */
2242
- sample(size: number): this;
2243
-
2244
- /** Sets the session for this aggregation. Useful for [transactions](/docs/transactions.html). */
2245
- session(session: mongodb.ClientSession | null): this;
2246
-
2247
- /**
2248
- * Appends a new $skip operator to this aggregate pipeline.
2249
- * @param num number of records to skip before next stage
2250
- */
2251
- skip(num: number): this;
2252
-
2253
- /** Appends a new $sort operator to this aggregate pipeline. */
2254
- sort(arg: string | Record<string, SortValues> | PipelineStage.Sort['$sort']): this;
2255
-
2256
- /** Provides promise for aggregate. */
2257
- then: Promise<R>['then'];
2258
-
2259
- /**
2260
- * Appends a new $sortByCount operator to this aggregate pipeline. Accepts either a string field name
2261
- * or a pipeline object.
2262
- */
2263
- sortByCount(arg: string | any): this;
2264
-
2265
- /** Appends new $unionWith operator to this aggregate pipeline. */
2266
- unionWith(options: any): this;
2267
-
2268
- /** Appends new custom $unwind operator(s) to this aggregate pipeline. */
2269
- unwind(...args: PipelineStage.Unwind['$unwind'][]): this;
2270
- }
2271
-
2272
2257
  class SchemaType {
2273
2258
  /** SchemaType constructor */
2274
2259
  constructor(path: string, options?: AnyObject, instance?: string);