mongoose 9.3.1 → 9.3.2
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 +16 -18
- package/lib/cast/number.js +4 -4
- package/lib/cast.js +1 -1
- package/lib/connection.js +6 -6
- package/lib/helpers/get.js +1 -1
- package/lib/mongoose.js +6 -0
- package/lib/query.js +6 -6
- package/package.json +1 -1
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](
|
|
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](
|
|
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](
|
|
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](
|
|
144
|
-
* [Defaults](
|
|
145
|
-
* [Getters](
|
|
146
|
-
* [Setters](
|
|
147
|
-
* [Indexes](
|
|
148
|
-
* [Middleware](
|
|
149
|
-
* [Methods](
|
|
150
|
-
* [Statics](
|
|
151
|
-
* [Plugins](
|
|
152
|
-
* [pseudo-JOINs](
|
|
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](
|
|
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](
|
|
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](
|
|
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/cast/number.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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 '
|
|
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
|
|
135
|
+
throw new MongooseError('Invalid connection state: ' + val);
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
if (this._readyState !== val) {
|
|
@@ -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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
1569
|
+
throw new MongooseError('First parameter to `deleteModel()` must be a string ' +
|
|
1570
1570
|
'or regexp, got "' + name + '"');
|
|
1571
1571
|
}
|
|
1572
1572
|
|
package/lib/helpers/get.js
CHANGED
package/lib/mongoose.js
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
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
|
|
3083
|
+
throw new MongooseError('sort() takes at most 2 arguments');
|
|
3084
3084
|
}
|
|
3085
3085
|
if (options != null && typeof options !== 'object') {
|
|
3086
|
-
throw new
|
|
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
|
|
4698
|
+
throw new MongooseError('Invalid field "" passed to sort()');
|
|
4699
4699
|
}
|
|
4700
4700
|
|
|
4701
4701
|
if (this._execCount > 0) {
|