mongoose 8.0.2 → 8.0.4

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 (39) hide show
  1. package/dist/browser.umd.js +1 -1
  2. package/lib/cursor/changeStream.js +0 -12
  3. package/lib/document.js +19 -6
  4. package/lib/helpers/clone.js +1 -1
  5. package/lib/helpers/discriminator/applyEmbeddedDiscriminators.js +30 -0
  6. package/lib/helpers/document/applyDefaults.js +3 -1
  7. package/lib/helpers/indexes/getRelatedIndexes.js +5 -1
  8. package/lib/helpers/model/castBulkWrite.js +8 -6
  9. package/lib/helpers/populate/assignVals.js +5 -0
  10. package/lib/helpers/projection/hasIncludedChildren.js +1 -0
  11. package/lib/helpers/projection/isExclusive.js +2 -3
  12. package/lib/helpers/projection/isNestedProjection.js +8 -0
  13. package/lib/helpers/query/castUpdate.js +5 -1
  14. package/lib/model.js +3 -1
  15. package/lib/mongoose.js +3 -0
  16. package/lib/query.js +11 -36
  17. package/lib/queryHelpers.js +3 -2
  18. package/lib/schema/array.js +2 -2
  19. package/lib/schema/bigint.js +11 -4
  20. package/lib/schema/boolean.js +9 -4
  21. package/lib/schema/buffer.js +21 -12
  22. package/lib/schema/date.js +15 -8
  23. package/lib/schema/decimal128.js +7 -8
  24. package/lib/schema/documentArray.js +1 -7
  25. package/lib/schema/number.js +22 -13
  26. package/lib/schema/objectId.js +7 -7
  27. package/lib/schema/string.js +11 -3
  28. package/lib/schema/subdocument.js +3 -7
  29. package/lib/schema/uuid.js +12 -5
  30. package/lib/schema.js +11 -9
  31. package/lib/schemaType.js +8 -1
  32. package/lib/types/subdocument.js +3 -3
  33. package/lib/utils.js +0 -33
  34. package/package.json +8 -8
  35. package/types/document.d.ts +2 -2
  36. package/types/index.d.ts +3 -0
  37. package/types/inferschematype.d.ts +13 -12
  38. package/types/models.d.ts +21 -12
  39. package/types/query.d.ts +57 -39
@@ -7,7 +7,6 @@
7
7
  const SchemaType = require('../schemaType');
8
8
  const CastError = SchemaType.CastError;
9
9
  const castDecimal128 = require('../cast/decimal128');
10
- const utils = require('../utils');
11
10
  const isBsonType = require('../helpers/isBsonType');
12
11
 
13
12
  /**
@@ -214,13 +213,13 @@ function handleSingle(val) {
214
213
  return this.cast(val);
215
214
  }
216
215
 
217
- SchemaDecimal128.prototype.$conditionalHandlers =
218
- utils.options(SchemaType.prototype.$conditionalHandlers, {
219
- $gt: handleSingle,
220
- $gte: handleSingle,
221
- $lt: handleSingle,
222
- $lte: handleSingle
223
- });
216
+ SchemaDecimal128.prototype.$conditionalHandlers = {
217
+ ...SchemaType.prototype.$conditionalHandlers,
218
+ $gt: handleSingle,
219
+ $gte: handleSingle,
220
+ $lt: handleSingle,
221
+ $lte: handleSingle
222
+ };
224
223
 
225
224
  /*!
226
225
  * Module exports.
@@ -88,12 +88,6 @@ function SchemaDocumentArray(key, schema, options, schemaOptions) {
88
88
 
89
89
  this.$embeddedSchemaType.caster = this.Constructor;
90
90
  this.$embeddedSchemaType.schema = this.schema;
91
-
92
- if (schema._applyDiscriminators != null && !options?._skipApplyDiscriminators) {
93
- for (const disc of schema._applyDiscriminators.keys()) {
94
- this.discriminator(disc, schema._applyDiscriminators.get(disc));
95
- }
96
- }
97
91
  }
98
92
 
99
93
  /**
@@ -528,7 +522,7 @@ SchemaDocumentArray.prototype.cast = function(value, doc, init, prev, options) {
528
522
 
529
523
  SchemaDocumentArray.prototype.clone = function() {
530
524
  const options = Object.assign({}, this.options);
531
- const schematype = new this.constructor(this.path, this.schema, { ...options, _skipApplyDiscriminators: true }, this.schemaOptions);
525
+ const schematype = new this.constructor(this.path, this.schema, options, this.schemaOptions);
532
526
  schematype.validators = this.validators.slice();
533
527
  if (this.requiredValidator !== undefined) {
534
528
  schematype.requiredValidator = this.requiredValidator;
@@ -399,18 +399,18 @@ function handleArray(val) {
399
399
  });
400
400
  }
401
401
 
402
- SchemaNumber.prototype.$conditionalHandlers =
403
- utils.options(SchemaType.prototype.$conditionalHandlers, {
404
- $bitsAllClear: handleBitwiseOperator,
405
- $bitsAnyClear: handleBitwiseOperator,
406
- $bitsAllSet: handleBitwiseOperator,
407
- $bitsAnySet: handleBitwiseOperator,
408
- $gt: handleSingle,
409
- $gte: handleSingle,
410
- $lt: handleSingle,
411
- $lte: handleSingle,
412
- $mod: handleArray
413
- });
402
+ SchemaNumber.prototype.$conditionalHandlers = {
403
+ ...SchemaType.prototype.$conditionalHandlers,
404
+ $bitsAllClear: handleBitwiseOperator,
405
+ $bitsAnyClear: handleBitwiseOperator,
406
+ $bitsAllSet: handleBitwiseOperator,
407
+ $bitsAnySet: handleBitwiseOperator,
408
+ $gt: handleSingle,
409
+ $gte: handleSingle,
410
+ $lt: handleSingle,
411
+ $lte: handleSingle,
412
+ $mod: handleArray
413
+ };
414
414
 
415
415
  /**
416
416
  * Casts contents for queries.
@@ -429,7 +429,16 @@ SchemaNumber.prototype.castForQuery = function($conditional, val, context) {
429
429
  }
430
430
  return handler.call(this, val, context);
431
431
  }
432
- val = this.applySetters(val, context);
432
+
433
+ try {
434
+ val = this.applySetters(val, context);
435
+ } catch (err) {
436
+ if (err instanceof CastError && err.path === this.path && this.$fullPath != null) {
437
+ err.path = this.$fullPath;
438
+ }
439
+ throw err;
440
+ }
441
+
433
442
  return val;
434
443
  };
435
444
 
@@ -259,13 +259,13 @@ function handleSingle(val) {
259
259
  return this.cast(val);
260
260
  }
261
261
 
262
- SchemaObjectId.prototype.$conditionalHandlers =
263
- utils.options(SchemaType.prototype.$conditionalHandlers, {
264
- $gt: handleSingle,
265
- $gte: handleSingle,
266
- $lt: handleSingle,
267
- $lte: handleSingle
268
- });
262
+ SchemaObjectId.prototype.$conditionalHandlers = {
263
+ ...SchemaType.prototype.$conditionalHandlers,
264
+ $gt: handleSingle,
265
+ $gte: handleSingle,
266
+ $lt: handleSingle,
267
+ $lte: handleSingle
268
+ };
269
269
 
270
270
  /*!
271
271
  * ignore
@@ -641,7 +641,8 @@ function handleSingleNoSetters(val) {
641
641
  return this.cast(val, this);
642
642
  }
643
643
 
644
- const $conditionalHandlers = utils.options(SchemaType.prototype.$conditionalHandlers, {
644
+ const $conditionalHandlers = {
645
+ ...SchemaType.prototype.$conditionalHandlers,
645
646
  $all: handleArray,
646
647
  $gt: handleSingle,
647
648
  $gte: handleSingle,
@@ -656,7 +657,7 @@ const $conditionalHandlers = utils.options(SchemaType.prototype.$conditionalHand
656
657
  return handleSingleNoSetters.call(this, val);
657
658
  },
658
659
  $not: handleSingle
659
- });
660
+ };
660
661
 
661
662
  Object.defineProperty(SchemaString.prototype, '$conditionalHandlers', {
662
663
  configurable: false,
@@ -687,7 +688,14 @@ SchemaString.prototype.castForQuery = function($conditional, val, context) {
687
688
  return val;
688
689
  }
689
690
 
690
- return this.applySetters(val, context);
691
+ try {
692
+ return this.applySetters(val, context);
693
+ } catch (err) {
694
+ if (err instanceof CastError && err.path === this.path && this.$fullPath != null) {
695
+ err.path = this.$fullPath;
696
+ }
697
+ throw err;
698
+ }
691
699
  };
692
700
 
693
701
  /*!
@@ -48,18 +48,13 @@ function SchemaSubdocument(schema, path, options) {
48
48
 
49
49
  schema = handleIdOption(schema, options);
50
50
 
51
- this.caster = _createConstructor(schema);
51
+ this.caster = _createConstructor(schema, null, options);
52
52
  this.caster.path = path;
53
53
  this.caster.prototype.$basePath = path;
54
54
  this.schema = schema;
55
55
  this.$isSingleNested = true;
56
56
  this.base = schema.base;
57
57
  SchemaType.call(this, path, options, 'Embedded');
58
- if (schema._applyDiscriminators != null && !options?._skipApplyDiscriminators) {
59
- for (const disc of schema._applyDiscriminators.keys()) {
60
- this.discriminator(disc, schema._applyDiscriminators.get(disc));
61
- }
62
- }
63
58
  }
64
59
 
65
60
  /*!
@@ -74,7 +69,7 @@ SchemaSubdocument.prototype.OptionsConstructor = SchemaSubdocumentOptions;
74
69
  * ignore
75
70
  */
76
71
 
77
- function _createConstructor(schema, baseClass) {
72
+ function _createConstructor(schema, baseClass, options) {
78
73
  // lazy load
79
74
  SubdocumentType || (SubdocumentType = require('../types/subdocument'));
80
75
 
@@ -94,6 +89,7 @@ function _createConstructor(schema, baseClass) {
94
89
  _embedded.prototype = Object.create(proto);
95
90
  _embedded.prototype.$__setSchema(schema);
96
91
  _embedded.prototype.constructor = _embedded;
92
+ _embedded.$__required = options?.required;
97
93
  _embedded.base = schema.base;
98
94
  _embedded.schema = schema;
99
95
  _embedded.$isSingleNested = true;
@@ -313,8 +313,8 @@ function handleArray(val) {
313
313
  });
314
314
  }
315
315
 
316
- SchemaUUID.prototype.$conditionalHandlers =
317
- utils.options(SchemaType.prototype.$conditionalHandlers, {
316
+ SchemaUUID.prototype.$conditionalHandlers = {
317
+ ...SchemaType.prototype.$conditionalHandlers,
318
318
  $bitsAllClear: handleBitwiseOperator,
319
319
  $bitsAnyClear: handleBitwiseOperator,
320
320
  $bitsAllSet: handleBitwiseOperator,
@@ -327,7 +327,7 @@ utils.options(SchemaType.prototype.$conditionalHandlers, {
327
327
  $lte: handleSingle,
328
328
  $ne: handleSingle,
329
329
  $nin: handleArray
330
- });
330
+ };
331
331
 
332
332
  /**
333
333
  * Casts contents for queries.
@@ -344,8 +344,15 @@ SchemaUUID.prototype.castForQuery = function($conditional, val, context) {
344
344
  if (!handler)
345
345
  throw new Error('Can\'t use ' + $conditional + ' with UUID.');
346
346
  return handler.call(this, val, context);
347
- } else {
348
- return this.cast(val);
347
+ }
348
+
349
+ try {
350
+ return this.applySetters(val, context);
351
+ } catch (err) {
352
+ if (err instanceof CastError && err.path === this.path && this.$fullPath != null) {
353
+ err.path = this.$fullPath;
354
+ }
355
+ throw err;
349
356
  }
350
357
  };
351
358
 
package/lib/schema.js CHANGED
@@ -25,6 +25,8 @@ const utils = require('./utils');
25
25
  const validateRef = require('./helpers/populate/validateRef');
26
26
  const util = require('util');
27
27
 
28
+ const hasNumericSubpathRegex = /\.\d+(\.|$)/;
29
+
28
30
  let MongooseTypes;
29
31
 
30
32
  const queryHooks = require('./helpers/query/applyQueryMiddleware').
@@ -49,7 +51,7 @@ const numberRE = /^\d+$/;
49
51
  * const Tree = mongoose.model('Tree', schema);
50
52
  *
51
53
  * // setting schema options
52
- * new Schema({ name: String }, { _id: false, autoIndex: false })
54
+ * new Schema({ name: String }, { id: false, autoIndex: false })
53
55
  *
54
56
  * #### Options:
55
57
  *
@@ -560,7 +562,7 @@ Schema.prototype.defaultOptions = function(options) {
560
562
  const strict = 'strict' in baseOptions ? baseOptions.strict : true;
561
563
  const strictQuery = 'strictQuery' in baseOptions ? baseOptions.strictQuery : false;
562
564
  const id = 'id' in baseOptions ? baseOptions.id : true;
563
- options = utils.options({
565
+ options = {
564
566
  strict,
565
567
  strictQuery,
566
568
  bufferCommands: true,
@@ -577,8 +579,9 @@ Schema.prototype.defaultOptions = function(options) {
577
579
  // the following are only applied at construction time
578
580
  _id: true,
579
581
  id: id,
580
- typeKey: 'type'
581
- }, clone(options));
582
+ typeKey: 'type',
583
+ ...options
584
+ };
582
585
 
583
586
  if (options.versionKey && typeof options.versionKey !== 'string') {
584
587
  throw new MongooseError('`versionKey` must be falsy or string, got `' + (typeof options.versionKey) + '`');
@@ -1007,7 +1010,7 @@ Schema.prototype.path = function(path, obj) {
1007
1010
  }
1008
1011
 
1009
1012
  // subpaths?
1010
- return /\.\d+\.?.*$/.test(path)
1013
+ return hasNumericSubpathRegex.test(path)
1011
1014
  ? getPositionalPath(this, path, cleanPath)
1012
1015
  : undefined;
1013
1016
  }
@@ -1122,15 +1125,14 @@ Schema.prototype.path = function(path, obj) {
1122
1125
  if (_schemaType.$isMongooseDocumentArray) {
1123
1126
  _schemaType.$embeddedSchemaType._arrayPath = arrayPath;
1124
1127
  _schemaType.$embeddedSchemaType._arrayParentPath = path;
1125
- _schemaType = _schemaType.$embeddedSchemaType.clone();
1128
+ _schemaType = _schemaType.$embeddedSchemaType;
1126
1129
  } else {
1127
1130
  _schemaType.caster._arrayPath = arrayPath;
1128
1131
  _schemaType.caster._arrayParentPath = path;
1129
- _schemaType = _schemaType.caster.clone();
1132
+ _schemaType = _schemaType.caster;
1130
1133
  }
1131
1134
 
1132
- _schemaType.path = arrayPath;
1133
- toAdd.push(_schemaType);
1135
+ this.subpaths[arrayPath] = _schemaType;
1134
1136
  }
1135
1137
 
1136
1138
  for (const _schemaType of toAdd) {
package/lib/schemaType.js CHANGED
@@ -1630,7 +1630,14 @@ SchemaType.prototype.castForQuery = function($conditional, val, context) {
1630
1630
  return handler.call(this, val, context);
1631
1631
  }
1632
1632
 
1633
- return this.applySetters(val, context);
1633
+ try {
1634
+ return this.applySetters(val, context);
1635
+ } catch (err) {
1636
+ if (err instanceof CastError && err.path === this.path && this.$fullPath != null) {
1637
+ err.path = this.$fullPath;
1638
+ }
1639
+ throw err;
1640
+ }
1634
1641
  };
1635
1642
 
1636
1643
  /**
@@ -182,7 +182,7 @@ Subdocument.prototype.markModified = function(path) {
182
182
  * ignore
183
183
  */
184
184
 
185
- Subdocument.prototype.isModified = function(paths, modifiedPaths) {
185
+ Subdocument.prototype.isModified = function(paths, options, modifiedPaths) {
186
186
  const parent = this.$parent();
187
187
  if (parent != null) {
188
188
  if (Array.isArray(paths) || typeof paths === 'string') {
@@ -192,10 +192,10 @@ Subdocument.prototype.isModified = function(paths, modifiedPaths) {
192
192
  paths = this.$__pathRelativeToParent();
193
193
  }
194
194
 
195
- return parent.$isModified(paths, modifiedPaths);
195
+ return parent.$isModified(paths, options, modifiedPaths);
196
196
  }
197
197
 
198
- return Document.prototype.isModified.call(this, paths, modifiedPaths);
198
+ return Document.prototype.isModified.call(this, paths, options, modifiedPaths);
199
199
  };
200
200
 
201
201
  /**
package/lib/utils.js CHANGED
@@ -227,33 +227,6 @@ exports.omit = function omit(obj, keys) {
227
227
  return ret;
228
228
  };
229
229
 
230
-
231
- /**
232
- * Shallow copies defaults into options.
233
- *
234
- * @param {Object} defaults
235
- * @param {Object} [options]
236
- * @return {Object} the merged object
237
- * @api private
238
- */
239
-
240
- exports.options = function(defaults, options) {
241
- const keys = Object.keys(defaults);
242
- let i = keys.length;
243
- let k;
244
-
245
- options = options || {};
246
-
247
- while (i--) {
248
- k = keys[i];
249
- if (!(k in options)) {
250
- options[k] = defaults[k];
251
- }
252
- }
253
-
254
- return options;
255
- };
256
-
257
230
  /**
258
231
  * Merges `from` into `to` without overwriting existing properties.
259
232
  *
@@ -687,12 +660,6 @@ exports.object.vals = function vals(o) {
687
660
  return ret;
688
661
  };
689
662
 
690
- /**
691
- * @see exports.options
692
- */
693
-
694
- exports.object.shallowCopy = exports.options;
695
-
696
663
  const hop = Object.prototype.hasOwnProperty;
697
664
 
698
665
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "8.0.2",
4
+ "version": "8.0.4",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -28,8 +28,8 @@
28
28
  "sift": "16.0.1"
29
29
  },
30
30
  "devDependencies": {
31
- "@babel/core": "7.23.2",
32
- "@babel/preset-env": "7.22.20",
31
+ "@babel/core": "7.23.7",
32
+ "@babel/preset-env": "7.23.7",
33
33
  "@typescript-eslint/eslint-plugin": "^6.2.1",
34
34
  "@typescript-eslint/parser": "^6.2.1",
35
35
  "acquit": "1.3.0",
@@ -45,15 +45,15 @@
45
45
  "crypto-browserify": "3.12.0",
46
46
  "dotenv": "16.3.1",
47
47
  "dox": "1.0.0",
48
- "eslint": "8.52.0",
48
+ "eslint": "8.56.0",
49
49
  "eslint-plugin-markdown": "^3.0.1",
50
50
  "eslint-plugin-mocha-no-only": "1.1.1",
51
51
  "express": "^4.18.1",
52
- "fs-extra": "~11.1.1",
52
+ "fs-extra": "~11.2.0",
53
53
  "highlight.js": "11.8.0",
54
54
  "lodash.isequal": "4.5.0",
55
55
  "lodash.isequalwith": "4.4.0",
56
- "markdownlint-cli2": "^0.10.0",
56
+ "markdownlint-cli2": "^0.11.0",
57
57
  "marked": "4.3.0",
58
58
  "mkdirp": "^3.0.1",
59
59
  "mocha": "10.2.0",
@@ -65,8 +65,8 @@
65
65
  "q": "1.5.1",
66
66
  "sinon": "17.0.1",
67
67
  "stream-browserify": "3.0.0",
68
- "tsd": "0.29.0",
69
- "typescript": "5.2.2",
68
+ "tsd": "0.30.3",
69
+ "typescript": "5.3.3",
70
70
  "uuid": "9.0.1",
71
71
  "webpack": "5.89.0"
72
72
  },
@@ -184,8 +184,8 @@ declare module 'mongoose' {
184
184
  * Returns true if any of the given paths are modified, else false. If no arguments, returns `true` if any path
185
185
  * in this document is modified.
186
186
  */
187
- isModified<T extends keyof DocType>(path?: T | Array<T>): boolean;
188
- isModified(path?: string | Array<string>): boolean;
187
+ isModified<T extends keyof DocType>(path?: T | Array<T>, options?: { ignoreAtomics?: boolean } | null): boolean;
188
+ isModified(path?: string | Array<string>, options?: { ignoreAtomics?: boolean } | null): boolean;
189
189
 
190
190
  /** Boolean flag specifying if the document is new. */
191
191
  isNew: boolean;
package/types/index.d.ts CHANGED
@@ -302,6 +302,9 @@ declare module 'mongoose' {
302
302
  /** The original object passed to the schema constructor */
303
303
  obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>, EnforcedDocType>;
304
304
 
305
+ /** Returns a new schema that has the `paths` from the original schema, minus the omitted ones. */
306
+ omit<T = this>(paths: string[], options?: SchemaOptions): T;
307
+
305
308
  /** Gets/sets schema paths. */
306
309
  path<ResultType extends SchemaType = SchemaType<any, THydratedDocumentType>>(path: string): ResultType;
307
310
  path<pathGeneric extends keyof EnforcedDocType>(path: pathGeneric): SchemaType<EnforcedDocType[pathGeneric]>;
@@ -262,15 +262,16 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
262
262
  IfEquals<PathValueType, Schema.Types.Decimal128> extends true ? Types.Decimal128 :
263
263
  IfEquals<PathValueType, Types.Decimal128> extends true ? Types.Decimal128 :
264
264
  IfEquals<PathValueType, Schema.Types.BigInt> extends true ? bigint :
265
- PathValueType extends 'bigint' | 'BigInt' | typeof Schema.Types.BigInt ? bigint :
266
- PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer :
267
- IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
268
- PathValueType extends MapConstructor | 'Map' ? Map<string, ResolvePathType<Options['of']>> :
269
- IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolvePathType<Options['of']>> :
270
- PathValueType extends ArrayConstructor ? any[] :
271
- PathValueType extends typeof Schema.Types.Mixed ? any:
272
- IfEquals<PathValueType, ObjectConstructor> extends true ? any:
273
- IfEquals<PathValueType, {}> extends true ? any:
274
- PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
275
- PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, { typeKey: TypeKey }> :
276
- unknown;
265
+ IfEquals<PathValueType, BigInt> extends true ? bigint :
266
+ PathValueType extends 'bigint' | 'BigInt' | typeof Schema.Types.BigInt | typeof BigInt ? bigint :
267
+ PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer :
268
+ IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
269
+ PathValueType extends MapConstructor | 'Map' ? Map<string, ResolvePathType<Options['of']>> :
270
+ IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolvePathType<Options['of']>> :
271
+ PathValueType extends ArrayConstructor ? any[] :
272
+ PathValueType extends typeof Schema.Types.Mixed ? any:
273
+ IfEquals<PathValueType, ObjectConstructor> extends true ? any:
274
+ IfEquals<PathValueType, {}> extends true ? any:
275
+ PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
276
+ PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, { typeKey: TypeKey }> :
277
+ unknown;
package/types/models.d.ts CHANGED
@@ -27,11 +27,12 @@ declare module 'mongoose' {
27
27
  skipValidation?: boolean;
28
28
  throwOnValidationError?: boolean;
29
29
  timestamps?: boolean;
30
+ strict?: boolean | 'throw';
30
31
  }
31
32
 
32
33
  interface MongooseBulkWritePerWriteOptions {
33
34
  timestamps?: boolean;
34
- strict?: boolean;
35
+ strict?: boolean | 'throw';
35
36
  session?: ClientSession;
36
37
  skipValidation?: boolean;
37
38
  }
@@ -183,6 +184,10 @@ declare module 'mongoose' {
183
184
  /* Cast the given POJO to the model's schema */
184
185
  castObject(obj: AnyObject, options?: { ignoreCastErrors?: boolean }): TRawDocType;
185
186
 
187
+ /* Apply defaults to the given document or POJO. */
188
+ applyDefaults(obj: AnyObject): AnyObject;
189
+ applyDefaults(obj: TRawDocType): TRawDocType;
190
+
186
191
  /**
187
192
  * Sends multiple `insertOne`, `updateOne`, `updateMany`, `replaceOne`,
188
193
  * `deleteOne`, and/or `deleteMany` operations to the MongoDB server in one
@@ -218,7 +223,7 @@ declare module 'mongoose' {
218
223
  /** Creates a `countDocuments` query: counts the number of documents that match `filter`. */
219
224
  countDocuments(
220
225
  filter?: FilterQuery<TRawDocType>,
221
- options?: QueryOptions<TRawDocType>
226
+ options?: (mongodb.CountOptions & Omit<MongooseSpecificQueryOptions, 'lean' | 'timestamps'>) | null
222
227
  ): QueryWithHelpers<
223
228
  number,
224
229
  THydratedDocumentType,
@@ -250,7 +255,7 @@ declare module 'mongoose' {
250
255
  */
251
256
  deleteMany(
252
257
  filter?: FilterQuery<TRawDocType>,
253
- options?: QueryOptions<TRawDocType>
258
+ options?: (mongodb.DeleteOptions & Omit<MongooseSpecificQueryOptions, 'lean' | 'timestamps'>) | null
254
259
  ): QueryWithHelpers<
255
260
  mongodb.DeleteResult,
256
261
  THydratedDocumentType,
@@ -275,7 +280,7 @@ declare module 'mongoose' {
275
280
  */
276
281
  deleteOne(
277
282
  filter?: FilterQuery<TRawDocType>,
278
- options?: QueryOptions<TRawDocType>
283
+ options?: (mongodb.DeleteOptions & Omit<MongooseSpecificQueryOptions, 'lean' | 'timestamps'>) | null
279
284
  ): QueryWithHelpers<
280
285
  mongodb.DeleteResult,
281
286
  THydratedDocumentType,
@@ -395,6 +400,10 @@ declare module 'mongoose' {
395
400
  docs: Array<TRawDocType>,
396
401
  options: InsertManyOptions & { rawResult: true; }
397
402
  ): Promise<mongodb.InsertManyResult<Require_id<THydratedDocumentType>>>;
403
+ insertMany(
404
+ doc: Array<TRawDocType>,
405
+ options: InsertManyOptions
406
+ ): Promise<Array<THydratedDocumentType>>;
398
407
  insertMany<DocContents = TRawDocType>(
399
408
  docs: Array<DocContents | TRawDocType>,
400
409
  options: InsertManyOptions & { lean: true; }
@@ -556,8 +565,8 @@ declare module 'mongoose' {
556
565
  'findOneAndDelete'
557
566
  >;
558
567
  findByIdAndDelete<ResultDoc = THydratedDocumentType>(
559
- id?: mongodb.ObjectId | any,
560
- options?: QueryOptions<TRawDocType> & { includeResultMetadata: true }
568
+ id: mongodb.ObjectId | any,
569
+ options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
561
570
  ): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
562
571
  findByIdAndDelete<ResultDoc = THydratedDocumentType>(
563
572
  id?: mongodb.ObjectId | any,
@@ -608,11 +617,11 @@ declare module 'mongoose' {
608
617
  'findOneAndDelete'
609
618
  >;
610
619
  findOneAndDelete<ResultDoc = THydratedDocumentType>(
611
- filter?: FilterQuery<TRawDocType>,
612
- options?: QueryOptions<TRawDocType> & { includeResultMetadata: true }
620
+ filter: FilterQuery<TRawDocType>,
621
+ options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
613
622
  ): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
614
623
  findOneAndDelete<ResultDoc = THydratedDocumentType>(
615
- filter?: FilterQuery<TRawDocType>,
624
+ filter?: FilterQuery<TRawDocType> | null,
616
625
  options?: QueryOptions<TRawDocType> | null
617
626
  ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
618
627
 
@@ -681,7 +690,7 @@ declare module 'mongoose' {
681
690
  replaceOne<ResultDoc = THydratedDocumentType>(
682
691
  filter?: FilterQuery<TRawDocType>,
683
692
  replacement?: TRawDocType | AnyObject,
684
- options?: QueryOptions<TRawDocType> | null
693
+ options?: (mongodb.ReplaceOptions & MongooseSpecificQueryOptions) | null
685
694
  ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'replaceOne'>;
686
695
 
687
696
  /** Schema the model uses. */
@@ -691,14 +700,14 @@ declare module 'mongoose' {
691
700
  updateMany<ResultDoc = THydratedDocumentType>(
692
701
  filter?: FilterQuery<TRawDocType>,
693
702
  update?: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline,
694
- options?: QueryOptions<TRawDocType> | null
703
+ options?: (mongodb.UpdateOptions & Omit<MongooseSpecificQueryOptions, 'lean'>) | null
695
704
  ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'updateMany'>;
696
705
 
697
706
  /** Creates a `updateOne` query: updates the first document that matches `filter` with `update`. */
698
707
  updateOne<ResultDoc = THydratedDocumentType>(
699
708
  filter?: FilterQuery<TRawDocType>,
700
709
  update?: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline,
701
- options?: QueryOptions<TRawDocType> | null
710
+ options?: (mongodb.UpdateOptions & Omit<MongooseSpecificQueryOptions, 'lean'>) | null
702
711
  ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'updateOne'>;
703
712
 
704
713
  /** Creates a Query, applies the passed conditions, and returns the Query. */