mongoose 8.18.3 → 8.19.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.
package/types/models.d.ts CHANGED
@@ -58,6 +58,7 @@ declare module 'mongoose' {
58
58
  interface HydrateOptions {
59
59
  setters?: boolean;
60
60
  hydratedPopulatedDocs?: boolean;
61
+ virtuals?: boolean;
61
62
  }
62
63
 
63
64
  interface InsertManyOptions extends
@@ -260,6 +261,12 @@ declare module 'mongoose' {
260
261
  hint?: mongodb.Hint;
261
262
  }
262
263
 
264
+ type HasLeanOption<TSchema> = 'lean' extends keyof ObtainSchemaGeneric<TSchema, 'TSchemaOptions'> ?
265
+ ObtainSchemaGeneric<TSchema, 'TSchemaOptions'>['lean'] extends Record<string, any> ?
266
+ true :
267
+ ObtainSchemaGeneric<TSchema, 'TSchemaOptions'>['lean'] :
268
+ false;
269
+
263
270
  /**
264
271
  * Models are fancy constructors compiled from `Schema` definitions.
265
272
  * An instance of a model is called a document.
@@ -480,11 +487,25 @@ declare module 'mongoose' {
480
487
  id: any,
481
488
  projection?: ProjectionType<TRawDocType> | null,
482
489
  options?: QueryOptions<TRawDocType> | null
483
- ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOne', TInstanceMethods & TVirtuals>;
490
+ ): QueryWithHelpers<
491
+ HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
492
+ ResultDoc,
493
+ TQueryHelpers,
494
+ TRawDocType,
495
+ 'findOne',
496
+ TInstanceMethods & TVirtuals
497
+ >;
484
498
  findById<ResultDoc = THydratedDocumentType>(
485
499
  id: any,
486
500
  projection?: ProjectionType<TRawDocType> | null
487
- ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOne', TInstanceMethods & TVirtuals>;
501
+ ): QueryWithHelpers<
502
+ HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
503
+ ResultDoc,
504
+ TQueryHelpers,
505
+ TRawDocType,
506
+ 'findOne',
507
+ TInstanceMethods & TVirtuals
508
+ >;
488
509
 
489
510
  /** Finds one document. */
490
511
  findOne<ResultDoc = THydratedDocumentType>(
@@ -503,14 +524,35 @@ declare module 'mongoose' {
503
524
  filter?: RootFilterQuery<TRawDocType>,
504
525
  projection?: ProjectionType<TRawDocType> | null,
505
526
  options?: QueryOptions<TRawDocType> & mongodb.Abortable | null
506
- ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOne', TInstanceMethods & TVirtuals>;
527
+ ): QueryWithHelpers<
528
+ HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
529
+ ResultDoc,
530
+ TQueryHelpers,
531
+ TRawDocType,
532
+ 'findOne',
533
+ TInstanceMethods & TVirtuals
534
+ >;
507
535
  findOne<ResultDoc = THydratedDocumentType>(
508
536
  filter?: RootFilterQuery<TRawDocType>,
509
537
  projection?: ProjectionType<TRawDocType> | null
510
- ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOne', TInstanceMethods & TVirtuals>;
538
+ ): QueryWithHelpers<
539
+ HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
540
+ ResultDoc,
541
+ TQueryHelpers,
542
+ TRawDocType,
543
+ 'findOne',
544
+ TInstanceMethods & TVirtuals
545
+ >;
511
546
  findOne<ResultDoc = THydratedDocumentType>(
512
547
  filter?: RootFilterQuery<TRawDocType>
513
- ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOne', TInstanceMethods & TVirtuals>;
548
+ ): QueryWithHelpers<
549
+ HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
550
+ ResultDoc,
551
+ TQueryHelpers,
552
+ TRawDocType,
553
+ 'findOne',
554
+ TInstanceMethods & TVirtuals
555
+ >;
514
556
 
515
557
  /**
516
558
  * Shortcut for creating a new Document from existing raw data, pre-saved in the DB.
@@ -734,16 +776,44 @@ declare module 'mongoose' {
734
776
  filter: RootFilterQuery<TRawDocType>,
735
777
  projection?: ProjectionType<TRawDocType> | null | undefined,
736
778
  options?: QueryOptions<TRawDocType> & mongodb.Abortable | null | undefined
737
- ): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'find', TInstanceMethods & TVirtuals>;
779
+ ): QueryWithHelpers<
780
+ HasLeanOption<TSchema> extends true ? TRawDocType[] : ResultDoc[],
781
+ ResultDoc,
782
+ TQueryHelpers,
783
+ TRawDocType,
784
+ 'find',
785
+ TInstanceMethods & TVirtuals
786
+ >;
738
787
  find<ResultDoc = THydratedDocumentType>(
739
788
  filter: RootFilterQuery<TRawDocType>,
740
789
  projection?: ProjectionType<TRawDocType> | null | undefined
741
- ): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'find', TInstanceMethods & TVirtuals>;
790
+ ): QueryWithHelpers<
791
+ HasLeanOption<TSchema> extends true ? TRawDocType[] : ResultDoc[],
792
+ ResultDoc,
793
+ TQueryHelpers,
794
+ TRawDocType,
795
+ 'find',
796
+ TInstanceMethods & TVirtuals
797
+ >;
742
798
  find<ResultDoc = THydratedDocumentType>(
743
799
  filter: RootFilterQuery<TRawDocType>
744
- ): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'find', TInstanceMethods & TVirtuals>;
800
+ ): QueryWithHelpers<
801
+ HasLeanOption<TSchema> extends true ? TRawDocType[] : ResultDoc[],
802
+ ResultDoc,
803
+ TQueryHelpers,
804
+ TRawDocType,
805
+ 'find',
806
+ TInstanceMethods & TVirtuals
807
+ >;
745
808
  find<ResultDoc = THydratedDocumentType>(
746
- ): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'find', TInstanceMethods & TVirtuals>;
809
+ ): QueryWithHelpers<
810
+ HasLeanOption<TSchema> extends true ? TRawDocType[] : ResultDoc[],
811
+ ResultDoc,
812
+ TQueryHelpers,
813
+ TRawDocType,
814
+ 'find',
815
+ TInstanceMethods & TVirtuals
816
+ >;
747
817
 
748
818
  /** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */
749
819
  findByIdAndDelete<ResultDoc = THydratedDocumentType>(
@@ -760,11 +830,25 @@ declare module 'mongoose' {
760
830
  findByIdAndDelete<ResultDoc = THydratedDocumentType>(
761
831
  id: mongodb.ObjectId | any,
762
832
  options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
763
- ): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete', TInstanceMethods & TVirtuals>;
833
+ ): QueryWithHelpers<
834
+ HasLeanOption<TSchema> extends true ? ModifyResult<TRawDocType> : ModifyResult<ResultDoc>,
835
+ ResultDoc,
836
+ TQueryHelpers,
837
+ TRawDocType,
838
+ 'findOneAndDelete',
839
+ TInstanceMethods & TVirtuals
840
+ >;
764
841
  findByIdAndDelete<ResultDoc = THydratedDocumentType>(
765
842
  id?: mongodb.ObjectId | any,
766
843
  options?: QueryOptions<TRawDocType> | null
767
- ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete', TInstanceMethods & TVirtuals>;
844
+ ): QueryWithHelpers<
845
+ HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
846
+ ResultDoc,
847
+ TQueryHelpers,
848
+ TRawDocType,
849
+ 'findOneAndDelete',
850
+ TInstanceMethods & TVirtuals
851
+ >;
768
852
 
769
853
  /** Creates a `findOneAndUpdate` query, filtering by the given `_id`. */
770
854
  findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
@@ -795,21 +879,49 @@ declare module 'mongoose' {
795
879
  id: mongodb.ObjectId | any,
796
880
  update: UpdateQuery<TRawDocType>,
797
881
  options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
798
- ): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate', TInstanceMethods & TVirtuals>;
882
+ ): QueryWithHelpers<
883
+ HasLeanOption<TSchema> extends true ? ModifyResult<TRawDocType> : ModifyResult<ResultDoc>,
884
+ ResultDoc,
885
+ TQueryHelpers,
886
+ TRawDocType,
887
+ 'findOneAndUpdate',
888
+ TInstanceMethods & TVirtuals
889
+ >;
799
890
  findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
800
891
  id: mongodb.ObjectId | any,
801
892
  update: UpdateQuery<TRawDocType>,
802
893
  options: QueryOptions<TRawDocType> & { upsert: true } & ReturnsNewDoc
803
- ): QueryWithHelpers<ResultDoc, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate', TInstanceMethods & TVirtuals>;
894
+ ): QueryWithHelpers<
895
+ HasLeanOption<TSchema> extends true ? TRawDocType : ResultDoc,
896
+ ResultDoc,
897
+ TQueryHelpers,
898
+ TRawDocType,
899
+ 'findOneAndUpdate',
900
+ TInstanceMethods & TVirtuals
901
+ >;
804
902
  findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
805
903
  id?: mongodb.ObjectId | any,
806
904
  update?: UpdateQuery<TRawDocType>,
807
905
  options?: QueryOptions<TRawDocType> | null
808
- ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate', TInstanceMethods & TVirtuals>;
906
+ ): QueryWithHelpers<
907
+ HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
908
+ ResultDoc,
909
+ TQueryHelpers,
910
+ TRawDocType,
911
+ 'findOneAndUpdate',
912
+ TInstanceMethods & TVirtuals
913
+ >;
809
914
  findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
810
915
  id: mongodb.ObjectId | any,
811
916
  update: UpdateQuery<TRawDocType>
812
- ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate', TInstanceMethods & TVirtuals>;
917
+ ): QueryWithHelpers<
918
+ HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
919
+ ResultDoc,
920
+ TQueryHelpers,
921
+ TRawDocType,
922
+ 'findOneAndUpdate',
923
+ TInstanceMethods & TVirtuals
924
+ >;
813
925
 
814
926
  /** Creates a `findOneAndDelete` query: atomically finds the given document, deletes it, and returns the document as it was before deletion. */
815
927
  findOneAndDelete<ResultDoc = THydratedDocumentType>(
@@ -826,11 +938,25 @@ declare module 'mongoose' {
826
938
  findOneAndDelete<ResultDoc = THydratedDocumentType>(
827
939
  filter: RootFilterQuery<TRawDocType>,
828
940
  options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
829
- ): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete', TInstanceMethods & TVirtuals>;
941
+ ): QueryWithHelpers<
942
+ HasLeanOption<TSchema> extends true ? ModifyResult<TRawDocType> : ModifyResult<ResultDoc>,
943
+ ResultDoc,
944
+ TQueryHelpers,
945
+ TRawDocType,
946
+ 'findOneAndDelete',
947
+ TInstanceMethods & TVirtuals
948
+ >;
830
949
  findOneAndDelete<ResultDoc = THydratedDocumentType>(
831
950
  filter?: RootFilterQuery<TRawDocType> | null,
832
951
  options?: QueryOptions<TRawDocType> | null
833
- ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete', TInstanceMethods & TVirtuals>;
952
+ ): QueryWithHelpers<
953
+ HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
954
+ ResultDoc,
955
+ TQueryHelpers,
956
+ TRawDocType,
957
+ 'findOneAndDelete',
958
+ TInstanceMethods & TVirtuals
959
+ >;
834
960
 
835
961
  /** Creates a `findOneAndReplace` query: atomically finds the given document and replaces it with `replacement`. */
836
962
  findOneAndReplace<ResultDoc = THydratedDocumentType>(
@@ -849,17 +975,38 @@ declare module 'mongoose' {
849
975
  filter: RootFilterQuery<TRawDocType>,
850
976
  replacement: TRawDocType | AnyObject,
851
977
  options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
852
- ): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace', TInstanceMethods & TVirtuals>;
978
+ ): QueryWithHelpers<
979
+ HasLeanOption<TSchema> extends true ? ModifyResult<TRawDocType> : ModifyResult<ResultDoc>,
980
+ ResultDoc,
981
+ TQueryHelpers,
982
+ TRawDocType,
983
+ 'findOneAndReplace',
984
+ TInstanceMethods & TVirtuals
985
+ >;
853
986
  findOneAndReplace<ResultDoc = THydratedDocumentType>(
854
987
  filter: RootFilterQuery<TRawDocType>,
855
988
  replacement: TRawDocType | AnyObject,
856
989
  options: QueryOptions<TRawDocType> & { upsert: true } & ReturnsNewDoc
857
- ): QueryWithHelpers<ResultDoc, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace', TInstanceMethods & TVirtuals>;
990
+ ): QueryWithHelpers<
991
+ HasLeanOption<TSchema> extends true ? TRawDocType : ResultDoc,
992
+ ResultDoc,
993
+ TQueryHelpers,
994
+ TRawDocType,
995
+ 'findOneAndReplace',
996
+ TInstanceMethods & TVirtuals
997
+ >;
858
998
  findOneAndReplace<ResultDoc = THydratedDocumentType>(
859
999
  filter?: RootFilterQuery<TRawDocType>,
860
1000
  replacement?: TRawDocType | AnyObject,
861
1001
  options?: QueryOptions<TRawDocType> | null
862
- ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace', TInstanceMethods & TVirtuals>;
1002
+ ): QueryWithHelpers<
1003
+ HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
1004
+ ResultDoc,
1005
+ TQueryHelpers,
1006
+ TRawDocType,
1007
+ 'findOneAndReplace',
1008
+ TInstanceMethods & TVirtuals
1009
+ >;
863
1010
 
864
1011
  /** Creates a `findOneAndUpdate` query: atomically find the first document that matches `filter` and apply `update`. */
865
1012
  findOneAndUpdate<ResultDoc = THydratedDocumentType>(
@@ -890,17 +1037,38 @@ declare module 'mongoose' {
890
1037
  filter: RootFilterQuery<TRawDocType>,
891
1038
  update: UpdateQuery<TRawDocType>,
892
1039
  options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
893
- ): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate', TInstanceMethods & TVirtuals>;
1040
+ ): QueryWithHelpers<
1041
+ HasLeanOption<TSchema> extends true ? ModifyResult<TRawDocType> : ModifyResult<ResultDoc>,
1042
+ ResultDoc,
1043
+ TQueryHelpers,
1044
+ TRawDocType,
1045
+ 'findOneAndUpdate',
1046
+ TInstanceMethods & TVirtuals
1047
+ >;
894
1048
  findOneAndUpdate<ResultDoc = THydratedDocumentType>(
895
1049
  filter: RootFilterQuery<TRawDocType>,
896
1050
  update: UpdateQuery<TRawDocType>,
897
1051
  options: QueryOptions<TRawDocType> & { upsert: true } & ReturnsNewDoc
898
- ): QueryWithHelpers<ResultDoc, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate', TInstanceMethods & TVirtuals>;
1052
+ ): QueryWithHelpers<
1053
+ HasLeanOption<TSchema> extends true ? TRawDocType : ResultDoc,
1054
+ ResultDoc,
1055
+ TQueryHelpers,
1056
+ TRawDocType,
1057
+ 'findOneAndUpdate',
1058
+ TInstanceMethods & TVirtuals
1059
+ >;
899
1060
  findOneAndUpdate<ResultDoc = THydratedDocumentType>(
900
1061
  filter?: RootFilterQuery<TRawDocType>,
901
1062
  update?: UpdateQuery<TRawDocType>,
902
1063
  options?: QueryOptions<TRawDocType> | null
903
- ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate', TInstanceMethods & TVirtuals>;
1064
+ ): QueryWithHelpers<
1065
+ HasLeanOption<TSchema> extends true ? TRawDocType | null : ResultDoc | null,
1066
+ ResultDoc,
1067
+ TQueryHelpers,
1068
+ TRawDocType,
1069
+ 'findOneAndUpdate',
1070
+ TInstanceMethods & TVirtuals
1071
+ >;
904
1072
 
905
1073
  /** Creates a `replaceOne` query: finds the first document that matches `filter` and replaces it with `replacement`. */
906
1074
  replaceOne<ResultDoc = THydratedDocumentType>(
@@ -936,9 +1104,9 @@ declare module 'mongoose' {
936
1104
  where<ResultDoc = THydratedDocumentType>(
937
1105
  path: string,
938
1106
  val?: any
939
- ): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'find', TInstanceMethods>;
1107
+ ): QueryWithHelpers<HasLeanOption<TSchema> extends true ? TRawDocType[] : ResultDoc[], ResultDoc, TQueryHelpers, TRawDocType, 'find', TInstanceMethods>;
940
1108
  where<ResultDoc = THydratedDocumentType>(obj: object): QueryWithHelpers<
941
- Array<ResultDoc>,
1109
+ HasLeanOption<TSchema> extends true ? TRawDocType[] : ResultDoc[],
942
1110
  ResultDoc,
943
1111
  TQueryHelpers,
944
1112
  TRawDocType,
@@ -946,7 +1114,7 @@ declare module 'mongoose' {
946
1114
  TInstanceMethods & TVirtuals
947
1115
  >;
948
1116
  where<ResultDoc = THydratedDocumentType>(): QueryWithHelpers<
949
- Array<ResultDoc>,
1117
+ HasLeanOption<TSchema> extends true ? TRawDocType[] : ResultDoc[],
950
1118
  ResultDoc,
951
1119
  TQueryHelpers,
952
1120
  TRawDocType,
package/types/query.d.ts CHANGED
@@ -10,11 +10,9 @@ declare module 'mongoose' {
10
10
  * { age: { $gte: 30 } }
11
11
  * ```
12
12
  */
13
- type RootFilterQuery<T> = FilterQuery<T> | Query<any, any> | Types.ObjectId;
13
+ type RootFilterQuery<T> = IsItRecordAndNotAny<T> extends true ? FilterQuery<T> | Query<any, any> | Types.ObjectId : FilterQuery<Record<string, any>> | Query<any, any> | Types.ObjectId;
14
14
 
15
- type FilterQuery<T> = {
16
- [P in keyof T]?: Condition<T[P]>;
17
- } & RootQuerySelector<T> & { _id?: Condition<string>; };
15
+ type FilterQuery<T> = ({ [P in keyof T]?: Condition<T[P]>; } & RootQuerySelector<T> & { _id?: Condition<string>; });
18
16
 
19
17
  type MongooseBaseQueryOptionKeys =
20
18
  | 'context'
@@ -120,6 +118,17 @@ declare module 'mongoose' {
120
118
  updatedAt?: boolean;
121
119
  }
122
120
 
121
+ // Options that can be passed to Query.prototype.lean()
122
+ interface LeanOptions {
123
+ // Set to false to strip out the version key
124
+ versionKey?: boolean;
125
+ // Transform the result document in place. `doc` is the raw document being transformed.
126
+ // Typed as `Record<string, unknown>` because TypeScript gets confused when handling Document.prototype.deleteOne()
127
+ // and other document methods that try to infer the raw doc type from the Document class.
128
+ transform?: (doc: Record<string, unknown>) => void;
129
+ [key: string]: any;
130
+ }
131
+
123
132
  interface QueryOptions<DocType = unknown> extends
124
133
  PopulateOption,
125
134
  SessionOption {
@@ -134,7 +143,7 @@ declare module 'mongoose' {
134
143
  /**
135
144
  * If truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document.
136
145
  */
137
- lean?: boolean | Record<string, any>;
146
+ lean?: boolean | LeanOptions;
138
147
  limit?: number;
139
148
  maxTimeMS?: number;
140
149
  multi?: boolean;
@@ -231,7 +240,7 @@ declare module 'mongoose' {
231
240
  : MergeType<ResultType, Paths>;
232
241
 
233
242
  class Query<ResultType, DocType, THelpers = {}, RawDocType = unknown, QueryOp = 'find', TDocOverrides = Record<string, never>> implements SessionOperation {
234
- _mongooseOptions: QueryOptions<DocType>;
243
+ _mongooseOptions: QueryOptions<RawDocType>;
235
244
 
236
245
  /**
237
246
  * Returns a wrapper around a [mongodb driver cursor](https://mongodb.github.io/node-mongodb-native/4.9/classes/FindCursor.html).
@@ -310,14 +319,14 @@ declare module 'mongoose' {
310
319
  /** Specifies this query as a `countDocuments` query. */
311
320
  countDocuments(
312
321
  criteria?: RootFilterQuery<RawDocType>,
313
- options?: QueryOptions<DocType>
322
+ options?: QueryOptions<RawDocType>
314
323
  ): QueryWithHelpers<number, DocType, THelpers, RawDocType, 'countDocuments', TDocOverrides>;
315
324
 
316
325
  /**
317
326
  * Returns a wrapper around a [mongodb driver cursor](https://mongodb.github.io/node-mongodb-native/4.9/classes/FindCursor.html).
318
327
  * A QueryCursor exposes a Streams3 interface, as well as a `.next()` function.
319
328
  */
320
- cursor(options?: QueryOptions<DocType>): Cursor<Unpacked<ResultType>, QueryOptions<DocType>>;
329
+ cursor(options?: QueryOptions<RawDocType>): Cursor<Unpacked<ResultType>, QueryOptions<RawDocType>>;
321
330
 
322
331
  /**
323
332
  * Declare and/or execute this query as a `deleteMany()` operation. Works like
@@ -326,7 +335,7 @@ declare module 'mongoose' {
326
335
  */
327
336
  deleteMany(
328
337
  filter?: RootFilterQuery<RawDocType>,
329
- options?: QueryOptions<DocType>
338
+ options?: QueryOptions<RawDocType>
330
339
  ): QueryWithHelpers<any, DocType, THelpers, RawDocType, 'deleteMany', TDocOverrides>;
331
340
  deleteMany(filter: RootFilterQuery<RawDocType>): QueryWithHelpers<
332
341
  any,
@@ -345,7 +354,7 @@ declare module 'mongoose' {
345
354
  */
346
355
  deleteOne(
347
356
  filter?: RootFilterQuery<RawDocType>,
348
- options?: QueryOptions<DocType>
357
+ options?: QueryOptions<RawDocType>
349
358
  ): QueryWithHelpers<any, DocType, THelpers, RawDocType, 'deleteOne', TDocOverrides>;
350
359
  deleteOne(filter: RootFilterQuery<RawDocType>): QueryWithHelpers<
351
360
  any,
@@ -361,7 +370,7 @@ declare module 'mongoose' {
361
370
  distinct<DocKey extends string, ResultType = unknown>(
362
371
  field: DocKey,
363
372
  filter?: RootFilterQuery<RawDocType>,
364
- options?: QueryOptions<DocType>
373
+ options?: QueryOptions<RawDocType>
365
374
  ): QueryWithHelpers<
366
375
  Array<
367
376
  DocKey extends keyof WithLevel1NestedPaths<DocType>
@@ -390,7 +399,7 @@ declare module 'mongoose' {
390
399
  equals(val: any): this;
391
400
 
392
401
  /** Creates a `estimatedDocumentCount` query: counts the number of documents in the collection. */
393
- estimatedDocumentCount(options?: QueryOptions<DocType>): QueryWithHelpers<
402
+ estimatedDocumentCount(options?: QueryOptions<RawDocType>): QueryWithHelpers<
394
403
  number,
395
404
  DocType,
396
405
  THelpers,
@@ -413,54 +422,39 @@ declare module 'mongoose' {
413
422
 
414
423
  /** Creates a `find` query: gets a list of documents that match `filter`. */
415
424
  find(
416
- filter: RootFilterQuery<RawDocType>,
425
+ filter?: RootFilterQuery<RawDocType>,
417
426
  projection?: ProjectionType<RawDocType> | null,
418
- options?: QueryOptions<DocType> | null
419
- ): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType, 'find', TDocOverrides>;
420
- find(
421
- filter: RootFilterQuery<RawDocType>,
422
- projection?: ProjectionType<RawDocType> | null
423
- ): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType, 'find', TDocOverrides>;
424
- find(
425
- filter: RootFilterQuery<RawDocType>
427
+ options?: QueryOptions<RawDocType> | null
426
428
  ): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType, 'find', TDocOverrides>;
427
- find(): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType, 'find', TDocOverrides>;
428
429
 
429
430
  /** Declares the query a findOne operation. When executed, returns the first found document. */
430
431
  findOne(
431
432
  filter?: RootFilterQuery<RawDocType>,
432
433
  projection?: ProjectionType<RawDocType> | null,
433
- options?: QueryOptions<DocType> | null
434
- ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne', TDocOverrides>;
435
- findOne(
436
- filter?: RootFilterQuery<RawDocType>,
437
- projection?: ProjectionType<RawDocType> | null
438
- ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne', TDocOverrides>;
439
- findOne(
440
- filter?: RootFilterQuery<RawDocType>
434
+ options?: QueryOptions<RawDocType> | null
441
435
  ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne', TDocOverrides>;
442
436
 
443
437
  /** Creates a `findOneAndDelete` query: atomically finds the given document, deletes it, and returns the document as it was before deletion. */
444
438
  findOneAndDelete(
445
439
  filter?: RootFilterQuery<RawDocType>,
446
- options?: QueryOptions<DocType> | null
440
+ options?: QueryOptions<RawDocType> | null
447
441
  ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOneAndDelete'>;
448
442
 
449
443
  /** Creates a `findOneAndUpdate` query: atomically find the first document that matches `filter` and apply `update`. */
450
444
  findOneAndUpdate(
451
445
  filter: RootFilterQuery<RawDocType>,
452
446
  update: UpdateQuery<RawDocType>,
453
- options: QueryOptions<DocType> & { includeResultMetadata: true }
447
+ options: QueryOptions<RawDocType> & { includeResultMetadata: true }
454
448
  ): QueryWithHelpers<ModifyResult<DocType>, DocType, THelpers, RawDocType, 'findOneAndUpdate', TDocOverrides>;
455
449
  findOneAndUpdate(
456
450
  filter: RootFilterQuery<RawDocType>,
457
451
  update: UpdateQuery<RawDocType>,
458
- options: QueryOptions<DocType> & { upsert: true } & ReturnsNewDoc
452
+ options: QueryOptions<RawDocType> & { upsert: true } & ReturnsNewDoc
459
453
  ): QueryWithHelpers<DocType, DocType, THelpers, RawDocType, 'findOneAndUpdate', TDocOverrides>;
460
454
  findOneAndUpdate(
461
455
  filter: RootFilterQuery<RawDocType>,
462
456
  update: UpdateQuery<RawDocType>,
463
- options?: QueryOptions<DocType> | null
457
+ options?: QueryOptions<RawDocType> | null
464
458
  ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOneAndUpdate', TDocOverrides>;
465
459
  findOneAndUpdate(
466
460
  update: UpdateQuery<RawDocType>
@@ -471,41 +465,34 @@ declare module 'mongoose' {
471
465
  findById(
472
466
  id: mongodb.ObjectId | any,
473
467
  projection?: ProjectionType<RawDocType> | null,
474
- options?: QueryOptions<DocType> | null
475
- ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne', TDocOverrides>;
476
- findById(
477
- id: mongodb.ObjectId | any,
478
- projection?: ProjectionType<RawDocType> | null
479
- ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne', TDocOverrides>;
480
- findById(
481
- id: mongodb.ObjectId | any
468
+ options?: QueryOptions<RawDocType> | null
482
469
  ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne', TDocOverrides>;
483
470
 
484
471
  /** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */
485
472
  findByIdAndDelete(
486
473
  id: mongodb.ObjectId | any,
487
- options: QueryOptions<DocType> & { includeResultMetadata: true }
474
+ options: QueryOptions<RawDocType> & { includeResultMetadata: true }
488
475
  ): QueryWithHelpers<ModifyResult<DocType>, DocType, THelpers, RawDocType, 'findOneAndDelete', TDocOverrides>;
489
476
  findByIdAndDelete(
490
477
  id?: mongodb.ObjectId | any,
491
- options?: QueryOptions<DocType> | null
478
+ options?: QueryOptions<RawDocType> | null
492
479
  ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOneAndDelete', TDocOverrides>;
493
480
 
494
481
  /** Creates a `findOneAndUpdate` query, filtering by the given `_id`. */
495
482
  findByIdAndUpdate(
496
483
  id: mongodb.ObjectId | any,
497
484
  update: UpdateQuery<RawDocType>,
498
- options: QueryOptions<DocType> & { includeResultMetadata: true }
485
+ options: QueryOptions<RawDocType> & { includeResultMetadata: true }
499
486
  ): QueryWithHelpers<any, DocType, THelpers, RawDocType, 'findOneAndUpdate', TDocOverrides>;
500
487
  findByIdAndUpdate(
501
488
  id: mongodb.ObjectId | any,
502
489
  update: UpdateQuery<RawDocType>,
503
- options: QueryOptions<DocType> & { upsert: true } & ReturnsNewDoc
490
+ options: QueryOptions<RawDocType> & { upsert: true } & ReturnsNewDoc
504
491
  ): QueryWithHelpers<DocType, DocType, THelpers, RawDocType, 'findOneAndUpdate', TDocOverrides>;
505
492
  findByIdAndUpdate(
506
493
  id?: mongodb.ObjectId | any,
507
494
  update?: UpdateQuery<RawDocType>,
508
- options?: QueryOptions<DocType> | null
495
+ options?: QueryOptions<RawDocType> | null
509
496
  ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOneAndUpdate', TDocOverrides>;
510
497
  findByIdAndUpdate(
511
498
  id: mongodb.ObjectId | any,
@@ -526,7 +513,7 @@ declare module 'mongoose' {
526
513
  getFilter(): FilterQuery<RawDocType>;
527
514
 
528
515
  /** Gets query options. */
529
- getOptions(): QueryOptions<DocType>;
516
+ getOptions(): QueryOptions<RawDocType>;
530
517
 
531
518
  /** Gets a list of paths to be populated by this query */
532
519
  getPopulatedPaths(): Array<string>;
@@ -559,8 +546,18 @@ declare module 'mongoose' {
559
546
  j(val: boolean | null): this;
560
547
 
561
548
  /** Sets the lean option. */
549
+ lean(): QueryWithHelpers<
550
+ ResultType extends null
551
+ ? GetLeanResultType<RawDocType, ResultType, QueryOp> | null
552
+ : GetLeanResultType<RawDocType, ResultType, QueryOp>,
553
+ DocType,
554
+ THelpers,
555
+ RawDocType,
556
+ QueryOp,
557
+ TDocOverrides
558
+ >;
562
559
  lean(
563
- val?: boolean | any
560
+ val: true | LeanOptions
564
561
  ): QueryWithHelpers<
565
562
  ResultType extends null
566
563
  ? GetLeanResultType<RawDocType, ResultType, QueryOp> | null
@@ -571,8 +568,32 @@ declare module 'mongoose' {
571
568
  QueryOp,
572
569
  TDocOverrides
573
570
  >;
571
+ lean(
572
+ val: false
573
+ ): QueryWithHelpers<
574
+ ResultType extends AnyArray<any>
575
+ ? DocType[]
576
+ : ResultType extends null
577
+ ? DocType | null
578
+ : DocType,
579
+ DocType,
580
+ THelpers,
581
+ RawDocType,
582
+ QueryOp,
583
+ TDocOverrides
584
+ >;
585
+ lean<LeanResultType>(): QueryWithHelpers<
586
+ ResultType extends null
587
+ ? LeanResultType | null
588
+ : LeanResultType,
589
+ DocType,
590
+ THelpers,
591
+ RawDocType,
592
+ QueryOp,
593
+ TDocOverrides
594
+ >;
574
595
  lean<LeanResultType>(
575
- val?: boolean | any
596
+ val: boolean | LeanOptions
576
597
  ): QueryWithHelpers<
577
598
  ResultType extends null
578
599
  ? LeanResultType | null
@@ -626,7 +647,7 @@ declare module 'mongoose' {
626
647
  * Getter/setter around the current mongoose-specific options for this query
627
648
  * Below are the current Mongoose-specific options.
628
649
  */
629
- mongooseOptions(val?: QueryOptions<DocType>): QueryOptions<DocType>;
650
+ mongooseOptions(val?: QueryOptions<RawDocType>): QueryOptions<RawDocType>;
630
651
 
631
652
  /** Specifies a `$ne` query condition. When called with one argument, the most recent path passed to `where()` is used. */
632
653
  ne<K = string>(path: K, val: any): this;
@@ -734,7 +755,7 @@ declare module 'mongoose' {
734
755
  replaceOne(
735
756
  filter?: RootFilterQuery<RawDocType>,
736
757
  replacement?: DocType | AnyObject,
737
- options?: QueryOptions<DocType> | null
758
+ options?: QueryOptions<RawDocType> | null
738
759
  ): QueryWithHelpers<any, DocType, THelpers, RawDocType, 'replaceOne', TDocOverrides>;
739
760
 
740
761
  /**
@@ -800,7 +821,7 @@ declare module 'mongoose' {
800
821
  set(path: string | Record<string, unknown>, value?: any): this;
801
822
 
802
823
  /** Sets query options. Some options only make sense for certain operations. */
803
- setOptions(options: QueryOptions<DocType>, overwrite?: boolean): this;
824
+ setOptions(options: QueryOptions<RawDocType>, overwrite?: boolean): this;
804
825
 
805
826
  /** Sets the query conditions to the provided JSON object. */
806
827
  setQuery(val: FilterQuery<RawDocType> | null): void;
@@ -848,7 +869,7 @@ declare module 'mongoose' {
848
869
  updateMany(
849
870
  filter: RootFilterQuery<RawDocType>,
850
871
  update: UpdateQuery<RawDocType> | UpdateWithAggregationPipeline,
851
- options?: QueryOptions<DocType> | null
872
+ options?: QueryOptions<RawDocType> | null
852
873
  ): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType, 'updateMany', TDocOverrides>;
853
874
  updateMany(
854
875
  update: UpdateQuery<RawDocType> | UpdateWithAggregationPipeline
@@ -861,7 +882,7 @@ declare module 'mongoose' {
861
882
  updateOne(
862
883
  filter: RootFilterQuery<RawDocType>,
863
884
  update: UpdateQuery<RawDocType> | UpdateWithAggregationPipeline,
864
- options?: QueryOptions<DocType> | null
885
+ options?: QueryOptions<RawDocType> | null
865
886
  ): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType, 'updateOne', TDocOverrides>;
866
887
  updateOne(
867
888
  update: UpdateQuery<RawDocType> | UpdateWithAggregationPipeline
@@ -58,6 +58,9 @@ declare module 'mongoose' {
58
58
  /** Arbitrary options passed to `createCollection()` */
59
59
  collectionOptions?: mongodb.CreateCollectionOptions;
60
60
 
61
+ /** Default lean options for queries */
62
+ lean?: boolean | LeanOptions;
63
+
61
64
  /** The timeseries option to use when creating the model's collection. */
62
65
  timeseries?: mongodb.TimeSeriesCollectionOptions;
63
66