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
@@ -217,11 +217,12 @@ declare module 'mongoose' {
217
217
  translateAliases?: boolean;
218
218
 
219
219
  /**
220
- * Mongoose queries currently store an `_executionStack` property that stores the stack trace
221
- * of where the query was originally executed for debugging `Query was already executed` errors.
222
- * This behavior can cause performance issues with bundlers and source maps. Set this option to
223
- * `true` to disable Mongoose query stack trace collection.
220
+ * If `true`, allows passing update pipelines (arrays) to update operations by default
221
+ * without explicitly setting `updatePipeline: true` in each query. This is the global
222
+ * default for the `updatePipeline` query option.
223
+ *
224
+ * @default false
224
225
  */
225
- skipOriginalStackTraces?: boolean;
226
+ updatePipeline?: boolean;
226
227
  }
227
228
  }
@@ -184,7 +184,7 @@ declare module 'mongoose' {
184
184
 
185
185
  export interface Match {
186
186
  /** [`$match` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match/) */
187
- $match: FilterQuery<any>;
187
+ $match: QueryFilter<any>;
188
188
  }
189
189
 
190
190
  export interface Merge {
package/types/query.d.ts CHANGED
@@ -1,18 +1,27 @@
1
1
  declare module 'mongoose' {
2
2
  import mongodb = require('mongodb');
3
3
 
4
- export type Condition<T> = T | QuerySelector<T | any> | any;
5
-
6
- /**
7
- * Filter query to select the documents that match the query
8
- * @example
9
- * ```js
10
- * { age: { $gte: 30 } }
11
- * ```
12
- */
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
-
15
- type FilterQuery<T> = ({ [P in keyof T]?: Condition<T[P]>; } & RootQuerySelector<T> & { _id?: Condition<string>; });
4
+ type StringQueryTypeCasting = string | RegExp;
5
+ type ObjectIdQueryTypeCasting = Types.ObjectId | string;
6
+ type DateQueryTypeCasting = string | number | NativeDate;
7
+ type UUIDQueryTypeCasting = Types.UUID | string;
8
+ type BufferQueryCasting = Buffer | mongodb.Binary | number[] | string | { $binary: string | mongodb.Binary };
9
+ type QueryTypeCasting<T> = T extends string
10
+ ? StringQueryTypeCasting
11
+ : T extends Types.ObjectId
12
+ ? ObjectIdQueryTypeCasting
13
+ : T extends Types.UUID
14
+ ? UUIDQueryTypeCasting
15
+ : T extends Buffer
16
+ ? BufferQueryCasting
17
+ : T extends NativeDate
18
+ ? DateQueryTypeCasting
19
+ : T;
20
+
21
+ export type ApplyBasicQueryCasting<T> = QueryTypeCasting<T> | QueryTypeCasting<T[]> | (T extends (infer U)[] ? QueryTypeCasting<U> : T) | null;
22
+
23
+ type _QueryFilter<T> = ({ [P in keyof T]?: mongodb.Condition<ApplyBasicQueryCasting<T[P]>>; } & mongodb.RootFilterOperators<{ [P in keyof T]?: ApplyBasicQueryCasting<T[P]>; }>) | Query<any, any>;
24
+ type QueryFilter<T> = IsItRecordAndNotAny<T> extends true ? _QueryFilter<WithLevel1NestedPaths<T>> : _QueryFilter<Record<string, any>>;
16
25
 
17
26
  type MongooseBaseQueryOptionKeys =
18
27
  | 'context'
@@ -27,7 +36,8 @@ declare module 'mongoose' {
27
36
  | 'setDefaultsOnInsert'
28
37
  | 'strict'
29
38
  | 'strictQuery'
30
- | 'translateAliases';
39
+ | 'translateAliases'
40
+ | 'updatePipeline';
31
41
 
32
42
  type MongooseBaseQueryOptions<DocType = unknown> = Pick<QueryOptions<DocType>, MongooseBaseQueryOptionKeys | 'timestamps' | 'lean'> & {
33
43
  [other: string]: any;
@@ -46,73 +56,6 @@ declare module 'mongoose' {
46
56
  TDocOverrides = Record<string, never>
47
57
  > = Query<ResultType, DocType, THelpers, RawDocType, QueryOp, TDocOverrides> & THelpers;
48
58
 
49
- type QuerySelector<T> = {
50
- // Comparison
51
- $eq?: T;
52
- $gt?: T;
53
- $gte?: T;
54
- $in?: [T] extends AnyArray<any> ? Unpacked<T>[] : T[];
55
- $lt?: T;
56
- $lte?: T;
57
- $ne?: T;
58
- $nin?: [T] extends AnyArray<any> ? Unpacked<T>[] : T[];
59
- // Logical
60
- $not?: T extends string ? QuerySelector<T> | RegExp : QuerySelector<T>;
61
- // Element
62
- /**
63
- * When `true`, `$exists` matches the documents that contain the field,
64
- * including documents where the field value is null.
65
- */
66
- $exists?: boolean;
67
- $type?: string | number;
68
- // Evaluation
69
- $expr?: any;
70
- $jsonSchema?: any;
71
- $mod?: T extends number ? [number, number] : never;
72
- $regex?: T extends string ? RegExp | string : never;
73
- $options?: T extends string ? string : never;
74
- // Geospatial
75
- // TODO: define better types for geo queries
76
- $geoIntersects?: { $geometry: object };
77
- $geoWithin?: object;
78
- $near?: object;
79
- $nearSphere?: object;
80
- $maxDistance?: number;
81
- // Array
82
- // TODO: define better types for $all and $elemMatch
83
- $all?: T extends AnyArray<any> ? any[] : never;
84
- $elemMatch?: T extends AnyArray<any> ? object : never;
85
- $size?: T extends AnyArray<any> ? number : never;
86
- // Bitwise
87
- $bitsAllClear?: number | mongodb.Binary | number[];
88
- $bitsAllSet?: number | mongodb.Binary | number[];
89
- $bitsAnyClear?: number | mongodb.Binary | number[];
90
- $bitsAnySet?: number | mongodb.Binary | number[];
91
- };
92
-
93
- type RootQuerySelector<T> = {
94
- /** @see https://www.mongodb.com/docs/manual/reference/operator/query/and/#op._S_and */
95
- $and?: Array<FilterQuery<T>>;
96
- /** @see https://www.mongodb.com/docs/manual/reference/operator/query/nor/#op._S_nor */
97
- $nor?: Array<FilterQuery<T>>;
98
- /** @see https://www.mongodb.com/docs/manual/reference/operator/query/or/#op._S_or */
99
- $or?: Array<FilterQuery<T>>;
100
- /** @see https://www.mongodb.com/docs/manual/reference/operator/query/text */
101
- $text?: {
102
- $search: string;
103
- $language?: string;
104
- $caseSensitive?: boolean;
105
- $diacriticSensitive?: boolean;
106
- };
107
- /** @see https://www.mongodb.com/docs/manual/reference/operator/query/where/#op._S_where */
108
- $where?: string | Function;
109
- /** @see https://www.mongodb.com/docs/manual/reference/operator/query/comment/#op._S_comment */
110
- $comment?: string;
111
- $expr?: Record<string, any>;
112
- // this will mark all unrecognized properties as any (including nested queries)
113
- [key: string]: any;
114
- };
115
-
116
59
  interface QueryTimestampsConfig {
117
60
  createdAt?: boolean;
118
61
  updatedAt?: boolean;
@@ -132,7 +75,7 @@ declare module 'mongoose' {
132
75
  interface QueryOptions<DocType = unknown> extends
133
76
  PopulateOption,
134
77
  SessionOption {
135
- arrayFilters?: { [key: string]: any }[];
78
+ arrayFilters?: AnyObject[];
136
79
  batchSize?: number;
137
80
  collation?: mongodb.CollationOptions;
138
81
  comment?: any;
@@ -161,7 +104,7 @@ declare module 'mongoose' {
161
104
  * Set `overwriteImmutable` to `true` to allow updating immutable properties using other update operators.
162
105
  */
163
106
  overwriteImmutable?: boolean;
164
- projection?: { [P in keyof DocType]?: number | string } | AnyObject | string;
107
+ projection?: AnyObject | string;
165
108
  /**
166
109
  * if true, returns the full ModifyResult rather than just the document
167
110
  */
@@ -216,6 +159,11 @@ declare module 'mongoose' {
216
159
  translateAliases?: boolean;
217
160
  upsert?: boolean;
218
161
  useBigInt64?: boolean;
162
+ /**
163
+ * Set to true to allow passing in an update pipeline instead of an update document.
164
+ * Mongoose disallows update pipelines by default because Mongoose does not cast update pipelines.
165
+ */
166
+ updatePipeline?: boolean;
219
167
  writeConcern?: mongodb.WriteConcern;
220
168
 
221
169
  [other: string]: any;
@@ -224,7 +172,7 @@ declare module 'mongoose' {
224
172
  type QueryOpThatReturnsDocument = 'find' | 'findOne' | 'findOneAndUpdate' | 'findOneAndReplace' | 'findOneAndDelete';
225
173
 
226
174
  type GetLeanResultType<RawDocType, ResultType, QueryOp> = QueryOp extends QueryOpThatReturnsDocument
227
- ? (ResultType extends any[] ? Default__v<Require_id<FlattenMaps<BufferToBinary<RawDocType>>>>[] : Default__v<Require_id<FlattenMaps<BufferToBinary<RawDocType>>>>)
175
+ ? (ResultType extends any[] ? Default__v<Require_id<RawDocType>>[] : Default__v<Require_id<RawDocType>>)
228
176
  : ResultType;
229
177
 
230
178
  type MergePopulatePaths<RawDocType, ResultType, QueryOp, Paths, TQueryHelpers, TDocOverrides = Record<string, never>> = QueryOp extends QueryOpThatReturnsDocument
@@ -269,7 +217,7 @@ declare module 'mongoose' {
269
217
  allowDiskUse(value: boolean): this;
270
218
 
271
219
  /** Specifies arguments for an `$and` condition. */
272
- and(array: FilterQuery<RawDocType>[]): this;
220
+ and(array: QueryFilter<RawDocType>[]): this;
273
221
 
274
222
  /** Specifies the batchSize option. */
275
223
  batchSize(val: number): this;
@@ -318,7 +266,7 @@ declare module 'mongoose' {
318
266
 
319
267
  /** Specifies this query as a `countDocuments` query. */
320
268
  countDocuments(
321
- criteria?: RootFilterQuery<RawDocType>,
269
+ criteria?: QueryFilter<RawDocType>,
322
270
  options?: QueryOptions<RawDocType>
323
271
  ): QueryWithHelpers<number, DocType, THelpers, RawDocType, 'countDocuments', TDocOverrides>;
324
272
 
@@ -334,10 +282,10 @@ declare module 'mongoose' {
334
282
  * collection, regardless of the value of `single`.
335
283
  */
336
284
  deleteMany(
337
- filter?: RootFilterQuery<RawDocType>,
285
+ filter?: QueryFilter<RawDocType>,
338
286
  options?: QueryOptions<RawDocType>
339
287
  ): QueryWithHelpers<any, DocType, THelpers, RawDocType, 'deleteMany', TDocOverrides>;
340
- deleteMany(filter: RootFilterQuery<RawDocType>): QueryWithHelpers<
288
+ deleteMany(filter: QueryFilter<RawDocType>): QueryWithHelpers<
341
289
  any,
342
290
  DocType,
343
291
  THelpers,
@@ -353,10 +301,10 @@ declare module 'mongoose' {
353
301
  * option.
354
302
  */
355
303
  deleteOne(
356
- filter?: RootFilterQuery<RawDocType>,
304
+ filter?: QueryFilter<RawDocType>,
357
305
  options?: QueryOptions<RawDocType>
358
306
  ): QueryWithHelpers<any, DocType, THelpers, RawDocType, 'deleteOne', TDocOverrides>;
359
- deleteOne(filter: RootFilterQuery<RawDocType>): QueryWithHelpers<
307
+ deleteOne(filter: QueryFilter<RawDocType>): QueryWithHelpers<
360
308
  any,
361
309
  DocType,
362
310
  THelpers,
@@ -369,7 +317,7 @@ declare module 'mongoose' {
369
317
  /** Creates a `distinct` query: returns the distinct values of the given `field` that match `filter`. */
370
318
  distinct<DocKey extends string, ResultType = unknown>(
371
319
  field: DocKey,
372
- filter?: RootFilterQuery<RawDocType>,
320
+ filter?: QueryFilter<RawDocType>,
373
321
  options?: QueryOptions<RawDocType>
374
322
  ): QueryWithHelpers<
375
323
  Array<
@@ -385,7 +333,7 @@ declare module 'mongoose' {
385
333
  >;
386
334
 
387
335
  /** Specifies a `$elemMatch` query condition. When called with one argument, the most recent path passed to `where()` is used. */
388
- elemMatch<K = string>(path: K, val: any): this;
336
+ elemMatch(path: string, val: any): this;
389
337
  elemMatch(val: Function | any): this;
390
338
 
391
339
  /**
@@ -409,7 +357,7 @@ declare module 'mongoose' {
409
357
  >;
410
358
 
411
359
  /** Specifies a `$exists` query condition. When called with one argument, the most recent path passed to `where()` is used. */
412
- exists<K = string>(path: K, val: boolean): this;
360
+ exists(path: string, val: boolean): this;
413
361
  exists(val: boolean): this;
414
362
 
415
363
  /**
@@ -422,44 +370,40 @@ declare module 'mongoose' {
422
370
 
423
371
  /** Creates a `find` query: gets a list of documents that match `filter`. */
424
372
  find(
425
- filter?: RootFilterQuery<RawDocType>,
373
+ filter?: QueryFilter<RawDocType>,
426
374
  projection?: ProjectionType<RawDocType> | null,
427
375
  options?: QueryOptions<RawDocType> | null
428
376
  ): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType, 'find', TDocOverrides>;
429
377
 
430
378
  /** Declares the query a findOne operation. When executed, returns the first found document. */
431
379
  findOne(
432
- filter?: RootFilterQuery<RawDocType>,
380
+ filter?: QueryFilter<RawDocType>,
433
381
  projection?: ProjectionType<RawDocType> | null,
434
382
  options?: QueryOptions<RawDocType> | null
435
383
  ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne', TDocOverrides>;
436
384
 
437
385
  /** Creates a `findOneAndDelete` query: atomically finds the given document, deletes it, and returns the document as it was before deletion. */
438
386
  findOneAndDelete(
439
- filter?: RootFilterQuery<RawDocType>,
387
+ filter?: QueryFilter<RawDocType>,
440
388
  options?: QueryOptions<RawDocType> | null
441
389
  ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOneAndDelete'>;
442
390
 
443
391
  /** Creates a `findOneAndUpdate` query: atomically find the first document that matches `filter` and apply `update`. */
444
392
  findOneAndUpdate(
445
- filter: RootFilterQuery<RawDocType>,
393
+ filter: QueryFilter<RawDocType>,
446
394
  update: UpdateQuery<RawDocType>,
447
395
  options: QueryOptions<RawDocType> & { includeResultMetadata: true }
448
396
  ): QueryWithHelpers<ModifyResult<DocType>, DocType, THelpers, RawDocType, 'findOneAndUpdate', TDocOverrides>;
449
397
  findOneAndUpdate(
450
- filter: RootFilterQuery<RawDocType>,
398
+ filter: QueryFilter<RawDocType>,
451
399
  update: UpdateQuery<RawDocType>,
452
400
  options: QueryOptions<RawDocType> & { upsert: true } & ReturnsNewDoc
453
401
  ): QueryWithHelpers<DocType, DocType, THelpers, RawDocType, 'findOneAndUpdate', TDocOverrides>;
454
402
  findOneAndUpdate(
455
- filter: RootFilterQuery<RawDocType>,
456
- update: UpdateQuery<RawDocType>,
403
+ filter?: QueryFilter<RawDocType>,
404
+ update?: UpdateQuery<RawDocType>,
457
405
  options?: QueryOptions<RawDocType> | null
458
406
  ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOneAndUpdate', TDocOverrides>;
459
- findOneAndUpdate(
460
- update: UpdateQuery<RawDocType>
461
- ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOneAndUpdate', TDocOverrides>;
462
- findOneAndUpdate(): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOneAndUpdate', TDocOverrides>;
463
407
 
464
408
  /** Declares the query a findById operation. When executed, returns the document with the given `_id`. */
465
409
  findById(
@@ -494,10 +438,6 @@ declare module 'mongoose' {
494
438
  update?: UpdateQuery<RawDocType>,
495
439
  options?: QueryOptions<RawDocType> | null
496
440
  ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOneAndUpdate', TDocOverrides>;
497
- findByIdAndUpdate(
498
- id: mongodb.ObjectId | any,
499
- update: UpdateQuery<RawDocType>
500
- ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOneAndUpdate', TDocOverrides>;
501
441
 
502
442
  /** Specifies a `$geometry` condition */
503
443
  geometry(object: { type: string, coordinates: any[] }): this;
@@ -510,7 +450,7 @@ declare module 'mongoose' {
510
450
  get(path: string): any;
511
451
 
512
452
  /** Returns the current query filter (also known as conditions) as a POJO. */
513
- getFilter(): FilterQuery<RawDocType>;
453
+ getFilter(): QueryFilter<RawDocType>;
514
454
 
515
455
  /** Gets query options. */
516
456
  getOptions(): QueryOptions<RawDocType>;
@@ -519,24 +459,24 @@ declare module 'mongoose' {
519
459
  getPopulatedPaths(): Array<string>;
520
460
 
521
461
  /** Returns the current query filter. Equivalent to `getFilter()`. */
522
- getQuery(): FilterQuery<RawDocType>;
462
+ getQuery(): QueryFilter<RawDocType>;
523
463
 
524
464
  /** Returns the current update operations as a JSON object. */
525
465
  getUpdate(): UpdateQuery<DocType> | UpdateWithAggregationPipeline | null;
526
466
 
527
467
  /** Specifies a `$gt` query condition. When called with one argument, the most recent path passed to `where()` is used. */
528
- gt<K = string>(path: K, val: any): this;
468
+ gt(path: string, val: any): this;
529
469
  gt(val: number): this;
530
470
 
531
471
  /** Specifies a `$gte` query condition. When called with one argument, the most recent path passed to `where()` is used. */
532
- gte<K = string>(path: K, val: any): this;
472
+ gte(path: string, val: any): this;
533
473
  gte(val: number): this;
534
474
 
535
475
  /** Sets query hints. */
536
476
  hint(val: any): this;
537
477
 
538
478
  /** Specifies an `$in` query condition. When called with one argument, the most recent path passed to `where()` is used. */
539
- in<K = string>(path: K, val: any[]): this;
479
+ in(path: string, val: any[]): this;
540
480
  in(val: Array<any>): this;
541
481
 
542
482
  /** Declares an intersects query for `geometry()`. */
@@ -582,7 +522,7 @@ declare module 'mongoose' {
582
522
  QueryOp,
583
523
  TDocOverrides
584
524
  >;
585
- lean<LeanResultType>(): QueryWithHelpers<
525
+ lean<LeanResultType = RawDocType>(): QueryWithHelpers<
586
526
  ResultType extends null
587
527
  ? LeanResultType | null
588
528
  : LeanResultType,
@@ -592,7 +532,7 @@ declare module 'mongoose' {
592
532
  QueryOp,
593
533
  TDocOverrides
594
534
  >;
595
- lean<LeanResultType>(
535
+ lean<LeanResultType = RawDocType>(
596
536
  val: boolean | LeanOptions
597
537
  ): QueryWithHelpers<
598
538
  ResultType extends null
@@ -609,11 +549,11 @@ declare module 'mongoose' {
609
549
  limit(val: number): this;
610
550
 
611
551
  /** Specifies a `$lt` query condition. When called with one argument, the most recent path passed to `where()` is used. */
612
- lt<K = string>(path: K, val: any): this;
552
+ lt(path: string, val: any): this;
613
553
  lt(val: number): this;
614
554
 
615
555
  /** Specifies a `$lte` query condition. When called with one argument, the most recent path passed to `where()` is used. */
616
- lte<K = string>(path: K, val: any): this;
556
+ lte(path: string, val: any): this;
617
557
  lte(val: number): this;
618
558
 
619
559
  /**
@@ -634,10 +574,10 @@ declare module 'mongoose' {
634
574
  maxTimeMS(ms: number): this;
635
575
 
636
576
  /** Merges another Query or conditions object into this one. */
637
- merge(source: RootFilterQuery<RawDocType>): this;
577
+ merge(source: QueryFilter<RawDocType>): this;
638
578
 
639
579
  /** Specifies a `$mod` condition, filters documents for documents whose `path` property is a number that is equal to `remainder` modulo `divisor`. */
640
- mod<K = string>(path: K, val: number): this;
580
+ mod(path: string, val: number): this;
641
581
  mod(val: Array<number>): this;
642
582
 
643
583
  /** The model this query was created from */
@@ -650,22 +590,22 @@ declare module 'mongoose' {
650
590
  mongooseOptions(val?: QueryOptions<RawDocType>): QueryOptions<RawDocType>;
651
591
 
652
592
  /** Specifies a `$ne` query condition. When called with one argument, the most recent path passed to `where()` is used. */
653
- ne<K = string>(path: K, val: any): this;
593
+ ne(path: string, val: any): this;
654
594
  ne(val: any): this;
655
595
 
656
596
  /** Specifies a `$near` or `$nearSphere` condition */
657
- near<K = string>(path: K, val: any): this;
597
+ near(path: string, val: any): this;
658
598
  near(val: any): this;
659
599
 
660
600
  /** Specifies an `$nin` query condition. When called with one argument, the most recent path passed to `where()` is used. */
661
- nin<K = string>(path: K, val: any[]): this;
601
+ nin(path: string, val: any[]): this;
662
602
  nin(val: Array<any>): this;
663
603
 
664
604
  /** Specifies arguments for an `$nor` condition. */
665
- nor(array: Array<FilterQuery<RawDocType>>): this;
605
+ nor(array: Array<QueryFilter<RawDocType>>): this;
666
606
 
667
607
  /** Specifies arguments for an `$or` condition. */
668
- or(array: Array<FilterQuery<RawDocType>>): this;
608
+ or(array: Array<QueryFilter<RawDocType>>): this;
669
609
 
670
610
  /**
671
611
  * Make this query throw an error if no documents match the given `filter`.
@@ -744,7 +684,7 @@ declare module 'mongoose' {
744
684
  readConcern(level: string): this;
745
685
 
746
686
  /** Specifies a `$regex` query condition. When called with one argument, the most recent path passed to `where()` is used. */
747
- regex<K = string>(path: K, val: RegExp): this;
687
+ regex(path: string, val: RegExp): this;
748
688
  regex(val: string | RegExp): this;
749
689
 
750
690
  /**
@@ -753,7 +693,7 @@ declare module 'mongoose' {
753
693
  * not accept any [atomic](https://www.mongodb.com/docs/manual/tutorial/model-data-for-atomic-operations/#pattern) operators (`$set`, etc.)
754
694
  */
755
695
  replaceOne(
756
- filter?: RootFilterQuery<RawDocType>,
696
+ filter?: QueryFilter<RawDocType>,
757
697
  replacement?: DocType | AnyObject,
758
698
  options?: QueryOptions<RawDocType> | null
759
699
  ): QueryWithHelpers<any, DocType, THelpers, RawDocType, 'replaceOne', TDocOverrides>;
@@ -824,12 +764,12 @@ declare module 'mongoose' {
824
764
  setOptions(options: QueryOptions<RawDocType>, overwrite?: boolean): this;
825
765
 
826
766
  /** Sets the query conditions to the provided JSON object. */
827
- setQuery(val: FilterQuery<RawDocType> | null): void;
767
+ setQuery(val: QueryFilter<RawDocType> | null): void;
828
768
 
829
769
  setUpdate(update: UpdateQuery<RawDocType> | UpdateWithAggregationPipeline): void;
830
770
 
831
771
  /** Specifies an `$size` query condition. When called with one argument, the most recent path passed to `where()` is used. */
832
- size<K = string>(path: K, val: number): this;
772
+ size(path: string, val: number): this;
833
773
  size(val: number): this;
834
774
 
835
775
  /** Specifies the number of documents to skip. */
@@ -841,7 +781,7 @@ declare module 'mongoose' {
841
781
 
842
782
  /** Sets the sort order. If an object is passed, values allowed are `asc`, `desc`, `ascending`, `descending`, `1`, and `-1`. */
843
783
  sort(
844
- arg?: string | { [key: string]: SortOrder | { $meta: any } } | [string, SortOrder][] | undefined | null,
784
+ arg?: string | Record<string, SortOrder | { $meta: any }> | [string, SortOrder][] | undefined | null,
845
785
  options?: { override?: boolean }
846
786
  ): this;
847
787
 
@@ -862,31 +802,23 @@ declare module 'mongoose' {
862
802
 
863
803
  /**
864
804
  * Declare and/or execute this query as an updateMany() operation. Same as
865
- * `update()`, except MongoDB will update _all_ documents that match
866
- * `filter` (as opposed to just the first one) regardless of the value of
867
- * the `multi` option.
805
+ * `update()`, except MongoDB will update _all_ documents that match `filter`
868
806
  */
869
807
  updateMany(
870
- filter: RootFilterQuery<RawDocType>,
808
+ filter: QueryFilter<RawDocType>,
871
809
  update: UpdateQuery<RawDocType> | UpdateWithAggregationPipeline,
872
810
  options?: QueryOptions<RawDocType> | null
873
811
  ): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType, 'updateMany', TDocOverrides>;
874
- updateMany(
875
- update: UpdateQuery<RawDocType> | UpdateWithAggregationPipeline
876
- ): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType, 'updateMany', TDocOverrides>;
877
812
 
878
813
  /**
879
814
  * Declare and/or execute this query as an updateOne() operation. Same as
880
815
  * `update()`, except it does not support the `multi` or `overwrite` options.
881
816
  */
882
817
  updateOne(
883
- filter: RootFilterQuery<RawDocType>,
818
+ filter: QueryFilter<RawDocType>,
884
819
  update: UpdateQuery<RawDocType> | UpdateWithAggregationPipeline,
885
820
  options?: QueryOptions<RawDocType> | null
886
821
  ): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType, 'updateOne', TDocOverrides>;
887
- updateOne(
888
- update: UpdateQuery<RawDocType> | UpdateWithAggregationPipeline
889
- ): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType, 'updateOne', TDocOverrides>;
890
822
 
891
823
  /**
892
824
  * Sets the specified number of `mongod` servers, or tag set of `mongod` servers,
@@ -16,7 +16,7 @@ declare module 'mongoose' {
16
16
  QueryHelpers = {},
17
17
  TStaticMethods = {},
18
18
  TVirtuals = {},
19
- THydratedDocumentType = HydratedDocument<DocType, TInstanceMethods, QueryHelpers>,
19
+ THydratedDocumentType = HydratedDocument<DocType, TVirtuals & TInstanceMethods, QueryHelpers>,
20
20
  TModelType = Model<DocType, QueryHelpers, TInstanceMethods, TVirtuals, THydratedDocumentType>
21
21
  > {
22
22
  /**
@@ -67,7 +67,7 @@ declare module 'mongoose' {
67
67
  AnyArray<ObjectIdSchemaDefinition> | AnyArray<SchemaTypeOptions<ObjectId, EnforcedDocType, THydratedDocumentType>>
68
68
  : T extends object[] ?
69
69
  | AnyArray<Schema<any, any, any>>
70
- | AnyArray<SchemaDefinition<Unpacked<T>>>
70
+ | AnyArray<SchemaDefinition<Unpacked<T>, EnforcedDocType, THydratedDocumentType>>
71
71
  | AnyArray<SchemaTypeOptions<Unpacked<T>, EnforcedDocType, THydratedDocumentType>>
72
72
  : T extends string[] ?
73
73
  AnyArray<StringSchemaDefinition> | AnyArray<SchemaTypeOptions<string, EnforcedDocType, THydratedDocumentType>>
@@ -77,15 +77,13 @@ declare module 'mongoose' {
77
77
  AnyArray<BooleanSchemaDefinition> | AnyArray<SchemaTypeOptions<boolean, EnforcedDocType, THydratedDocumentType>>
78
78
  : T extends Function[] ?
79
79
  AnyArray<Function | string> | AnyArray<SchemaTypeOptions<Unpacked<T>, EnforcedDocType, THydratedDocumentType>>
80
- : T | typeof SchemaType | Schema<any, any, any> | SchemaDefinition<T> | Function | AnyArray<Function>;
80
+ : T | typeof SchemaType | Schema<any, any, any> | SchemaDefinition<T, EnforcedDocType, THydratedDocumentType> | Function | AnyArray<Function>;
81
81
 
82
82
  /** Defines a virtual with the given name that gets/sets this path. */
83
83
  alias?: string | string[];
84
84
 
85
85
  /** Function or object describing how to validate this schematype. See [validation docs](https://mongoosejs.com/docs/validation.html). */
86
- validate?:
87
- | SchemaValidator<T, EnforcedDocType, THydratedDocumentType>
88
- | AnyArray<SchemaValidator<T, EnforcedDocType, THydratedDocumentType>>;
86
+ validate?: SchemaValidator<T, THydratedDocumentType> | AnyArray<SchemaValidator<T, THydratedDocumentType>>;
89
87
 
90
88
  /** Allows overriding casting logic for this individual path. If a string, the given string overwrites Mongoose's default cast error message. */
91
89
  cast?:
@@ -102,15 +100,15 @@ declare module 'mongoose' {
102
100
  */
103
101
  required?:
104
102
  | boolean
105
- | ((this: EnforcedDocType) => boolean)
103
+ | ((this: THydratedDocumentType) => boolean)
106
104
  | [boolean, string]
107
- | [(this: EnforcedDocType) => boolean, string];
105
+ | [(this: THydratedDocumentType) => boolean, string];
108
106
 
109
107
  /**
110
108
  * The default value for this path. If a function, Mongoose executes the function
111
109
  * and uses the return value as the default.
112
110
  */
113
- default?: DefaultType<T> | ((this: EnforcedDocType, doc: any) => DefaultType<T> | null | undefined) | null;
111
+ default?: DefaultType<T> | ((this: THydratedDocumentType, doc: THydratedDocumentType) => DefaultType<T> | null | undefined) | null;
114
112
 
115
113
  /**
116
114
  * The model that `populate()` should use if populating this path.
@@ -403,7 +401,7 @@ declare module 'mongoose' {
403
401
  discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
404
402
 
405
403
  /** The schematype embedded in this array */
406
- caster?: SchemaType;
404
+ embeddedSchemaType: SchemaType;
407
405
 
408
406
  /** Default options for this SchemaType */
409
407
  static defaultOptions: Record<string, any>;
@@ -505,8 +503,11 @@ declare module 'mongoose' {
505
503
  /** The schema used for documents in this array */
506
504
  schema: Schema;
507
505
 
506
+ /** The schematype embedded in this array */
507
+ embeddedSchemaType: Subdocument;
508
+
508
509
  /** The constructor used for subdocuments in this array */
509
- caster?: typeof Types.Subdocument;
510
+ Constructor: typeof Types.Subdocument;
510
511
 
511
512
  /** Default options for this SchemaType */
512
513
  static defaultOptions: Record<string, any>;
@@ -576,6 +577,9 @@ declare module 'mongoose' {
576
577
  /** The document's schema */
577
578
  schema: Schema;
578
579
 
580
+ /** The constructor used to create subdocuments based on this schematype */
581
+ Constructor: typeof Types.Subdocument;
582
+
579
583
  /** Default options for this SchemaType */
580
584
  static defaultOptions: Record<string, any>;
581
585
 
package/types/types.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
 
2
2
  declare module 'mongoose' {
3
3
  import mongodb = require('mongodb');
4
- import bson = require('bson');
5
4
 
6
5
  class NativeBuffer extends Buffer {}
7
6
 
@@ -60,7 +59,7 @@ declare module 'mongoose' {
60
59
 
61
60
  class Decimal128 extends mongodb.Decimal128 { }
62
61
 
63
- class DocumentArray<T, THydratedDocumentType extends Types.Subdocument<any, any, T> = Types.Subdocument<InferId<T>, any, T> & T> extends Types.Array<THydratedDocumentType> {
62
+ class DocumentArray<T, THydratedDocumentType extends Types.Subdocument<any, any, any> = Types.Subdocument<InferId<T>, unknown, T> & T> extends Types.Array<THydratedDocumentType> {
64
63
  /** DocumentArray constructor */
65
64
  constructor(values: AnyObject[]);
66
65
 
@@ -103,8 +102,8 @@ declare module 'mongoose' {
103
102
  parentArray(): Types.DocumentArray<unknown>;
104
103
  }
105
104
 
106
- class UUID extends bson.UUID {}
105
+ class UUID extends mongodb.UUID {}
107
106
 
108
- class Double extends bson.Double {}
107
+ class Double extends mongodb.Double {}
109
108
  }
110
109
  }