mongoose 8.20.0 → 9.0.0-rc1

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 (90) hide show
  1. package/eslint.config.mjs +198 -0
  2. package/lib/aggregate.js +17 -73
  3. package/lib/cast/bigint.js +1 -1
  4. package/lib/cast/double.js +1 -1
  5. package/lib/cast/uuid.js +5 -48
  6. package/lib/cast.js +2 -2
  7. package/lib/connection.js +0 -1
  8. package/lib/cursor/aggregationCursor.js +14 -24
  9. package/lib/cursor/queryCursor.js +7 -14
  10. package/lib/document.js +125 -121
  11. package/lib/drivers/node-mongodb-native/connection.js +3 -10
  12. package/lib/error/objectParameter.js +1 -2
  13. package/lib/error/validation.js +0 -8
  14. package/lib/helpers/clone.js +1 -1
  15. package/lib/helpers/common.js +1 -1
  16. package/lib/helpers/indexes/isIndexEqual.js +0 -1
  17. package/lib/helpers/model/applyDefaultsToPOJO.js +2 -2
  18. package/lib/helpers/model/applyHooks.js +43 -53
  19. package/lib/helpers/model/applyMethods.js +2 -2
  20. package/lib/helpers/model/applyStaticHooks.js +1 -48
  21. package/lib/helpers/model/castBulkWrite.js +1 -1
  22. package/lib/helpers/parallelLimit.js +18 -36
  23. package/lib/helpers/pluralize.js +3 -3
  24. package/lib/helpers/populate/assignRawDocsToIdStructure.js +1 -8
  25. package/lib/helpers/populate/createPopulateQueryFilter.js +1 -1
  26. package/lib/helpers/populate/getModelsMapForPopulate.js +17 -9
  27. package/lib/helpers/populate/getSchemaTypes.js +5 -5
  28. package/lib/helpers/query/cast$expr.js +8 -10
  29. package/lib/helpers/query/castFilterPath.js +1 -1
  30. package/lib/helpers/query/castUpdate.js +14 -12
  31. package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +1 -1
  32. package/lib/helpers/schema/applyPlugins.js +1 -1
  33. package/lib/helpers/schema/getIndexes.js +1 -7
  34. package/lib/helpers/timestamps/setupTimestamps.js +3 -6
  35. package/lib/helpers/updateValidators.js +57 -111
  36. package/lib/model.js +419 -607
  37. package/lib/mongoose.js +41 -13
  38. package/lib/plugins/saveSubdocs.js +24 -51
  39. package/lib/plugins/sharding.js +5 -4
  40. package/lib/plugins/validateBeforeSave.js +3 -13
  41. package/lib/query.js +101 -145
  42. package/lib/queryHelpers.js +2 -2
  43. package/lib/schema/array.js +41 -84
  44. package/lib/schema/documentArray.js +57 -94
  45. package/lib/schema/documentArrayElement.js +16 -11
  46. package/lib/schema/string.js +1 -1
  47. package/lib/schema/subdocument.js +22 -28
  48. package/lib/schema/uuid.js +0 -21
  49. package/lib/schema.js +81 -39
  50. package/lib/schemaType.js +39 -57
  51. package/lib/types/array/index.js +2 -2
  52. package/lib/types/array/methods/index.js +4 -4
  53. package/lib/types/arraySubdocument.js +1 -1
  54. package/lib/types/buffer.js +10 -10
  55. package/lib/types/decimal128.js +1 -1
  56. package/lib/types/documentArray/index.js +1 -1
  57. package/lib/types/documentArray/methods/index.js +5 -3
  58. package/lib/types/double.js +1 -1
  59. package/lib/types/objectid.js +1 -1
  60. package/lib/types/subdocument.js +15 -43
  61. package/lib/types/uuid.js +1 -1
  62. package/lib/utils.js +1 -8
  63. package/lib/validOptions.js +3 -3
  64. package/package.json +11 -24
  65. package/types/connection.d.ts +20 -11
  66. package/types/document.d.ts +95 -26
  67. package/types/index.d.ts +143 -39
  68. package/types/inferhydrateddoctype.d.ts +115 -0
  69. package/types/inferrawdoctype.d.ts +99 -75
  70. package/types/inferschematype.d.ts +17 -3
  71. package/types/middlewares.d.ts +0 -2
  72. package/types/models.d.ts +131 -199
  73. package/types/mongooseoptions.d.ts +6 -5
  74. package/types/pipelinestage.d.ts +1 -1
  75. package/types/query.d.ts +71 -139
  76. package/types/schemaoptions.d.ts +1 -1
  77. package/types/schematypes.d.ts +14 -10
  78. package/types/types.d.ts +3 -4
  79. package/types/utility.d.ts +68 -48
  80. package/types/validation.d.ts +18 -14
  81. package/browser.js +0 -8
  82. package/dist/browser.umd.js +0 -2
  83. package/lib/browser.js +0 -141
  84. package/lib/browserDocument.js +0 -101
  85. package/lib/documentProvider.js +0 -30
  86. package/lib/drivers/browser/binary.js +0 -14
  87. package/lib/drivers/browser/decimal128.js +0 -7
  88. package/lib/drivers/browser/index.js +0 -13
  89. package/lib/drivers/browser/objectid.js +0 -29
  90. package/lib/helpers/promiseOrCallback.js +0 -54
package/types/models.d.ts CHANGED
@@ -196,7 +196,7 @@ declare module 'mongoose' {
196
196
 
197
197
  export interface ReplaceOneModel<TSchema = AnyObject> {
198
198
  /** The filter to limit the replaced document. */
199
- filter: RootFilterQuery<TSchema>;
199
+ filter: QueryFilter<TSchema>;
200
200
  /** The document with which to replace the matched document. */
201
201
  replacement: mongodb.WithoutId<TSchema>;
202
202
  /** Specifies a collation. */
@@ -211,7 +211,7 @@ declare module 'mongoose' {
211
211
 
212
212
  export interface UpdateOneModel<TSchema = AnyObject> {
213
213
  /** The filter to limit the updated documents. */
214
- filter: RootFilterQuery<TSchema>;
214
+ filter: QueryFilter<TSchema>;
215
215
  /** A document or pipeline containing update operators. */
216
216
  update: UpdateQuery<TSchema>;
217
217
  /** A set of filters specifying to which array elements an update should apply. */
@@ -228,7 +228,7 @@ declare module 'mongoose' {
228
228
 
229
229
  export interface UpdateManyModel<TSchema = AnyObject> {
230
230
  /** The filter to limit the updated documents. */
231
- filter: RootFilterQuery<TSchema>;
231
+ filter: QueryFilter<TSchema>;
232
232
  /** A document or pipeline containing update operators. */
233
233
  update: UpdateQuery<TSchema>;
234
234
  /** A set of filters specifying to which array elements an update should apply. */
@@ -245,7 +245,7 @@ declare module 'mongoose' {
245
245
 
246
246
  export interface DeleteOneModel<TSchema = AnyObject> {
247
247
  /** The filter to limit the deleted documents. */
248
- filter: RootFilterQuery<TSchema>;
248
+ filter: QueryFilter<TSchema>;
249
249
  /** Specifies a collation. */
250
250
  collation?: mongodb.CollationOptions;
251
251
  /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
@@ -254,13 +254,31 @@ declare module 'mongoose' {
254
254
 
255
255
  export interface DeleteManyModel<TSchema = AnyObject> {
256
256
  /** The filter to limit the deleted documents. */
257
- filter: RootFilterQuery<TSchema>;
257
+ filter: QueryFilter<TSchema>;
258
258
  /** Specifies a collation. */
259
259
  collation?: mongodb.CollationOptions;
260
260
  /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
261
261
  hint?: mongodb.Hint;
262
262
  }
263
263
 
264
+ /*
265
+ * Apply common casting logic to the given type, allowing:
266
+ * - strings for ObjectIds
267
+ * - strings and numbers for Dates
268
+ * - strings for Buffers
269
+ * - strings for UUIDs
270
+ * - POJOs for subdocuments
271
+ * - vanilla arrays of POJOs for document arrays
272
+ * - POJOs and array of arrays for maps
273
+ */
274
+ type ApplyBasicCreateCasting<T> = {
275
+ [K in keyof T]: NonNullable<T[K]> extends Map<infer KeyType extends string, infer ValueType>
276
+ ? (Record<KeyType, ValueType> | Array<[KeyType, ValueType]> | T[K])
277
+ : NonNullable<T[K]> extends Types.DocumentArray<infer RawSubdocType>
278
+ ? RawSubdocType[] | T[K]
279
+ : QueryTypeCasting<T[K]>;
280
+ };
281
+
264
282
  type HasLeanOption<TSchema> = 'lean' extends keyof ObtainSchemaGeneric<TSchema, 'TSchemaOptions'> ?
265
283
  ObtainSchemaGeneric<TSchema, 'TSchemaOptions'>['lean'] extends Record<string, any> ?
266
284
  true :
@@ -278,11 +296,12 @@ declare module 'mongoose' {
278
296
  TInstanceMethods = {},
279
297
  TVirtuals = {},
280
298
  THydratedDocumentType = HydratedDocument<TRawDocType, TVirtuals & TInstanceMethods, TQueryHelpers, TVirtuals>,
281
- TSchema = any> extends
299
+ TSchema = any,
300
+ TLeanResultType = TRawDocType> extends
282
301
  NodeJS.EventEmitter,
283
302
  IndexManager,
284
303
  SessionStarter {
285
- new <DocType = Partial<TRawDocType>>(doc?: DocType, fields?: any | null, options?: boolean | AnyObject): THydratedDocumentType;
304
+ new <DocType = Partial<TRawDocType>>(doc?: DocType, fields?: any | null, options?: AnyObject): THydratedDocumentType;
286
305
 
287
306
  aggregate<R = any>(pipeline?: PipelineStage[], options?: AggregateOptions): Aggregate<Array<R>>;
288
307
  aggregate<R = any>(pipeline: PipelineStage[]): Aggregate<Array<R>>;
@@ -339,7 +358,7 @@ declare module 'mongoose' {
339
358
 
340
359
  /** Creates a `countDocuments` query: counts the number of documents that match `filter`. */
341
360
  countDocuments(
342
- filter?: RootFilterQuery<TRawDocType>,
361
+ filter?: QueryFilter<TRawDocType>,
343
362
  options?: (mongodb.CountOptions & MongooseBaseQueryOptions<TRawDocType> & mongodb.Abortable) | null
344
363
  ): QueryWithHelpers<
345
364
  number,
@@ -351,10 +370,11 @@ declare module 'mongoose' {
351
370
  >;
352
371
 
353
372
  /** Creates a new document or documents */
354
- create<DocContents = AnyKeys<TRawDocType>>(docs: Array<TRawDocType | DocContents>, options: CreateOptions & { aggregateErrors: true }): Promise<(THydratedDocumentType | Error)[]>;
355
- create<DocContents = AnyKeys<TRawDocType>>(docs: Array<TRawDocType | DocContents>, options?: CreateOptions): Promise<THydratedDocumentType[]>;
356
- create<DocContents = AnyKeys<TRawDocType>>(doc: DocContents | TRawDocType): Promise<THydratedDocumentType>;
357
- create<DocContents = AnyKeys<TRawDocType>>(...docs: Array<TRawDocType | DocContents>): Promise<THydratedDocumentType[]>;
373
+ create(): Promise<null>;
374
+ create(docs: Array<DeepPartial<ApplyBasicCreateCasting<Require_id<TRawDocType>>>>, options: CreateOptions & { aggregateErrors: true }): Promise<(THydratedDocumentType | Error)[]>;
375
+ create(docs: Array<DeepPartial<ApplyBasicCreateCasting<Require_id<TRawDocType>>>>, options?: CreateOptions): Promise<THydratedDocumentType[]>;
376
+ create(doc: DeepPartial<ApplyBasicCreateCasting<Require_id<TRawDocType>>>): Promise<THydratedDocumentType>;
377
+ create(...docs: Array<DeepPartial<ApplyBasicCreateCasting<Require_id<TRawDocType>>>>): Promise<THydratedDocumentType[]>;
358
378
 
359
379
  /**
360
380
  * Create the collection for this model. By default, if no indexes are specified,
@@ -384,23 +404,13 @@ declare module 'mongoose' {
384
404
  * regardless of the `single` option.
385
405
  */
386
406
  deleteMany(
387
- filter?: RootFilterQuery<TRawDocType>,
407
+ filter?: QueryFilter<TRawDocType>,
388
408
  options?: (mongodb.DeleteOptions & MongooseBaseQueryOptions<TRawDocType>) | null
389
409
  ): QueryWithHelpers<
390
410
  mongodb.DeleteResult,
391
411
  THydratedDocumentType,
392
412
  TQueryHelpers,
393
- TRawDocType,
394
- 'deleteMany',
395
- TInstanceMethods & TVirtuals
396
- >;
397
- deleteMany(
398
- filter: RootFilterQuery<TRawDocType>
399
- ): QueryWithHelpers<
400
- mongodb.DeleteResult,
401
- THydratedDocumentType,
402
- TQueryHelpers,
403
- TRawDocType,
413
+ TLeanResultType,
404
414
  'deleteMany',
405
415
  TInstanceMethods & TVirtuals
406
416
  >;
@@ -411,23 +421,13 @@ declare module 'mongoose' {
411
421
  * `single` option.
412
422
  */
413
423
  deleteOne(
414
- filter?: RootFilterQuery<TRawDocType>,
424
+ filter?: QueryFilter<TRawDocType>,
415
425
  options?: (mongodb.DeleteOptions & MongooseBaseQueryOptions<TRawDocType>) | null
416
426
  ): QueryWithHelpers<
417
427
  mongodb.DeleteResult,
418
428
  THydratedDocumentType,
419
429
  TQueryHelpers,
420
- TRawDocType,
421
- 'deleteOne',
422
- TInstanceMethods & TVirtuals
423
- >;
424
- deleteOne(
425
- filter: RootFilterQuery<TRawDocType>
426
- ): QueryWithHelpers<
427
- mongodb.DeleteResult,
428
- THydratedDocumentType,
429
- TQueryHelpers,
430
- TRawDocType,
430
+ TLeanResultType,
431
431
  'deleteOne',
432
432
  TInstanceMethods & TVirtuals
433
433
  >;
@@ -476,80 +476,48 @@ declare module 'mongoose' {
476
476
  projection: ProjectionType<TRawDocType> | null | undefined,
477
477
  options: QueryOptions<TRawDocType> & { lean: true }
478
478
  ): QueryWithHelpers<
479
- GetLeanResultType<TRawDocType, TRawDocType, 'findOne'> | null,
479
+ TLeanResultType | null,
480
480
  ResultDoc,
481
481
  TQueryHelpers,
482
- TRawDocType,
482
+ TLeanResultType,
483
483
  'findOne',
484
484
  TInstanceMethods & TVirtuals
485
485
  >;
486
486
  findById<ResultDoc = THydratedDocumentType>(
487
- id: any,
488
- projection?: ProjectionType<TRawDocType> | null,
487
+ id?: any,
488
+ projection?: ProjectionType<TRawDocType> | null | undefined,
489
489
  options?: QueryOptions<TRawDocType> | null
490
490
  ): QueryWithHelpers<
491
- HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
492
- ResultDoc,
493
- TQueryHelpers,
494
- TRawDocType,
495
- 'findOne',
496
- TInstanceMethods & TVirtuals
497
- >;
498
- findById<ResultDoc = THydratedDocumentType>(
499
- id: any,
500
- projection?: ProjectionType<TRawDocType> | null
501
- ): QueryWithHelpers<
502
- HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
491
+ HasLeanOption<TSchema> extends true ? TLeanResultType | null : ResultDoc | null,
503
492
  ResultDoc,
504
493
  TQueryHelpers,
505
- TRawDocType,
494
+ TLeanResultType,
506
495
  'findOne',
507
496
  TInstanceMethods & TVirtuals
508
497
  >;
509
498
 
510
499
  /** Finds one document. */
511
500
  findOne<ResultDoc = THydratedDocumentType>(
512
- filter: RootFilterQuery<TRawDocType>,
501
+ filter: QueryFilter<TRawDocType>,
513
502
  projection: ProjectionType<TRawDocType> | null | undefined,
514
503
  options: QueryOptions<TRawDocType> & { lean: true } & mongodb.Abortable
515
504
  ): QueryWithHelpers<
516
- GetLeanResultType<TRawDocType, TRawDocType, 'findOne'> | null,
517
- ResultDoc,
518
- TQueryHelpers,
519
- TRawDocType,
520
- 'findOne',
521
- TInstanceMethods & TVirtuals
522
- >;
523
- findOne<ResultDoc = THydratedDocumentType>(
524
- filter?: RootFilterQuery<TRawDocType>,
525
- projection?: ProjectionType<TRawDocType> | null,
526
- options?: QueryOptions<TRawDocType> & mongodb.Abortable | null
527
- ): QueryWithHelpers<
528
- HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
529
- ResultDoc,
530
- TQueryHelpers,
531
- TRawDocType,
532
- 'findOne',
533
- TInstanceMethods & TVirtuals
534
- >;
535
- findOne<ResultDoc = THydratedDocumentType>(
536
- filter?: RootFilterQuery<TRawDocType>,
537
- projection?: ProjectionType<TRawDocType> | null
538
- ): QueryWithHelpers<
539
- HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
505
+ TLeanResultType | null,
540
506
  ResultDoc,
541
507
  TQueryHelpers,
542
- TRawDocType,
508
+ TLeanResultType,
543
509
  'findOne',
544
510
  TInstanceMethods & TVirtuals
545
511
  >;
546
512
  findOne<ResultDoc = THydratedDocumentType>(
547
- filter?: RootFilterQuery<TRawDocType>
513
+ filter?: QueryFilter<TRawDocType>,
514
+ projection?: ProjectionType<TRawDocType> | null | undefined,
515
+ options?: QueryOptions<TRawDocType> & mongodb.Abortable | null | undefined
548
516
  ): QueryWithHelpers<
549
- HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
517
+ HasLeanOption<TSchema> extends true ? TLeanResultType | null : ResultDoc | null,
550
518
  ResultDoc,
551
519
  TQueryHelpers,
552
- TRawDocType,
520
+ TLeanResultType,
553
521
  'findOne',
554
522
  TInstanceMethods & TVirtuals
555
523
  >;
@@ -616,10 +584,6 @@ declare module 'mongoose' {
616
584
  docs: Array<DocContents | TRawDocType>,
617
585
  options: InsertManyOptions & { lean: true; }
618
586
  ): Promise<Array<Require_id<DocContents>>>;
619
- insertMany<DocContents = TRawDocType>(
620
- docs: DocContents | TRawDocType,
621
- options: InsertManyOptions & { lean: true; }
622
- ): Promise<Array<Require_id<DocContents>>>;
623
587
  insertMany<DocContents = TRawDocType>(
624
588
  docs: Array<DocContents | TRawDocType>,
625
589
  options: InsertManyOptions & { rawResult: true; }
@@ -658,7 +622,7 @@ declare module 'mongoose' {
658
622
  * `MyModel.insertOne(obj, options)` is almost equivalent to `new MyModel(obj).save(options)`.
659
623
  * The difference is that `insertOne()` checks if `obj` is already a document, and checks for discriminators.
660
624
  */
661
- insertOne<DocContents = AnyKeys<TRawDocType>>(doc: DocContents | TRawDocType, options?: SaveOptions): Promise<THydratedDocumentType>;
625
+ insertOne(doc: Partial<ApplyBasicCreateCasting<TRawDocType>>, options?: SaveOptions): Promise<THydratedDocumentType>;
662
626
 
663
627
  /**
664
628
  * List all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) on this model's collection.
@@ -717,19 +681,19 @@ declare module 'mongoose' {
717
681
  translateAliases(raw: any): any;
718
682
 
719
683
  /** Creates a `distinct` query: returns the distinct values of the given `field` that match `filter`. */
720
- distinct<DocKey extends string, ResultType = unknown>(
684
+ distinct<DocKey extends string>(
721
685
  field: DocKey,
722
- filter?: RootFilterQuery<TRawDocType>,
686
+ filter?: QueryFilter<TRawDocType>,
723
687
  options?: QueryOptions<TRawDocType>
724
688
  ): QueryWithHelpers<
725
689
  Array<
726
690
  DocKey extends keyof WithLevel1NestedPaths<TRawDocType>
727
691
  ? WithoutUndefined<Unpacked<WithLevel1NestedPaths<TRawDocType>[DocKey]>>
728
- : ResultType
692
+ : unknown
729
693
  >,
730
694
  THydratedDocumentType,
731
695
  TQueryHelpers,
732
- TRawDocType,
696
+ TLeanResultType,
733
697
  'distinct',
734
698
  TInstanceMethods & TVirtuals
735
699
  >;
@@ -739,7 +703,7 @@ declare module 'mongoose' {
739
703
  number,
740
704
  THydratedDocumentType,
741
705
  TQueryHelpers,
742
- TRawDocType,
706
+ TLeanResultType,
743
707
  'estimatedDocumentCount',
744
708
  TInstanceMethods & TVirtuals
745
709
  >;
@@ -749,81 +713,62 @@ declare module 'mongoose' {
749
713
  * the given `filter`, and `null` otherwise.
750
714
  */
751
715
  exists(
752
- filter: RootFilterQuery<TRawDocType>
716
+ filter: QueryFilter<TRawDocType>
753
717
  ): QueryWithHelpers<
754
718
  { _id: InferId<TRawDocType> } | null,
755
719
  THydratedDocumentType,
756
720
  TQueryHelpers,
757
- TRawDocType,
721
+ TLeanResultType,
758
722
  'findOne',
759
723
  TInstanceMethods & TVirtuals
760
724
  >;
761
725
 
762
726
  /** Creates a `find` query: gets a list of documents that match `filter`. */
763
727
  find<ResultDoc = THydratedDocumentType>(
764
- filter: RootFilterQuery<TRawDocType>,
728
+ filter: QueryFilter<TRawDocType>,
765
729
  projection: ProjectionType<TRawDocType> | null | undefined,
766
730
  options: QueryOptions<TRawDocType> & { lean: true } & mongodb.Abortable
767
731
  ): QueryWithHelpers<
768
732
  GetLeanResultType<TRawDocType, TRawDocType[], 'find'>,
769
733
  ResultDoc,
770
734
  TQueryHelpers,
771
- TRawDocType,
735
+ TLeanResultType,
772
736
  'find',
773
737
  TInstanceMethods & TVirtuals
774
738
  >;
775
739
  find<ResultDoc = THydratedDocumentType>(
776
- filter: RootFilterQuery<TRawDocType>,
740
+ filter?: QueryFilter<TRawDocType>,
777
741
  projection?: ProjectionType<TRawDocType> | null | undefined,
778
- options?: QueryOptions<TRawDocType> & mongodb.Abortable | null | undefined
742
+ options?: QueryOptions<TRawDocType> & mongodb.Abortable
779
743
  ): QueryWithHelpers<
780
- HasLeanOption<TSchema> extends true ? TRawDocType[] : ResultDoc[],
744
+ ResultDoc[],
781
745
  ResultDoc,
782
746
  TQueryHelpers,
783
- TRawDocType,
747
+ TLeanResultType,
784
748
  'find',
785
749
  TInstanceMethods & TVirtuals
786
750
  >;
787
- find<ResultDoc = THydratedDocumentType>(
788
- filter: RootFilterQuery<TRawDocType>,
789
- projection?: ProjectionType<TRawDocType> | null | undefined
790
- ): QueryWithHelpers<
791
- HasLeanOption<TSchema> extends true ? TRawDocType[] : ResultDoc[],
792
- ResultDoc,
793
- TQueryHelpers,
794
- TRawDocType,
795
- 'find',
796
- TInstanceMethods & TVirtuals
797
- >;
798
- find<ResultDoc = THydratedDocumentType>(
799
- filter: RootFilterQuery<TRawDocType>
800
- ): QueryWithHelpers<
801
- HasLeanOption<TSchema> extends true ? TRawDocType[] : ResultDoc[],
802
- ResultDoc,
803
- TQueryHelpers,
804
- TRawDocType,
805
- 'find',
806
- TInstanceMethods & TVirtuals
807
- >;
808
- find<ResultDoc = THydratedDocumentType>(
751
+
752
+ /** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */
753
+ findByIdAndDelete<ResultDoc = THydratedDocumentType>(
754
+ id: mongodb.ObjectId | any,
755
+ options: QueryOptions<TRawDocType> & { includeResultMetadata: true, lean: true }
809
756
  ): QueryWithHelpers<
810
- HasLeanOption<TSchema> extends true ? TRawDocType[] : ResultDoc[],
757
+ ModifyResult<TLeanResultType>,
811
758
  ResultDoc,
812
759
  TQueryHelpers,
813
- TRawDocType,
814
- 'find',
760
+ TLeanResultType,
761
+ 'findOneAndDelete',
815
762
  TInstanceMethods & TVirtuals
816
763
  >;
817
-
818
- /** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */
819
764
  findByIdAndDelete<ResultDoc = THydratedDocumentType>(
820
765
  id: mongodb.ObjectId | any,
821
766
  options: QueryOptions<TRawDocType> & { lean: true }
822
767
  ): QueryWithHelpers<
823
- GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndDelete'> | null,
768
+ TLeanResultType | null,
824
769
  ResultDoc,
825
770
  TQueryHelpers,
826
- TRawDocType,
771
+ TLeanResultType,
827
772
  'findOneAndDelete',
828
773
  TInstanceMethods & TVirtuals
829
774
  >;
@@ -831,10 +776,10 @@ declare module 'mongoose' {
831
776
  id: mongodb.ObjectId | any,
832
777
  options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
833
778
  ): QueryWithHelpers<
834
- HasLeanOption<TSchema> extends true ? ModifyResult<TRawDocType> : ModifyResult<ResultDoc>,
779
+ HasLeanOption<TSchema> extends true ? ModifyResult<TLeanResultType> : ModifyResult<ResultDoc>,
835
780
  ResultDoc,
836
781
  TQueryHelpers,
837
- TRawDocType,
782
+ TLeanResultType,
838
783
  'findOneAndDelete',
839
784
  TInstanceMethods & TVirtuals
840
785
  >;
@@ -842,24 +787,25 @@ declare module 'mongoose' {
842
787
  id?: mongodb.ObjectId | any,
843
788
  options?: QueryOptions<TRawDocType> | null
844
789
  ): QueryWithHelpers<
845
- HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
790
+ HasLeanOption<TSchema> extends true ? TLeanResultType | null : ResultDoc | null,
846
791
  ResultDoc,
847
792
  TQueryHelpers,
848
- TRawDocType,
793
+ TLeanResultType,
849
794
  'findOneAndDelete',
850
795
  TInstanceMethods & TVirtuals
851
796
  >;
852
797
 
798
+
853
799
  /** Creates a `findOneAndUpdate` query, filtering by the given `_id`. */
854
800
  findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
855
- filter: RootFilterQuery<TRawDocType>,
801
+ filter: QueryFilter<TRawDocType>,
856
802
  update: UpdateQuery<TRawDocType>,
857
803
  options: QueryOptions<TRawDocType> & { includeResultMetadata: true, lean: true }
858
804
  ): QueryWithHelpers<
859
805
  ModifyResult<TRawDocType>,
860
806
  ResultDoc,
861
807
  TQueryHelpers,
862
- TRawDocType,
808
+ TLeanResultType,
863
809
  'findOneAndUpdate',
864
810
  TInstanceMethods & TVirtuals
865
811
  >;
@@ -868,10 +814,10 @@ declare module 'mongoose' {
868
814
  update: UpdateQuery<TRawDocType>,
869
815
  options: QueryOptions<TRawDocType> & { lean: true }
870
816
  ): QueryWithHelpers<
871
- GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndUpdate'> | null,
817
+ TLeanResultType | null,
872
818
  ResultDoc,
873
819
  TQueryHelpers,
874
- TRawDocType,
820
+ TLeanResultType,
875
821
  'findOneAndUpdate',
876
822
  TInstanceMethods & TVirtuals
877
823
  >;
@@ -880,10 +826,10 @@ declare module 'mongoose' {
880
826
  update: UpdateQuery<TRawDocType>,
881
827
  options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
882
828
  ): QueryWithHelpers<
883
- HasLeanOption<TSchema> extends true ? ModifyResult<TRawDocType> : ModifyResult<ResultDoc>,
829
+ HasLeanOption<TSchema> extends true ? ModifyResult<TLeanResultType> : ModifyResult<ResultDoc>,
884
830
  ResultDoc,
885
831
  TQueryHelpers,
886
- TRawDocType,
832
+ TLeanResultType,
887
833
  'findOneAndUpdate',
888
834
  TInstanceMethods & TVirtuals
889
835
  >;
@@ -892,10 +838,10 @@ declare module 'mongoose' {
892
838
  update: UpdateQuery<TRawDocType>,
893
839
  options: QueryOptions<TRawDocType> & { upsert: true } & ReturnsNewDoc
894
840
  ): QueryWithHelpers<
895
- HasLeanOption<TSchema> extends true ? TRawDocType : ResultDoc,
841
+ HasLeanOption<TSchema> extends true ? TLeanResultType : ResultDoc,
896
842
  ResultDoc,
897
843
  TQueryHelpers,
898
- TRawDocType,
844
+ TLeanResultType,
899
845
  'findOneAndUpdate',
900
846
  TInstanceMethods & TVirtuals
901
847
  >;
@@ -904,178 +850,167 @@ declare module 'mongoose' {
904
850
  update?: UpdateQuery<TRawDocType>,
905
851
  options?: QueryOptions<TRawDocType> | null
906
852
  ): QueryWithHelpers<
907
- HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
908
- ResultDoc,
909
- TQueryHelpers,
910
- TRawDocType,
911
- 'findOneAndUpdate',
912
- TInstanceMethods & TVirtuals
913
- >;
914
- findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
915
- id: mongodb.ObjectId | any,
916
- update: UpdateQuery<TRawDocType>
917
- ): QueryWithHelpers<
918
- HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
853
+ HasLeanOption<TSchema> extends true ? TLeanResultType | null : ResultDoc | null,
919
854
  ResultDoc,
920
855
  TQueryHelpers,
921
- TRawDocType,
856
+ TLeanResultType,
922
857
  'findOneAndUpdate',
923
858
  TInstanceMethods & TVirtuals
924
859
  >;
925
860
 
926
861
  /** Creates a `findOneAndDelete` query: atomically finds the given document, deletes it, and returns the document as it was before deletion. */
927
862
  findOneAndDelete<ResultDoc = THydratedDocumentType>(
928
- filter: RootFilterQuery<TRawDocType>,
863
+ filter: QueryFilter<TRawDocType>,
929
864
  options: QueryOptions<TRawDocType> & { lean: true }
930
865
  ): QueryWithHelpers<
931
- GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndDelete'> | null,
866
+ TLeanResultType | null,
932
867
  ResultDoc,
933
868
  TQueryHelpers,
934
- TRawDocType,
869
+ TLeanResultType,
935
870
  'findOneAndDelete',
936
871
  TInstanceMethods & TVirtuals
937
872
  >;
938
873
  findOneAndDelete<ResultDoc = THydratedDocumentType>(
939
- filter: RootFilterQuery<TRawDocType>,
874
+ filter: QueryFilter<TRawDocType>,
940
875
  options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
941
876
  ): QueryWithHelpers<
942
877
  HasLeanOption<TSchema> extends true ? ModifyResult<TRawDocType> : ModifyResult<ResultDoc>,
943
878
  ResultDoc,
944
879
  TQueryHelpers,
945
- TRawDocType,
880
+ TLeanResultType,
946
881
  'findOneAndDelete',
947
882
  TInstanceMethods & TVirtuals
948
883
  >;
949
884
  findOneAndDelete<ResultDoc = THydratedDocumentType>(
950
- filter?: RootFilterQuery<TRawDocType> | null,
885
+ filter?: QueryFilter<TRawDocType> | null,
951
886
  options?: QueryOptions<TRawDocType> | null
952
887
  ): QueryWithHelpers<
953
888
  HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
954
889
  ResultDoc,
955
890
  TQueryHelpers,
956
- TRawDocType,
891
+ TLeanResultType,
957
892
  'findOneAndDelete',
958
893
  TInstanceMethods & TVirtuals
959
894
  >;
960
895
 
961
896
  /** Creates a `findOneAndReplace` query: atomically finds the given document and replaces it with `replacement`. */
962
897
  findOneAndReplace<ResultDoc = THydratedDocumentType>(
963
- filter: RootFilterQuery<TRawDocType>,
898
+ filter: QueryFilter<TRawDocType>,
964
899
  replacement: TRawDocType | AnyObject,
965
900
  options: QueryOptions<TRawDocType> & { lean: true }
966
901
  ): QueryWithHelpers<
967
- GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndReplace'> | null,
902
+ TLeanResultType | null,
968
903
  ResultDoc,
969
904
  TQueryHelpers,
970
- TRawDocType,
905
+ TLeanResultType,
971
906
  'findOneAndReplace',
972
907
  TInstanceMethods & TVirtuals
973
908
  >;
974
909
  findOneAndReplace<ResultDoc = THydratedDocumentType>(
975
- filter: RootFilterQuery<TRawDocType>,
910
+ filter: QueryFilter<TRawDocType>,
976
911
  replacement: TRawDocType | AnyObject,
977
912
  options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
978
913
  ): QueryWithHelpers<
979
- HasLeanOption<TSchema> extends true ? ModifyResult<TRawDocType> : ModifyResult<ResultDoc>,
914
+ HasLeanOption<TSchema> extends true ? ModifyResult<TLeanResultType> : ModifyResult<ResultDoc>,
980
915
  ResultDoc,
981
916
  TQueryHelpers,
982
- TRawDocType,
917
+ TLeanResultType,
983
918
  'findOneAndReplace',
984
919
  TInstanceMethods & TVirtuals
985
920
  >;
986
921
  findOneAndReplace<ResultDoc = THydratedDocumentType>(
987
- filter: RootFilterQuery<TRawDocType>,
922
+ filter: QueryFilter<TRawDocType>,
988
923
  replacement: TRawDocType | AnyObject,
989
924
  options: QueryOptions<TRawDocType> & { upsert: true } & ReturnsNewDoc
990
925
  ): QueryWithHelpers<
991
- HasLeanOption<TSchema> extends true ? TRawDocType : ResultDoc,
926
+ HasLeanOption<TSchema> extends true ? TLeanResultType : ResultDoc,
992
927
  ResultDoc,
993
928
  TQueryHelpers,
994
- TRawDocType,
929
+ TLeanResultType,
995
930
  'findOneAndReplace',
996
931
  TInstanceMethods & TVirtuals
997
932
  >;
998
933
  findOneAndReplace<ResultDoc = THydratedDocumentType>(
999
- filter?: RootFilterQuery<TRawDocType>,
934
+ filter?: QueryFilter<TRawDocType>,
1000
935
  replacement?: TRawDocType | AnyObject,
1001
936
  options?: QueryOptions<TRawDocType> | null
1002
937
  ): QueryWithHelpers<
1003
- HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
938
+ HasLeanOption<TSchema> extends true ? TLeanResultType | null : ResultDoc | null,
1004
939
  ResultDoc,
1005
940
  TQueryHelpers,
1006
- TRawDocType,
941
+ TLeanResultType,
1007
942
  'findOneAndReplace',
1008
943
  TInstanceMethods & TVirtuals
1009
944
  >;
1010
945
 
1011
946
  /** Creates a `findOneAndUpdate` query: atomically find the first document that matches `filter` and apply `update`. */
1012
947
  findOneAndUpdate<ResultDoc = THydratedDocumentType>(
1013
- filter: RootFilterQuery<TRawDocType>,
948
+ filter: QueryFilter<TRawDocType>,
1014
949
  update: UpdateQuery<TRawDocType>,
1015
950
  options: QueryOptions<TRawDocType> & { includeResultMetadata: true, lean: true }
1016
951
  ): QueryWithHelpers<
1017
952
  ModifyResult<TRawDocType>,
1018
953
  ResultDoc,
1019
954
  TQueryHelpers,
1020
- TRawDocType,
955
+ TLeanResultType,
1021
956
  'findOneAndUpdate',
1022
957
  TInstanceMethods & TVirtuals
1023
958
  >;
1024
959
  findOneAndUpdate<ResultDoc = THydratedDocumentType>(
1025
- filter: RootFilterQuery<TRawDocType>,
960
+ filter: QueryFilter<TRawDocType>,
1026
961
  update: UpdateQuery<TRawDocType>,
1027
962
  options: QueryOptions<TRawDocType> & { lean: true }
1028
963
  ): QueryWithHelpers<
1029
964
  GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndUpdate'> | null,
1030
965
  ResultDoc,
1031
966
  TQueryHelpers,
1032
- TRawDocType,
967
+ TLeanResultType,
1033
968
  'findOneAndUpdate',
1034
969
  TInstanceMethods & TVirtuals
1035
970
  >;
1036
971
  findOneAndUpdate<ResultDoc = THydratedDocumentType>(
1037
- filter: RootFilterQuery<TRawDocType>,
972
+ filter: QueryFilter<TRawDocType>,
1038
973
  update: UpdateQuery<TRawDocType>,
1039
974
  options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
1040
975
  ): QueryWithHelpers<
1041
- HasLeanOption<TSchema> extends true ? ModifyResult<TRawDocType> : ModifyResult<ResultDoc>,
976
+ HasLeanOption<TSchema> extends true ? ModifyResult<TLeanResultType> : ModifyResult<ResultDoc>,
1042
977
  ResultDoc,
1043
978
  TQueryHelpers,
1044
- TRawDocType,
979
+ TLeanResultType,
1045
980
  'findOneAndUpdate',
1046
981
  TInstanceMethods & TVirtuals
1047
982
  >;
1048
983
  findOneAndUpdate<ResultDoc = THydratedDocumentType>(
1049
- filter: RootFilterQuery<TRawDocType>,
984
+ filter: QueryFilter<TRawDocType>,
1050
985
  update: UpdateQuery<TRawDocType>,
1051
986
  options: QueryOptions<TRawDocType> & { upsert: true } & ReturnsNewDoc
1052
987
  ): QueryWithHelpers<
1053
- HasLeanOption<TSchema> extends true ? TRawDocType : ResultDoc,
988
+ HasLeanOption<TSchema> extends true ? TLeanResultType : ResultDoc,
1054
989
  ResultDoc,
1055
990
  TQueryHelpers,
1056
- TRawDocType,
991
+ TLeanResultType,
1057
992
  'findOneAndUpdate',
1058
993
  TInstanceMethods & TVirtuals
1059
994
  >;
1060
995
  findOneAndUpdate<ResultDoc = THydratedDocumentType>(
1061
- filter?: RootFilterQuery<TRawDocType>,
996
+ filter?: QueryFilter<TRawDocType>,
1062
997
  update?: UpdateQuery<TRawDocType>,
1063
998
  options?: QueryOptions<TRawDocType> | null
1064
999
  ): QueryWithHelpers<
1065
- HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
1000
+ HasLeanOption<TSchema> extends true ? TLeanResultType | null : ResultDoc | null,
1066
1001
  ResultDoc,
1067
1002
  TQueryHelpers,
1068
- TRawDocType,
1003
+ TLeanResultType,
1069
1004
  'findOneAndUpdate',
1070
1005
  TInstanceMethods & TVirtuals
1071
1006
  >;
1072
1007
 
1073
1008
  /** Creates a `replaceOne` query: finds the first document that matches `filter` and replaces it with `replacement`. */
1074
1009
  replaceOne<ResultDoc = THydratedDocumentType>(
1075
- filter?: RootFilterQuery<TRawDocType>,
1010
+ filter?: QueryFilter<TRawDocType>,
1076
1011
  replacement?: TRawDocType | AnyObject,
1077
1012
  options?: (mongodb.ReplaceOptions & QueryOptions<TRawDocType>) | null
1078
- ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'replaceOne', TInstanceMethods & TVirtuals>;
1013
+ ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TLeanResultType, 'replaceOne', TInstanceMethods & TVirtuals>;
1079
1014
 
1080
1015
  /** Apply changes made to this model's schema after this model was compiled. */
1081
1016
  recompileSchema(): void;
@@ -1084,21 +1019,18 @@ declare module 'mongoose' {
1084
1019
  schema: Schema<TRawDocType>;
1085
1020
 
1086
1021
  /** Creates a `updateMany` query: updates all documents that match `filter` with `update`. */
1087
- updateMany<ResultDoc = THydratedDocumentType>(
1088
- filter: RootFilterQuery<TRawDocType>,
1022
+ updateMany(
1023
+ filter: QueryFilter<TRawDocType>,
1089
1024
  update: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline,
1090
1025
  options?: (mongodb.UpdateOptions & MongooseUpdateQueryOptions<TRawDocType>) | null
1091
- ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'updateMany', TInstanceMethods & TVirtuals>;
1026
+ ): QueryWithHelpers<UpdateWriteOpResult, THydratedDocumentType, TQueryHelpers, TLeanResultType, 'updateMany', TInstanceMethods & TVirtuals>;
1092
1027
 
1093
1028
  /** Creates a `updateOne` query: updates the first document that matches `filter` with `update`. */
1094
- updateOne<ResultDoc = THydratedDocumentType>(
1095
- filter: RootFilterQuery<TRawDocType>,
1029
+ updateOne(
1030
+ filter: QueryFilter<TRawDocType>,
1096
1031
  update: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline,
1097
1032
  options?: (mongodb.UpdateOptions & MongooseUpdateQueryOptions<TRawDocType>) | null
1098
- ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'updateOne', TInstanceMethods & TVirtuals>;
1099
- updateOne<ResultDoc = THydratedDocumentType>(
1100
- update: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline
1101
- ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'updateOne', TInstanceMethods & TVirtuals>;
1033
+ ): QueryWithHelpers<UpdateWriteOpResult, THydratedDocumentType, TQueryHelpers, TLeanResultType, 'updateOne', TInstanceMethods & TVirtuals>;
1102
1034
 
1103
1035
  /** Creates a Query, applies the passed conditions, and returns the Query. */
1104
1036
  where<ResultDoc = THydratedDocumentType>(
@@ -1109,7 +1041,7 @@ declare module 'mongoose' {
1109
1041
  HasLeanOption<TSchema> extends true ? TRawDocType[] : ResultDoc[],
1110
1042
  ResultDoc,
1111
1043
  TQueryHelpers,
1112
- TRawDocType,
1044
+ TLeanResultType,
1113
1045
  'find',
1114
1046
  TInstanceMethods & TVirtuals
1115
1047
  >;
@@ -1117,7 +1049,7 @@ declare module 'mongoose' {
1117
1049
  HasLeanOption<TSchema> extends true ? TRawDocType[] : ResultDoc[],
1118
1050
  ResultDoc,
1119
1051
  TQueryHelpers,
1120
- TRawDocType,
1052
+ TLeanResultType,
1121
1053
  'find',
1122
1054
  TInstanceMethods & TVirtuals
1123
1055
  >;