mongoose 8.9.0 → 8.9.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/dist/browser.umd.js +1 -1
- package/index.js +0 -1
- package/lib/aggregate.js +21 -4
- package/lib/connection.js +41 -19
- package/lib/cursor/aggregationCursor.js +1 -2
- package/lib/document.js +11 -8
- package/lib/drivers/node-mongodb-native/collection.js +0 -1
- package/lib/helpers/indexes/isIndexSpecEqual.js +32 -0
- package/lib/helpers/model/discriminator.js +1 -0
- package/lib/model.js +17 -15
- package/lib/mongoose.js +18 -20
- package/lib/query.js +1 -22
- package/lib/schema/string.js +10 -10
- package/lib/schema.js +27 -9
- package/lib/types/map.js +8 -1
- package/package.json +3 -3
- package/types/index.d.ts +57 -50
package/index.js
CHANGED
|
@@ -46,7 +46,6 @@ module.exports.Decimal128 = mongoose.Decimal128;
|
|
|
46
46
|
module.exports.Mixed = mongoose.Mixed;
|
|
47
47
|
module.exports.Date = mongoose.Date;
|
|
48
48
|
module.exports.Number = mongoose.Number;
|
|
49
|
-
module.exports.Double = mongoose.Double;
|
|
50
49
|
module.exports.Error = mongoose.Error;
|
|
51
50
|
module.exports.MongooseError = mongoose.MongooseError;
|
|
52
51
|
module.exports.now = mongoose.now;
|
package/lib/aggregate.js
CHANGED
|
@@ -87,6 +87,24 @@ function Aggregate(pipeline, model) {
|
|
|
87
87
|
|
|
88
88
|
Aggregate.prototype.options;
|
|
89
89
|
|
|
90
|
+
/**
|
|
91
|
+
* Returns default options for this aggregate.
|
|
92
|
+
*
|
|
93
|
+
* @param {Model} model
|
|
94
|
+
* @api private
|
|
95
|
+
*/
|
|
96
|
+
|
|
97
|
+
Aggregate.prototype._optionsForExec = function() {
|
|
98
|
+
const options = this.options || {};
|
|
99
|
+
|
|
100
|
+
const asyncLocalStorage = this.model()?.db?.base.transactionAsyncLocalStorage?.getStore();
|
|
101
|
+
if (!options.hasOwnProperty('session') && asyncLocalStorage?.session != null) {
|
|
102
|
+
options.session = asyncLocalStorage.session;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return options;
|
|
106
|
+
};
|
|
107
|
+
|
|
90
108
|
/**
|
|
91
109
|
* Get/set the model that this aggregation will execute on.
|
|
92
110
|
*
|
|
@@ -914,6 +932,7 @@ Aggregate.prototype.option = function(value) {
|
|
|
914
932
|
*/
|
|
915
933
|
|
|
916
934
|
Aggregate.prototype.cursor = function(options) {
|
|
935
|
+
this._optionsForExec();
|
|
917
936
|
this.options.cursor = options || {};
|
|
918
937
|
return new AggregationCursor(this); // return this;
|
|
919
938
|
};
|
|
@@ -1022,10 +1041,7 @@ Aggregate.prototype.exec = async function exec() {
|
|
|
1022
1041
|
applyGlobalMaxTimeMS(this.options, model.db.options, model.base.options);
|
|
1023
1042
|
applyGlobalDiskUse(this.options, model.db.options, model.base.options);
|
|
1024
1043
|
|
|
1025
|
-
|
|
1026
|
-
if (!this.options.hasOwnProperty('session') && asyncLocalStorage?.session != null) {
|
|
1027
|
-
this.options.session = asyncLocalStorage.session;
|
|
1028
|
-
}
|
|
1044
|
+
this._optionsForExec();
|
|
1029
1045
|
|
|
1030
1046
|
if (this.options && this.options.cursor) {
|
|
1031
1047
|
return new AggregationCursor(this);
|
|
@@ -1052,6 +1068,7 @@ Aggregate.prototype.exec = async function exec() {
|
|
|
1052
1068
|
}
|
|
1053
1069
|
|
|
1054
1070
|
const options = clone(this.options || {});
|
|
1071
|
+
|
|
1055
1072
|
let result;
|
|
1056
1073
|
try {
|
|
1057
1074
|
const cursor = await collection.aggregate(this._pipeline, options);
|
package/lib/connection.js
CHANGED
|
@@ -158,7 +158,7 @@ Object.defineProperty(Connection.prototype, 'readyState', {
|
|
|
158
158
|
* @api public
|
|
159
159
|
*/
|
|
160
160
|
|
|
161
|
-
Connection.prototype.get = function(key) {
|
|
161
|
+
Connection.prototype.get = function getOption(key) {
|
|
162
162
|
if (this.config.hasOwnProperty(key)) {
|
|
163
163
|
return this.config[key];
|
|
164
164
|
}
|
|
@@ -186,7 +186,7 @@ Connection.prototype.get = function(key) {
|
|
|
186
186
|
* @api public
|
|
187
187
|
*/
|
|
188
188
|
|
|
189
|
-
Connection.prototype.set = function(key, val) {
|
|
189
|
+
Connection.prototype.set = function setOption(key, val) {
|
|
190
190
|
if (this.config.hasOwnProperty(key)) {
|
|
191
191
|
this.config[key] = val;
|
|
192
192
|
return val;
|
|
@@ -663,7 +663,7 @@ Connection.prototype.withSession = async function withSession(executor) {
|
|
|
663
663
|
*
|
|
664
664
|
* const session = await conn.startSession();
|
|
665
665
|
* let doc = await Person.findOne({ name: 'Ned Stark' }, null, { session });
|
|
666
|
-
* await doc.
|
|
666
|
+
* await doc.deleteOne();
|
|
667
667
|
* // `doc` will always be null, even if reading from a replica set
|
|
668
668
|
* // secondary. Without causal consistency, it is possible to
|
|
669
669
|
* // get a doc back from the below query if the query reads from a
|
|
@@ -925,7 +925,7 @@ Connection.prototype._shouldBufferCommands = function _shouldBufferCommands() {
|
|
|
925
925
|
* @api private
|
|
926
926
|
*/
|
|
927
927
|
|
|
928
|
-
Connection.prototype.error = function(err, callback) {
|
|
928
|
+
Connection.prototype.error = function error(err, callback) {
|
|
929
929
|
if (callback) {
|
|
930
930
|
callback(err);
|
|
931
931
|
return null;
|
|
@@ -939,6 +939,7 @@ Connection.prototype.error = function(err, callback) {
|
|
|
939
939
|
/**
|
|
940
940
|
* Called when the connection is opened
|
|
941
941
|
*
|
|
942
|
+
* @emits "open"
|
|
942
943
|
* @api private
|
|
943
944
|
*/
|
|
944
945
|
|
|
@@ -1043,11 +1044,21 @@ Connection.prototype.openUri = async function openUri(uri, options) {
|
|
|
1043
1044
|
return this;
|
|
1044
1045
|
};
|
|
1045
1046
|
|
|
1046
|
-
|
|
1047
|
-
*
|
|
1048
|
-
*
|
|
1047
|
+
/**
|
|
1048
|
+
* Listen to events in the Connection
|
|
1049
|
+
*
|
|
1050
|
+
* @param {String} event The event to listen on
|
|
1051
|
+
* @param {Function} callback
|
|
1052
|
+
* @see Connection#readyState https://mongoosejs.com/docs/api/connection.html#Connection.prototype.readyState
|
|
1053
|
+
*
|
|
1054
|
+
* @method on
|
|
1055
|
+
* @instance
|
|
1056
|
+
* @memberOf Connection
|
|
1057
|
+
* @api public
|
|
1049
1058
|
*/
|
|
1050
1059
|
|
|
1060
|
+
// Treat `on('error')` handlers as handling the initialConnection promise
|
|
1061
|
+
// to avoid uncaught exceptions when using `on('error')`. See gh-14377.
|
|
1051
1062
|
Connection.prototype.on = function on(event, callback) {
|
|
1052
1063
|
if (event === 'error' && this.$initialConnection) {
|
|
1053
1064
|
this.$initialConnection.catch(() => {});
|
|
@@ -1055,11 +1066,21 @@ Connection.prototype.on = function on(event, callback) {
|
|
|
1055
1066
|
return EventEmitter.prototype.on.call(this, event, callback);
|
|
1056
1067
|
};
|
|
1057
1068
|
|
|
1058
|
-
|
|
1059
|
-
*
|
|
1060
|
-
*
|
|
1069
|
+
/**
|
|
1070
|
+
* Listen to a event once in the Connection
|
|
1071
|
+
*
|
|
1072
|
+
* @param {String} event The event to listen on
|
|
1073
|
+
* @param {Function} callback
|
|
1074
|
+
* @see Connection#readyState https://mongoosejs.com/docs/api/connection.html#Connection.prototype.readyState
|
|
1075
|
+
*
|
|
1076
|
+
* @method once
|
|
1077
|
+
* @instance
|
|
1078
|
+
* @memberOf Connection
|
|
1079
|
+
* @api public
|
|
1061
1080
|
*/
|
|
1062
1081
|
|
|
1082
|
+
// Treat `on('error')` handlers as handling the initialConnection promise
|
|
1083
|
+
// to avoid uncaught exceptions when using `on('error')`. See gh-14377.
|
|
1063
1084
|
Connection.prototype.once = function on(event, callback) {
|
|
1064
1085
|
if (event === 'error' && this.$initialConnection) {
|
|
1065
1086
|
this.$initialConnection.catch(() => {});
|
|
@@ -1220,17 +1241,18 @@ Connection.prototype._close = async function _close(force, destroy) {
|
|
|
1220
1241
|
* @api private
|
|
1221
1242
|
*/
|
|
1222
1243
|
|
|
1223
|
-
Connection.prototype.doClose = function() {
|
|
1244
|
+
Connection.prototype.doClose = function doClose() {
|
|
1224
1245
|
throw new Error('Connection#doClose unimplemented by driver');
|
|
1225
1246
|
};
|
|
1226
1247
|
|
|
1227
1248
|
/**
|
|
1228
1249
|
* Called when the connection closes
|
|
1229
1250
|
*
|
|
1251
|
+
* @emits "close"
|
|
1230
1252
|
* @api private
|
|
1231
1253
|
*/
|
|
1232
1254
|
|
|
1233
|
-
Connection.prototype.onClose = function(force) {
|
|
1255
|
+
Connection.prototype.onClose = function onClose(force) {
|
|
1234
1256
|
this.readyState = STATES.disconnected;
|
|
1235
1257
|
|
|
1236
1258
|
// avoid having the collection subscribe to our event emitter
|
|
@@ -1334,7 +1356,7 @@ Connection.prototype.plugin = function(fn, opts) {
|
|
|
1334
1356
|
* @api public
|
|
1335
1357
|
*/
|
|
1336
1358
|
|
|
1337
|
-
Connection.prototype.model = function(name, schema, collection, options) {
|
|
1359
|
+
Connection.prototype.model = function model(name, schema, collection, options) {
|
|
1338
1360
|
if (!(this instanceof Connection)) {
|
|
1339
1361
|
throw new MongooseError('`connection.model()` should not be run with ' +
|
|
1340
1362
|
'`new`. If you are doing `new db.model(foo)(bar)`, use ' +
|
|
@@ -1454,7 +1476,7 @@ Connection.prototype.model = function(name, schema, collection, options) {
|
|
|
1454
1476
|
* @return {Connection} this
|
|
1455
1477
|
*/
|
|
1456
1478
|
|
|
1457
|
-
Connection.prototype.deleteModel = function(name) {
|
|
1479
|
+
Connection.prototype.deleteModel = function deleteModel(name) {
|
|
1458
1480
|
if (typeof name === 'string') {
|
|
1459
1481
|
const model = this.model(name);
|
|
1460
1482
|
if (model == null) {
|
|
@@ -1510,7 +1532,7 @@ Connection.prototype.deleteModel = function(name) {
|
|
|
1510
1532
|
* @return {ChangeStream} mongoose-specific change stream wrapper, inherits from EventEmitter
|
|
1511
1533
|
*/
|
|
1512
1534
|
|
|
1513
|
-
Connection.prototype.watch = function(pipeline, options) {
|
|
1535
|
+
Connection.prototype.watch = function watch(pipeline, options) {
|
|
1514
1536
|
const changeStreamThunk = cb => {
|
|
1515
1537
|
immediate(() => {
|
|
1516
1538
|
if (this.readyState === STATES.connecting) {
|
|
@@ -1559,7 +1581,7 @@ Connection.prototype.asPromise = async function asPromise() {
|
|
|
1559
1581
|
* @return {String[]}
|
|
1560
1582
|
*/
|
|
1561
1583
|
|
|
1562
|
-
Connection.prototype.modelNames = function() {
|
|
1584
|
+
Connection.prototype.modelNames = function modelNames() {
|
|
1563
1585
|
return Object.keys(this.models);
|
|
1564
1586
|
};
|
|
1565
1587
|
|
|
@@ -1571,7 +1593,7 @@ Connection.prototype.modelNames = function() {
|
|
|
1571
1593
|
* @api private
|
|
1572
1594
|
* @return {Boolean} true if the connection should be authenticated after it is opened, otherwise false.
|
|
1573
1595
|
*/
|
|
1574
|
-
Connection.prototype.shouldAuthenticate = function() {
|
|
1596
|
+
Connection.prototype.shouldAuthenticate = function shouldAuthenticate() {
|
|
1575
1597
|
return this.user != null &&
|
|
1576
1598
|
(this.pass != null || this.authMechanismDoesNotRequirePassword());
|
|
1577
1599
|
};
|
|
@@ -1584,7 +1606,7 @@ Connection.prototype.shouldAuthenticate = function() {
|
|
|
1584
1606
|
* @return {Boolean} true if the authentication mechanism specified in the options object requires
|
|
1585
1607
|
* a password, otherwise false.
|
|
1586
1608
|
*/
|
|
1587
|
-
Connection.prototype.authMechanismDoesNotRequirePassword = function() {
|
|
1609
|
+
Connection.prototype.authMechanismDoesNotRequirePassword = function authMechanismDoesNotRequirePassword() {
|
|
1588
1610
|
if (this.options && this.options.auth) {
|
|
1589
1611
|
return noPasswordAuthMechanisms.indexOf(this.options.auth.authMechanism) >= 0;
|
|
1590
1612
|
}
|
|
@@ -1602,7 +1624,7 @@ Connection.prototype.authMechanismDoesNotRequirePassword = function() {
|
|
|
1602
1624
|
* @return {Boolean} true if the provided options object provides enough data to authenticate with,
|
|
1603
1625
|
* otherwise false.
|
|
1604
1626
|
*/
|
|
1605
|
-
Connection.prototype.optionsProvideAuthenticationData = function(options) {
|
|
1627
|
+
Connection.prototype.optionsProvideAuthenticationData = function optionsProvideAuthenticationData(options) {
|
|
1606
1628
|
return (options) &&
|
|
1607
1629
|
(options.user) &&
|
|
1608
1630
|
((options.pass) || this.authMechanismDoesNotRequirePassword());
|
|
@@ -175,11 +175,10 @@ AggregationCursor.prototype._markError = function(error) {
|
|
|
175
175
|
* Marks this cursor as closed. Will stop streaming and subsequent calls to
|
|
176
176
|
* `next()` will error.
|
|
177
177
|
*
|
|
178
|
-
* @param {Function} callback
|
|
179
178
|
* @return {Promise}
|
|
180
179
|
* @api public
|
|
181
180
|
* @method close
|
|
182
|
-
* @emits close
|
|
181
|
+
* @emits "close"
|
|
183
182
|
* @see AggregationCursor.close https://mongodb.github.io/node-mongodb-native/4.9/classes/AggregationCursor.html#close
|
|
184
183
|
*/
|
|
185
184
|
|
package/lib/document.js
CHANGED
|
@@ -831,7 +831,7 @@ function init(self, obj, doc, opts, prefix) {
|
|
|
831
831
|
*
|
|
832
832
|
* #### Example:
|
|
833
833
|
*
|
|
834
|
-
* weirdCar.updateOne({$inc: {wheels:1}}, { w: 1 }
|
|
834
|
+
* weirdCar.updateOne({$inc: {wheels:1}}, { w: 1 });
|
|
835
835
|
*
|
|
836
836
|
* #### Valid options:
|
|
837
837
|
*
|
|
@@ -843,7 +843,6 @@ function init(self, obj, doc, opts, prefix) {
|
|
|
843
843
|
* @param {Object} [options.lean] if truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. See [`Query.lean()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.lean()) and the [Mongoose lean tutorial](https://mongoosejs.com/docs/tutorials/lean.html).
|
|
844
844
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
845
845
|
* @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.
|
|
846
|
-
* @param {Function} [callback]
|
|
847
846
|
* @return {Query}
|
|
848
847
|
* @api public
|
|
849
848
|
* @memberOf Document
|
|
@@ -1777,6 +1776,11 @@ Document.prototype.$__set = function(pathToMark, path, options, constructing, pa
|
|
|
1777
1776
|
*/
|
|
1778
1777
|
|
|
1779
1778
|
Document.prototype.$__getValue = function(path) {
|
|
1779
|
+
if (typeof path !== 'string' && !Array.isArray(path)) {
|
|
1780
|
+
throw new TypeError(
|
|
1781
|
+
`Invalid \`path\`. Must be either string or array. Got "${path}" (type ${typeof path})`
|
|
1782
|
+
);
|
|
1783
|
+
}
|
|
1780
1784
|
return utils.getValue(path, this._doc);
|
|
1781
1785
|
};
|
|
1782
1786
|
|
|
@@ -2353,17 +2357,17 @@ Document.prototype.$isDefault = function(path) {
|
|
|
2353
2357
|
};
|
|
2354
2358
|
|
|
2355
2359
|
/**
|
|
2356
|
-
* Getter/setter, determines whether the document was
|
|
2360
|
+
* Getter/setter, determines whether the document was deleted. The `Model.prototype.deleteOne()` method sets `$isDeleted` if the delete operation succeeded.
|
|
2357
2361
|
*
|
|
2358
2362
|
* #### Example:
|
|
2359
2363
|
*
|
|
2360
|
-
* const product = await product.
|
|
2364
|
+
* const product = await product.deleteOne();
|
|
2361
2365
|
* product.$isDeleted(); // true
|
|
2362
|
-
* product.
|
|
2366
|
+
* product.deleteOne(); // no-op, doesn't send anything to the db
|
|
2363
2367
|
*
|
|
2364
2368
|
* product.$isDeleted(false);
|
|
2365
2369
|
* product.$isDeleted(); // false
|
|
2366
|
-
* product.
|
|
2370
|
+
* product.deleteOne(); // will execute a remove against the db
|
|
2367
2371
|
*
|
|
2368
2372
|
*
|
|
2369
2373
|
* @param {Boolean} [val] optional, overrides whether mongoose thinks the doc is deleted
|
|
@@ -3439,12 +3443,11 @@ function _checkImmutableSubpaths(subdoc, schematype, priorVal) {
|
|
|
3439
3443
|
* @param {Number} [options.wtimeout] sets a [timeout for the write concern](https://www.mongodb.com/docs/manual/reference/write-concern/#wtimeout). Overrides the [schema-level `writeConcern` option](https://mongoosejs.com/docs/guide.html#writeConcern).
|
|
3440
3444
|
* @param {Boolean} [options.checkKeys=true] the MongoDB driver prevents you from saving keys that start with '$' or contain '.' by default. Set this option to `false` to skip that check. See [restrictions on field names](https://www.mongodb.com/docs/manual/reference/limits/#Restrictions-on-Field-Names)
|
|
3441
3445
|
* @param {Boolean} [options.timestamps=true] if `false` and [timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this `save()`.
|
|
3442
|
-
* @param {Function} [fn] optional callback
|
|
3443
3446
|
* @method save
|
|
3444
3447
|
* @memberOf Document
|
|
3445
3448
|
* @instance
|
|
3446
3449
|
* @throws {DocumentNotFoundError} if this [save updates an existing document](https://mongoosejs.com/docs/api/document.html#Document.prototype.isNew()) but the document doesn't exist in the database. For example, you will get this error if the document is [deleted between when you retrieved the document and when you saved it](documents.html#updating).
|
|
3447
|
-
* @return {Promise
|
|
3450
|
+
* @return {Promise}
|
|
3448
3451
|
* @api public
|
|
3449
3452
|
* @see middleware https://mongoosejs.com/docs/middleware.html
|
|
3450
3453
|
*/
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Compares two index specifications to determine if they are equal.
|
|
5
|
+
*
|
|
6
|
+
* #### Example:
|
|
7
|
+
* isIndexSpecEqual({ a: 1, b: 1 }, { a: 1, b: 1 }); // true
|
|
8
|
+
* isIndexSpecEqual({ a: 1, b: 1 }, { b: 1, a: 1 }); // false
|
|
9
|
+
* isIndexSpecEqual({ a: 1, b: -1 }, { a: 1, b: 1 }); // false
|
|
10
|
+
*
|
|
11
|
+
* @param {Object} spec1 The first index specification to compare.
|
|
12
|
+
* @param {Object} spec2 The second index specification to compare.
|
|
13
|
+
* @returns {Boolean} Returns true if the index specifications are equal, otherwise returns false.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
module.exports = function isIndexSpecEqual(spec1, spec2) {
|
|
17
|
+
const spec1Keys = Object.keys(spec1);
|
|
18
|
+
const spec2Keys = Object.keys(spec2);
|
|
19
|
+
|
|
20
|
+
if (spec1Keys.length !== spec2Keys.length) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
for (let i = 0; i < spec1Keys.length; i++) {
|
|
25
|
+
const key = spec1Keys[i];
|
|
26
|
+
if (key !== spec2Keys[i] || spec1[key] !== spec2[key]) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return true;
|
|
32
|
+
};
|
|
@@ -119,6 +119,7 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
|
|
|
119
119
|
// schema. `Schema.prototype.clone()` copies `obj` by reference, no cloning.
|
|
120
120
|
schema.obj = { ...schema.obj };
|
|
121
121
|
mergeDiscriminatorSchema(schema, baseSchema);
|
|
122
|
+
schema._gatherChildSchemas();
|
|
122
123
|
|
|
123
124
|
// Clean up conflicting paths _after_ merging re: gh-6076
|
|
124
125
|
for (const conflictingPath of conflictingPaths) {
|
package/lib/model.js
CHANGED
|
@@ -694,13 +694,20 @@ Model.prototype.$__where = function _where(where) {
|
|
|
694
694
|
};
|
|
695
695
|
|
|
696
696
|
/**
|
|
697
|
-
* Delete this document from the db.
|
|
697
|
+
* Delete this document from the db. Returns a Query instance containing a `deleteOne` operation by this document's `_id`.
|
|
698
698
|
*
|
|
699
699
|
* #### Example:
|
|
700
700
|
*
|
|
701
701
|
* await product.deleteOne();
|
|
702
702
|
* await Product.findById(product._id); // null
|
|
703
703
|
*
|
|
704
|
+
* Since `deleteOne()` returns a Query, the `deleteOne()` will **not** execute unless you use either `await`, `.then()`, `.catch()`, or [`.exec()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.exec())
|
|
705
|
+
*
|
|
706
|
+
* #### Example:
|
|
707
|
+
*
|
|
708
|
+
* product.deleteOne(); // Doesn't do anything
|
|
709
|
+
* product.deleteOne().exec(); // Deletes the document, returns a promise
|
|
710
|
+
*
|
|
704
711
|
* @return {Query} Query
|
|
705
712
|
* @api public
|
|
706
713
|
*/
|
|
@@ -1879,8 +1886,6 @@ Model.translateAliases = function translateAliases(fields, errorOnDuplicates) {
|
|
|
1879
1886
|
/**
|
|
1880
1887
|
* Deletes the first document that matches `conditions` from the collection.
|
|
1881
1888
|
* It returns an object with the property `deletedCount` indicating how many documents were deleted.
|
|
1882
|
-
* Behaves like `remove()`, but deletes at most one document regardless of the
|
|
1883
|
-
* `single` option.
|
|
1884
1889
|
*
|
|
1885
1890
|
* #### Example:
|
|
1886
1891
|
*
|
|
@@ -1914,8 +1919,6 @@ Model.deleteOne = function deleteOne(conditions, options) {
|
|
|
1914
1919
|
/**
|
|
1915
1920
|
* Deletes all of the documents that match `conditions` from the collection.
|
|
1916
1921
|
* It returns an object with the property `deletedCount` containing the number of documents deleted.
|
|
1917
|
-
* Behaves like `remove()`, but deletes all documents that match `conditions`
|
|
1918
|
-
* regardless of the `single` option.
|
|
1919
1922
|
*
|
|
1920
1923
|
* #### Example:
|
|
1921
1924
|
*
|
|
@@ -2231,7 +2234,7 @@ Model.$where = function $where() {
|
|
|
2231
2234
|
/**
|
|
2232
2235
|
* Issues a mongodb findOneAndUpdate command.
|
|
2233
2236
|
*
|
|
2234
|
-
* Finds a matching document, updates it according to the `update` arg, passing any `options
|
|
2237
|
+
* Finds a matching document, updates it according to the `update` arg, passing any `options`. A Query object is returned.
|
|
2235
2238
|
*
|
|
2236
2239
|
* #### Example:
|
|
2237
2240
|
*
|
|
@@ -2729,7 +2732,7 @@ Model.create = async function create(doc, options) {
|
|
|
2729
2732
|
* // operationType: 'delete',
|
|
2730
2733
|
* // ns: { db: 'mydb', coll: 'Person' },
|
|
2731
2734
|
* // documentKey: { _id: 5a51b125c5500f5aa094c7bd } }
|
|
2732
|
-
* await doc.
|
|
2735
|
+
* await doc.deleteOne();
|
|
2733
2736
|
*
|
|
2734
2737
|
* @param {Array} [pipeline]
|
|
2735
2738
|
* @param {Object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/4.9/classes/Collection.html#watch)
|
|
@@ -2777,7 +2780,7 @@ Model.watch = function(pipeline, options) {
|
|
|
2777
2780
|
*
|
|
2778
2781
|
* const session = await Person.startSession();
|
|
2779
2782
|
* let doc = await Person.findOne({ name: 'Ned Stark' }, null, { session });
|
|
2780
|
-
* await doc.
|
|
2783
|
+
* await doc.deleteOne();
|
|
2781
2784
|
* // `doc` will always be null, even if reading from a replica set
|
|
2782
2785
|
* // secondary. Without causal consistency, it is possible to
|
|
2783
2786
|
* // get a doc back from the below query if the query reads from a
|
|
@@ -3643,7 +3646,11 @@ Model.castObject = function castObject(obj, options) {
|
|
|
3643
3646
|
options = options || {};
|
|
3644
3647
|
const ret = {};
|
|
3645
3648
|
|
|
3646
|
-
|
|
3649
|
+
let schema = this.schema;
|
|
3650
|
+
const discriminatorKey = schema.options.discriminatorKey;
|
|
3651
|
+
if (schema.discriminators != null && obj != null && obj[discriminatorKey] != null) {
|
|
3652
|
+
schema = getSchemaDiscriminatorByValue(schema, obj[discriminatorKey]) || schema;
|
|
3653
|
+
}
|
|
3647
3654
|
const paths = Object.keys(schema.paths);
|
|
3648
3655
|
|
|
3649
3656
|
for (const path of paths) {
|
|
@@ -3992,7 +3999,7 @@ function _update(model, op, conditions, doc, options) {
|
|
|
3992
3999
|
/**
|
|
3993
4000
|
* Performs [aggregations](https://www.mongodb.com/docs/manual/aggregation/) on the models collection.
|
|
3994
4001
|
*
|
|
3995
|
-
*
|
|
4002
|
+
* The `aggregate` itself is returned.
|
|
3996
4003
|
*
|
|
3997
4004
|
* This function triggers the following middleware.
|
|
3998
4005
|
*
|
|
@@ -4047,10 +4054,6 @@ Model.aggregate = function aggregate(pipeline, options) {
|
|
|
4047
4054
|
aggregate.option(options);
|
|
4048
4055
|
}
|
|
4049
4056
|
|
|
4050
|
-
if (typeof callback === 'undefined') {
|
|
4051
|
-
return aggregate;
|
|
4052
|
-
}
|
|
4053
|
-
|
|
4054
4057
|
return aggregate;
|
|
4055
4058
|
};
|
|
4056
4059
|
|
|
@@ -4238,7 +4241,6 @@ Model.validate = async function validate(obj, pathsOrOptions, context) {
|
|
|
4238
4241
|
* @param {Object} [options.options=null] Additional options like `limit` and `lean`.
|
|
4239
4242
|
* @param {Function} [options.transform=null] Function that Mongoose will call on every populated document that allows you to transform the populated document.
|
|
4240
4243
|
* @param {Boolean} [options.forceRepopulate=true] Set to `false` to prevent Mongoose from repopulating paths that are already populated
|
|
4241
|
-
* @param {Function} [callback(err,doc)] Optional callback, executed upon completion. Receives `err` and the `doc(s)`.
|
|
4242
4244
|
* @return {Promise}
|
|
4243
4245
|
* @api public
|
|
4244
4246
|
*/
|
package/lib/mongoose.js
CHANGED
|
@@ -245,7 +245,7 @@ Mongoose.prototype.setDriver = function setDriver(driver) {
|
|
|
245
245
|
* @api public
|
|
246
246
|
*/
|
|
247
247
|
|
|
248
|
-
Mongoose.prototype.set = function(key, value) {
|
|
248
|
+
Mongoose.prototype.set = function getsetOptions(key, value) {
|
|
249
249
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
250
250
|
|
|
251
251
|
if (arguments.length === 1 && typeof key !== 'object') {
|
|
@@ -376,7 +376,7 @@ Mongoose.prototype.get = Mongoose.prototype.set;
|
|
|
376
376
|
* @api public
|
|
377
377
|
*/
|
|
378
378
|
|
|
379
|
-
Mongoose.prototype.createConnection = function(uri, options) {
|
|
379
|
+
Mongoose.prototype.createConnection = function createConnection(uri, options) {
|
|
380
380
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
381
381
|
|
|
382
382
|
const Connection = _mongoose.__driver.Connection;
|
|
@@ -427,7 +427,6 @@ Mongoose.prototype.createConnection = function(uri, options) {
|
|
|
427
427
|
* @param {Number} [options.socketTimeoutMS=0] How long the MongoDB driver will wait before killing a socket due to inactivity _after initial connection_. A socket may be inactive because of either no activity or a long-running operation. `socketTimeoutMS` defaults to 0, which means Node.js will not time out the socket due to inactivity. This option is passed to [Node.js `socket#setTimeout()` function](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback) after the MongoDB driver successfully completes.
|
|
428
428
|
* @param {Number} [options.family=0] Passed transparently to [Node.js' `dns.lookup()`](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback) function. May be either `0`, `4`, or `6`. `4` means use IPv4 only, `6` means use IPv6 only, `0` means try both.
|
|
429
429
|
* @param {Boolean} [options.autoCreate=false] Set to `true` to make Mongoose automatically call `createCollection()` on every model created on this connection.
|
|
430
|
-
* @param {Function} [callback]
|
|
431
430
|
* @see Mongoose#createConnection https://mongoosejs.com/docs/api/mongoose.html#Mongoose.prototype.createConnection()
|
|
432
431
|
* @api public
|
|
433
432
|
* @return {Promise} resolves to `this` if connection succeeded
|
|
@@ -479,12 +478,11 @@ Mongoose.prototype.disconnect = async function disconnect() {
|
|
|
479
478
|
*
|
|
480
479
|
* @param {Object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/4.9/classes/MongoClient.html#startSession)
|
|
481
480
|
* @param {Boolean} [options.causalConsistency=true] set to false to disable causal consistency
|
|
482
|
-
* @param {Function} [callback]
|
|
483
481
|
* @return {Promise<ClientSession>} promise that resolves to a MongoDB driver `ClientSession`
|
|
484
482
|
* @api public
|
|
485
483
|
*/
|
|
486
484
|
|
|
487
|
-
Mongoose.prototype.startSession = function() {
|
|
485
|
+
Mongoose.prototype.startSession = function startSession() {
|
|
488
486
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
489
487
|
|
|
490
488
|
return _mongoose.connection.startSession.apply(_mongoose.connection, arguments);
|
|
@@ -498,7 +496,7 @@ Mongoose.prototype.startSession = function() {
|
|
|
498
496
|
* @api public
|
|
499
497
|
*/
|
|
500
498
|
|
|
501
|
-
Mongoose.prototype.pluralize = function(fn) {
|
|
499
|
+
Mongoose.prototype.pluralize = function pluralize(fn) {
|
|
502
500
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
503
501
|
|
|
504
502
|
if (arguments.length > 0) {
|
|
@@ -561,7 +559,7 @@ Mongoose.prototype.pluralize = function(fn) {
|
|
|
561
559
|
* @api public
|
|
562
560
|
*/
|
|
563
561
|
|
|
564
|
-
Mongoose.prototype.model = function(name, schema, collection, options) {
|
|
562
|
+
Mongoose.prototype.model = function model(name, schema, collection, options) {
|
|
565
563
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
566
564
|
|
|
567
565
|
if (typeof schema === 'string') {
|
|
@@ -572,7 +570,7 @@ Mongoose.prototype.model = function(name, schema, collection, options) {
|
|
|
572
570
|
if (arguments.length === 1) {
|
|
573
571
|
const model = _mongoose.models[name];
|
|
574
572
|
if (!model) {
|
|
575
|
-
throw new
|
|
573
|
+
throw new _mongoose.Error.MissingSchemaError(name);
|
|
576
574
|
}
|
|
577
575
|
return model;
|
|
578
576
|
}
|
|
@@ -581,7 +579,7 @@ Mongoose.prototype.model = function(name, schema, collection, options) {
|
|
|
581
579
|
schema = new Schema(schema);
|
|
582
580
|
}
|
|
583
581
|
if (schema && !(schema instanceof Schema)) {
|
|
584
|
-
throw new Error('The 2nd parameter to `mongoose.model()` should be a ' +
|
|
582
|
+
throw new _mongoose.Error('The 2nd parameter to `mongoose.model()` should be a ' +
|
|
585
583
|
'schema or a POJO');
|
|
586
584
|
}
|
|
587
585
|
|
|
@@ -632,7 +630,7 @@ Mongoose.prototype.model = function(name, schema, collection, options) {
|
|
|
632
630
|
* ignore
|
|
633
631
|
*/
|
|
634
632
|
|
|
635
|
-
Mongoose.prototype._model = function(name, schema, collection, options) {
|
|
633
|
+
Mongoose.prototype._model = function _model(name, schema, collection, options) {
|
|
636
634
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
637
635
|
|
|
638
636
|
let model;
|
|
@@ -707,7 +705,7 @@ Mongoose.prototype._model = function(name, schema, collection, options) {
|
|
|
707
705
|
* @return {Mongoose} this
|
|
708
706
|
*/
|
|
709
707
|
|
|
710
|
-
Mongoose.prototype.deleteModel = function(name) {
|
|
708
|
+
Mongoose.prototype.deleteModel = function deleteModel(name) {
|
|
711
709
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
712
710
|
|
|
713
711
|
_mongoose.connection.deleteModel(name);
|
|
@@ -726,7 +724,7 @@ Mongoose.prototype.deleteModel = function(name) {
|
|
|
726
724
|
* @return {Array}
|
|
727
725
|
*/
|
|
728
726
|
|
|
729
|
-
Mongoose.prototype.modelNames = function() {
|
|
727
|
+
Mongoose.prototype.modelNames = function modelNames() {
|
|
730
728
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
731
729
|
|
|
732
730
|
const names = Object.keys(_mongoose.models);
|
|
@@ -740,7 +738,7 @@ Mongoose.prototype.modelNames = function() {
|
|
|
740
738
|
* @api private
|
|
741
739
|
*/
|
|
742
740
|
|
|
743
|
-
Mongoose.prototype._applyPlugins = function(schema, options) {
|
|
741
|
+
Mongoose.prototype._applyPlugins = function _applyPlugins(schema, options) {
|
|
744
742
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
745
743
|
|
|
746
744
|
options = options || {};
|
|
@@ -763,7 +761,7 @@ Mongoose.prototype._applyPlugins = function(schema, options) {
|
|
|
763
761
|
* @api public
|
|
764
762
|
*/
|
|
765
763
|
|
|
766
|
-
Mongoose.prototype.plugin = function(fn, opts) {
|
|
764
|
+
Mongoose.prototype.plugin = function plugin(fn, opts) {
|
|
767
765
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
768
766
|
|
|
769
767
|
_mongoose.plugins.push([fn, opts]);
|
|
@@ -1073,7 +1071,7 @@ Mongoose.prototype.ObjectId = SchemaTypes.ObjectId;
|
|
|
1073
1071
|
* @api public
|
|
1074
1072
|
*/
|
|
1075
1073
|
|
|
1076
|
-
Mongoose.prototype.isValidObjectId = function(v) {
|
|
1074
|
+
Mongoose.prototype.isValidObjectId = function isValidObjectId(v) {
|
|
1077
1075
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
1078
1076
|
return _mongoose.Types.ObjectId.isValid(v);
|
|
1079
1077
|
};
|
|
@@ -1105,7 +1103,7 @@ Mongoose.prototype.isValidObjectId = function(v) {
|
|
|
1105
1103
|
* @api public
|
|
1106
1104
|
*/
|
|
1107
1105
|
|
|
1108
|
-
Mongoose.prototype.isObjectIdOrHexString = function(v) {
|
|
1106
|
+
Mongoose.prototype.isObjectIdOrHexString = function isObjectIdOrHexString(v) {
|
|
1109
1107
|
return isBsonType(v, 'ObjectId') || (typeof v === 'string' && objectIdHexRegexp.test(v));
|
|
1110
1108
|
};
|
|
1111
1109
|
|
|
@@ -1117,7 +1115,7 @@ Mongoose.prototype.isObjectIdOrHexString = function(v) {
|
|
|
1117
1115
|
* @param {Boolean} options.continueOnError `false` by default. If set to `true`, mongoose will not throw an error if one model syncing failed, and will return an object where the keys are the names of the models, and the values are the results/errors for each model.
|
|
1118
1116
|
* @return {Promise} Returns a Promise, when the Promise resolves the value is a list of the dropped indexes.
|
|
1119
1117
|
*/
|
|
1120
|
-
Mongoose.prototype.syncIndexes = function(options) {
|
|
1118
|
+
Mongoose.prototype.syncIndexes = function syncIndexes(options) {
|
|
1121
1119
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
1122
1120
|
return _mongoose.connection.syncIndexes(options);
|
|
1123
1121
|
};
|
|
@@ -1192,8 +1190,8 @@ Mongoose.prototype.Number = SchemaTypes.Number;
|
|
|
1192
1190
|
* @api public
|
|
1193
1191
|
*/
|
|
1194
1192
|
|
|
1195
|
-
Mongoose.prototype.Error =
|
|
1196
|
-
Mongoose.prototype.MongooseError =
|
|
1193
|
+
Mongoose.prototype.Error = MongooseError;
|
|
1194
|
+
Mongoose.prototype.MongooseError = MongooseError;
|
|
1197
1195
|
|
|
1198
1196
|
/**
|
|
1199
1197
|
* Mongoose uses this function to get the current time when setting
|
|
@@ -1218,7 +1216,7 @@ Mongoose.prototype.now = function now() { return new Date(); };
|
|
|
1218
1216
|
* @api public
|
|
1219
1217
|
*/
|
|
1220
1218
|
|
|
1221
|
-
Mongoose.prototype.CastError =
|
|
1219
|
+
Mongoose.prototype.CastError = MongooseError.CastError;
|
|
1222
1220
|
|
|
1223
1221
|
/**
|
|
1224
1222
|
* The constructor used for schematype options
|