mongoose 9.3.1 → 9.3.3

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/README.md CHANGED
@@ -11,7 +11,7 @@ Mongoose is a [MongoDB](https://www.mongodb.org/) object modeling tool designed
11
11
 
12
12
  ## Documentation
13
13
 
14
- The official documentation website is [mongoosejs.com](http://mongoosejs.com/).
14
+ The official documentation website is [mongoosejs.com](https://mongoosejs.com/).
15
15
 
16
16
  Mongoose 9.0.0 was released on November 21, 2025. You can find more details on [backwards breaking changes in 9.0.0 on our docs site](https://mongoosejs.com/docs/migrating_to_9.html).
17
17
 
@@ -25,7 +25,7 @@ Mongoose 9.0.0 was released on November 21, 2025. You can find more details on [
25
25
 
26
26
  ## Plugins
27
27
 
28
- Check out the [plugins search site](http://plugins.mongoosejs.io/) to see hundreds of related modules from the community. Next, learn how to write your own plugin from the [docs](http://mongoosejs.com/docs/plugins.html) or [this blog post](http://thecodebarbarian.com/2015/03/06/guide-to-mongoose-plugins).
28
+ Check out the [plugins search site](https://plugins.mongoosejs.io/) to see hundreds of related modules from the community. Next, learn how to write your own plugin from the [docs](https://mongoosejs.com/docs/plugins.html) or [this blog post](http://thecodebarbarian.com/2015/03/06/guide-to-mongoose-plugins).
29
29
 
30
30
  ## Contributors
31
31
 
@@ -40,9 +40,7 @@ View all 400+ [contributors](https://github.com/Automattic/mongoose/graphs/contr
40
40
 
41
41
  ## Installation
42
42
 
43
- First install [Node.js](http://nodejs.org/) and [MongoDB](https://www.mongodb.org/downloads). Then:
44
-
45
- Then install the `mongoose` package using your preferred package manager:
43
+ First install [Node.js](https://nodejs.org/) and [MongoDB](https://www.mongodb.org/downloads), then install the `mongoose` package using your preferred package manager:
46
44
 
47
45
  ### Using npm
48
46
 
@@ -140,16 +138,16 @@ const BlogPost = new Schema({
140
138
 
141
139
  Aside from defining the structure of your documents and the types of data you're storing, a Schema handles the definition of:
142
140
 
143
- * [Validators](http://mongoosejs.com/docs/validation.html) (async and sync)
144
- * [Defaults](http://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-default)
145
- * [Getters](http://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-get)
146
- * [Setters](http://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-set)
147
- * [Indexes](http://mongoosejs.com/docs/guide.html#indexes)
148
- * [Middleware](http://mongoosejs.com/docs/middleware.html)
149
- * [Methods](http://mongoosejs.com/docs/guide.html#methods) definition
150
- * [Statics](http://mongoosejs.com/docs/guide.html#statics) definition
151
- * [Plugins](http://mongoosejs.com/docs/plugins.html)
152
- * [pseudo-JOINs](http://mongoosejs.com/docs/populate.html)
141
+ * [Validators](https://mongoosejs.com/docs/validation.html) (async and sync)
142
+ * [Defaults](https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-default)
143
+ * [Getters](https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-get)
144
+ * [Setters](https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-set)
145
+ * [Indexes](https://mongoosejs.com/docs/guide.html#indexes)
146
+ * [Middleware](https://mongoosejs.com/docs/middleware.html)
147
+ * [Methods](https://mongoosejs.com/docs/guide.html#methods) definition
148
+ * [Statics](https://mongoosejs.com/docs/guide.html#statics) definition
149
+ * [Plugins](https://mongoosejs.com/docs/plugins.html)
150
+ * [pseudo-JOINs](https://mongoosejs.com/docs/populate.html)
153
151
 
154
152
  The following example shows some of these features:
155
153
 
@@ -219,7 +217,7 @@ const instance = await MyModel.findOne({ /* ... */ });
219
217
  console.log(instance.my.key); // 'hello'
220
218
  ```
221
219
 
222
- For more details check out [the docs](http://mongoosejs.com/docs/queries.html).
220
+ For more details check out [the docs](https://mongoosejs.com/docs/queries.html).
223
221
 
224
222
  **Important!** If you opened a separate connection using `mongoose.createConnection()` but attempt to access the model through `mongoose.model('ModelName')` it will not work as expected since it is not hooked up to an active db connection. In this case access your model through the connection you created:
225
223
 
@@ -274,7 +272,7 @@ Embedded documents enjoy all the same features as your models. Defaults, validat
274
272
 
275
273
  ### Middleware
276
274
 
277
- See the [docs](http://mongoosejs.com/docs/middleware.html) page.
275
+ See the [docs](https://mongoosejs.com/docs/middleware.html) page.
278
276
 
279
277
  #### Intercepting and mutating method arguments
280
278
 
@@ -347,7 +345,7 @@ return a cursor.
347
345
 
348
346
  ## API Docs
349
347
 
350
- [Mongoose API documentation](http://mongoosejs.com/docs/api/mongoose.html), generated using [dox](https://github.com/tj/dox)
348
+ [Mongoose API documentation](https://mongoosejs.com/docs/api/mongoose.html), generated using [dox](https://github.com/tj/dox)
351
349
  and [acquit](https://github.com/vkarpov15/acquit).
352
350
 
353
351
  ## Related Projects
package/lib/aggregate.js CHANGED
@@ -159,7 +159,7 @@ Aggregate.prototype.model = function(model) {
159
159
  * const pipeline = [{ $match: { daw: 'Logic Audio X' }} ];
160
160
  * aggregate.append(pipeline);
161
161
  *
162
- * @param {...object|object[]} ops operator(s) to append. Can either be a spread of objects or a single parameter of a object array.
162
+ * @param {...object|object[]} ops operator(s) to append. Can either be a spread of objects or a single parameter of an object array.
163
163
  * @return {Aggregate}
164
164
  * @api public
165
165
  */
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const assert = require('assert');
4
-
5
3
  /**
6
4
  * Given a value, cast it to a number, or throw an `Error` if the value
7
5
  * cannot be casted. `null` and `undefined` are considered valid.
@@ -24,7 +22,9 @@ module.exports = function castNumber(val) {
24
22
  val = Number(val);
25
23
  }
26
24
 
27
- assert.ok(!isNaN(val));
25
+ if (isNaN(val)) {
26
+ throw new Error('Cast to Number failed: value is not a valid number');
27
+ }
28
28
  if (val instanceof Number) {
29
29
  return val.valueOf();
30
30
  }
@@ -38,5 +38,5 @@ module.exports = function castNumber(val) {
38
38
  return Number(val);
39
39
  }
40
40
 
41
- assert.ok(false);
41
+ throw new Error('Cast to Number failed: value is not a valid number');
42
42
  };
package/lib/cast.js CHANGED
@@ -35,7 +35,7 @@ const ALLOWED_GEOWITHIN_GEOJSON_TYPES = ['Polygon', 'MultiPolygon'];
35
35
  */
36
36
  module.exports = function cast(schema, obj, options, context) {
37
37
  if (Array.isArray(obj)) {
38
- throw new Error('Query filter must be an object, got an array ', util.inspect(obj));
38
+ throw new Error('Query filter must be an object, got an array ' + util.inspect(obj));
39
39
  }
40
40
 
41
41
  if (obj == null) {
package/lib/connection.js CHANGED
@@ -132,7 +132,7 @@ Object.defineProperty(Connection.prototype, 'readyState', {
132
132
  },
133
133
  set: function(val) {
134
134
  if (!(val in STATES)) {
135
- throw new Error('Invalid connection state: ' + val);
135
+ throw new MongooseError('Invalid connection state: ' + val);
136
136
  }
137
137
 
138
138
  if (this._readyState !== val) {
@@ -177,7 +177,7 @@ Connection.prototype.get = function getOption(key) {
177
177
  * Supported options include:
178
178
  *
179
179
  * - `maxTimeMS`: Set [`maxTimeMS`](https://mongoosejs.com/docs/api/query.html#Query.prototype.maxTimeMS()) for all queries on this connection.
180
- * - 'debug': If `true`, prints the operations mongoose sends to MongoDB to the console. If a writable stream is passed, it will log to that stream, without colorization. If a callback function is passed, it will receive the collection name, the method name, then all arugments passed to the method. For example, if you wanted to replicate the default logging, you could output from the callback `Mongoose: ${collectionName}.${methodName}(${methodArgs.join(', ')})`.
180
+ * - 'debug': If `true`, prints the operations mongoose sends to MongoDB to the console. If a writable stream is passed, it will log to that stream, without colorization. If a callback function is passed, it will receive the collection name, the method name, then all arguments passed to the method. For example, if you wanted to replicate the default logging, you could output from the callback `Mongoose: ${collectionName}.${methodName}(${methodArgs.join(', ')})`.
181
181
  *
182
182
  * #### Example:
183
183
  *
@@ -655,7 +655,7 @@ Connection.prototype.createCollections = async function createCollections(option
655
655
 
656
656
  Connection.prototype.withSession = async function withSession(executor) {
657
657
  if (arguments.length === 0) {
658
- throw new Error('Please provide an executor function');
658
+ throw new MongooseError('Please provide an executor function');
659
659
  }
660
660
  return await this.client.withSession(executor);
661
661
  };
@@ -713,7 +713,7 @@ Connection.prototype.startSession = async function startSession(options) {
713
713
  * doc.isNew; // false
714
714
  *
715
715
  * // Throw an error to abort the transaction
716
- * throw new Error('Oops!');
716
+ * throw new MongooseError('Oops!');
717
717
  * },{ readPreference: 'primary' }).catch(() => {});
718
718
  *
719
719
  * // true, `transaction()` reset the document's state because the
@@ -1310,7 +1310,7 @@ Connection.prototype._close = async function _close(force, destroy) {
1310
1310
  */
1311
1311
 
1312
1312
  Connection.prototype.doClose = function doClose() {
1313
- throw new Error('Connection#doClose unimplemented by driver');
1313
+ throw new MongooseError('Connection#doClose unimplemented by driver');
1314
1314
  };
1315
1315
 
1316
1316
  /**
@@ -1453,7 +1453,7 @@ Connection.prototype.model = function model(name, schema, collection, options) {
1453
1453
  }
1454
1454
  }
1455
1455
  if (schema && !schema.instanceOfSchema) {
1456
- throw new Error('The 2nd parameter to `mongoose.model()` should be a ' +
1456
+ throw new MongooseError('The 2nd parameter to `mongoose.model()` should be a ' +
1457
1457
  'schema or a POJO');
1458
1458
  }
1459
1459
 
@@ -1566,7 +1566,7 @@ Connection.prototype.deleteModel = function deleteModel(name) {
1566
1566
  }
1567
1567
  }
1568
1568
  } else {
1569
- throw new Error('First parameter to `deleteModel()` must be a string ' +
1569
+ throw new MongooseError('First parameter to `deleteModel()` must be a string ' +
1570
1570
  'or regexp, got "' + name + '"');
1571
1571
  }
1572
1572
 
@@ -47,7 +47,7 @@ module.exports = function get(obj, path, def) {
47
47
  cur = getProperty(cur, part);
48
48
 
49
49
  if (!isPathArray) {
50
- rest = rest.substr(part.length + 1);
50
+ rest = rest.substring(part.length + 1);
51
51
  }
52
52
  }
53
53
 
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  /**
4
- * Get the bson type, if it exists
4
+ * Determines if `obj` has the given BSON type.
5
5
  * @api private
6
6
  */
7
7
 
package/lib/mongoose.js CHANGED
@@ -206,8 +206,8 @@ Mongoose.prototype.setDriver = function setDriver(driver) {
206
206
  /**
207
207
  * Sets mongoose options
208
208
  *
209
- * `key` can be used a object to set multiple options at once.
210
- * If a error gets thrown for one option, other options will still be evaluated.
209
+ * `key` can be used as an object to set multiple options at once.
210
+ * If an error gets thrown for one option, other options will still be evaluated.
211
211
  *
212
212
  * #### Example:
213
213
  *
@@ -247,8 +247,8 @@ Mongoose.prototype.setDriver = function setDriver(driver) {
247
247
  * - `translateAliases`: `false` by default. If `true`, Mongoose will automatically translate aliases to their original paths before sending the query to MongoDB.
248
248
  * - `updatePipeline`: `false` by default. If `true`, allows passing update pipelines (arrays) to update operations by default without explicitly setting `updatePipeline: true` in each query.
249
249
  *
250
- * @param {string|object} key The name of the option or a object of multiple key-value pairs
251
- * @param {string|Function|boolean} value The value of the option, unused if "key" is a object
250
+ * @param {string|object} key The name of the option or an object of multiple key-value pairs
251
+ * @param {string|Function|boolean} value The value of the option, unused if "key" is an object
252
252
  * @returns {Mongoose} The used Mongoose instance
253
253
  * @api public
254
254
  */
@@ -256,6 +256,12 @@ Mongoose.prototype.setDriver = function setDriver(driver) {
256
256
  Mongoose.prototype.set = function getsetOptions(key, value) {
257
257
  const _mongoose = this instanceof Mongoose ? this : mongoose;
258
258
 
259
+ if (key == null) {
260
+ const error = new SetOptionError();
261
+ error.addError(String(key), new SetOptionError.SetOptionInnerError(String(key)));
262
+ throw error;
263
+ }
264
+
259
265
  if (arguments.length === 1 && typeof key !== 'object') {
260
266
  if (VALID_OPTIONS.indexOf(key) === -1) {
261
267
  const error = new SetOptionError();
package/lib/query.js CHANGED
@@ -185,7 +185,7 @@ function isEmptyFilter(obj) {
185
185
  // Helper function to check for empty/invalid filter
186
186
  function checkRequireFilter(filter, options) {
187
187
  if (options?.requireFilter && isEmptyFilter(filter)) {
188
- throw new Error('Empty or invalid filter not allowed with requireFilter enabled');
188
+ throw new MongooseError('Empty or invalid filter not allowed with requireFilter enabled');
189
189
  }
190
190
  }
191
191
 
@@ -1113,7 +1113,7 @@ Query.prototype.select = function select() {
1113
1113
  if (!arg) return this;
1114
1114
 
1115
1115
  if (arguments.length !== 1) {
1116
- throw new Error('Invalid select: select only takes 1 argument');
1116
+ throw new MongooseError('Invalid select: select only takes 1 argument');
1117
1117
  }
1118
1118
 
1119
1119
  this._validate('select');
@@ -1721,7 +1721,7 @@ Query.prototype.setOptions = function(options, overwrite) {
1721
1721
  return this;
1722
1722
  }
1723
1723
  if (typeof options !== 'object') {
1724
- throw new Error('Options must be an object, got "' + options + '"');
1724
+ throw new MongooseError('Options must be an object, got "' + options + '"');
1725
1725
  }
1726
1726
 
1727
1727
  options = Object.assign({}, options);
@@ -3080,10 +3080,10 @@ Query.prototype.distinct = function(field, conditions, options) {
3080
3080
 
3081
3081
  Query.prototype.sort = function(arg, options) {
3082
3082
  if (arguments.length > 2) {
3083
- throw new Error('sort() takes at most 2 arguments');
3083
+ throw new MongooseError('sort() takes at most 2 arguments');
3084
3084
  }
3085
3085
  if (options != null && typeof options !== 'object') {
3086
- throw new Error('sort() options argument must be an object or nullish');
3086
+ throw new MongooseError('sort() options argument must be an object or nullish');
3087
3087
  }
3088
3088
 
3089
3089
  if (this.options.sort == null) {
@@ -4695,7 +4695,7 @@ Query.prototype.exec = async function exec(op) {
4695
4695
  }
4696
4696
 
4697
4697
  if (this.options?.sort && typeof this.options.sort === 'object' && Object.hasOwn(this.options.sort, '')) {
4698
- throw new Error('Invalid field "" passed to sort()');
4698
+ throw new MongooseError('Invalid field "" passed to sort()');
4699
4699
  }
4700
4700
 
4701
4701
  if (this._execCount > 0) {
@@ -5582,7 +5582,7 @@ Query.prototype[Symbol.asyncIterator] = function queryAsyncIterator() {
5582
5582
  * @see $box https://www.mongodb.com/docs/manual/reference/operator/box/
5583
5583
  * @see within() Query#within https://mongoosejs.com/docs/api/query.html#Query.prototype.within()
5584
5584
  * @see MongoDB Geospatial Indexing https://www.mongodb.com/docs/manual/core/geospatial-indexes/
5585
- * @param {object|Array<number>} val1 Lower Left Coordinates OR a object of lower-left(ll) and upper-right(ur) Coordinates
5585
+ * @param {object|Array<number>} val1 Lower Left Coordinates OR an object of lower-left(ll) and upper-right(ur) Coordinates
5586
5586
  * @param {Array<number>} [val2] Upper Right Coordinates
5587
5587
  * @return {Query} this
5588
5588
  * @api public
package/lib/schema.js CHANGED
@@ -2253,7 +2253,7 @@ Schema.prototype.plugin = function(fn, opts) {
2253
2253
  *
2254
2254
  * NOTE: `Schema.method()` adds instance methods to the `Schema.methods` object. You can also add instance methods directly to the `Schema.methods` object as seen in the [guide](https://mongoosejs.com/docs/guide.html#methods)
2255
2255
  *
2256
- * @param {string|object} name The Method Name for a single function, or a Object of "string-function" pairs.
2256
+ * @param {string|object} name The Method Name for a single function, or an Object of "string-function" pairs.
2257
2257
  * @param {Function} [fn] The Function in a single-function definition.
2258
2258
  * @api public
2259
2259
  */
@@ -2298,7 +2298,7 @@ Schema.prototype.method = function(name, fn, options) {
2298
2298
  *
2299
2299
  * If a hash of name/fn pairs is passed as the only argument, each name/fn pair will be added as statics.
2300
2300
  *
2301
- * @param {string|object} name The Method Name for a single function, or a Object of "string-function" pairs.
2301
+ * @param {string|object} name The Method Name for a single function, or an Object of "string-function" pairs.
2302
2302
  * @param {Function} [fn] The Function in a single-function definition.
2303
2303
  * @api public
2304
2304
  * @see Statics https://mongoosejs.com/docs/guide.html#statics
@@ -3119,7 +3119,7 @@ Schema.prototype.toJSONSchema = function toJSONSchema(options) {
3119
3119
  for (const path of Object.keys(this.paths)) {
3120
3120
  const schemaType = this.paths[path];
3121
3121
 
3122
- // Skip Map embedded paths, maps will be handled seperately.
3122
+ // Skip Map embedded paths, maps will be handled separately.
3123
3123
  if (schemaType._presplitPath.indexOf('$*') !== -1) {
3124
3124
  continue;
3125
3125
  }
package/lib/schemaType.js CHANGED
@@ -99,11 +99,11 @@ function SchemaType(path, options, instance, parentSchema) {
99
99
  const index = this._index;
100
100
  if (typeof index === 'object' && index != null) {
101
101
  if (index.unique) {
102
- throw new Error('Path "' + this.path + '" may not have `index` ' +
102
+ throw new MongooseError('Path "' + this.path + '" may not have `index` ' +
103
103
  'set to false and `unique` set to true');
104
104
  }
105
105
  if (index.sparse) {
106
- throw new Error('Path "' + this.path + '" may not have `index` ' +
106
+ throw new MongooseError('Path "' + this.path + '" may not have `index` ' +
107
107
  'set to false and `sparse` set to true');
108
108
  }
109
109
  }
@@ -317,7 +317,7 @@ SchemaType.prototype.castFunction = function castFunction(caster, message) {
317
317
  */
318
318
 
319
319
  SchemaType.prototype.cast = function cast() {
320
- throw new Error('Base SchemaType class does not implement a `cast()` function');
320
+ throw new MongooseError('Base SchemaType class does not implement a `cast()` function');
321
321
  };
322
322
 
323
323
  /**
@@ -491,7 +491,7 @@ SchemaType.prototype.unique = function unique(value, message) {
491
491
  if (!value) {
492
492
  return;
493
493
  }
494
- throw new Error('Path "' + this.path + '" may not have `index` set to ' +
494
+ throw new MongooseError('Path "' + this.path + '" may not have `index` set to ' +
495
495
  'false and `unique` set to true');
496
496
  }
497
497
 
@@ -530,7 +530,7 @@ SchemaType.prototype.text = function(bool) {
530
530
  if (!bool) {
531
531
  return this;
532
532
  }
533
- throw new Error('Path "' + this.path + '" may not have `index` set to ' +
533
+ throw new MongooseError('Path "' + this.path + '" may not have `index` set to ' +
534
534
  'false and `text` set to true');
535
535
  }
536
536
 
@@ -567,7 +567,7 @@ SchemaType.prototype.sparse = function(bool) {
567
567
  if (!bool) {
568
568
  return this;
569
569
  }
570
- throw new Error('Path "' + this.path + '" may not have `index` set to ' +
570
+ throw new MongooseError('Path "' + this.path + '" may not have `index` set to ' +
571
571
  'false and `sparse` set to true');
572
572
  }
573
573
 
@@ -1003,7 +1003,7 @@ SchemaType.prototype.validate = function(obj, message, type) {
1003
1003
  + arg
1004
1004
  + '. See https://mongoosejs.com/docs/api/schematype.html#SchemaType.prototype.validate()';
1005
1005
 
1006
- throw new Error(msg);
1006
+ throw new MongooseError(msg);
1007
1007
  }
1008
1008
  this.validate(arg.validator, arg);
1009
1009
  }
@@ -1704,7 +1704,7 @@ SchemaType.prototype.castForQuery = function($conditional, val, context) {
1704
1704
  if ($conditional != null) {
1705
1705
  handler = this.$conditionalHandlers[$conditional];
1706
1706
  if (!handler) {
1707
- throw new Error('Can\'t use ' + $conditional);
1707
+ throw new MongooseError('Can\'t use ' + $conditional);
1708
1708
  }
1709
1709
  return handler.call(this, val, context);
1710
1710
  }
@@ -1820,7 +1820,7 @@ SchemaType.prototype._duplicateKeyErrorMessage = null;
1820
1820
  */
1821
1821
 
1822
1822
  SchemaType.prototype.toJSONSchema = function toJSONSchema(_options) { // eslint-disable-line no-unused-vars
1823
- throw new Error(`Converting unsupported SchemaType to JSON Schema: ${this.instance} at path "${this.path}"`);
1823
+ throw new MongooseError(`Converting unsupported SchemaType to JSON Schema: ${this.instance} at path "${this.path}"`);
1824
1824
  };
1825
1825
 
1826
1826
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "9.3.1",
4
+ "version": "9.3.3",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -103,7 +103,7 @@ declare module 'mongoose' {
103
103
  ? T
104
104
  : Omit<T, keyof U> & U;
105
105
 
106
- type MergeType<A, B> = Omit<A, keyof B> & B;
106
+ type MergeType<A, B> = A extends unknown ? Omit<A, keyof B> & B : never;
107
107
 
108
108
  /**
109
109
  * @summary Converts Unions to one record "object".