mongoose 9.11.2 → 9.11.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/lib/aggregate.js +0 -1
- package/lib/connection.js +9 -4
- package/lib/helpers/processConnectionOptions.js +6 -1
- package/lib/helpers/query/castUpdate.js +1 -1
- package/lib/helpers/update/applyTimestampsToUpdate.js +4 -3
- package/lib/model.js +7 -14
- package/lib/mongoose.js +1 -1
- package/lib/query.js +9 -15
- package/lib/schema/date.js +1 -1
- package/package.json +1 -1
package/lib/aggregate.js
CHANGED
package/lib/connection.js
CHANGED
|
@@ -565,8 +565,8 @@ Connection.prototype.bulkWrite = async function bulkWrite(ops, options) {
|
|
|
565
565
|
);
|
|
566
566
|
}
|
|
567
567
|
const BulkWriteResult = this.base.driver.get().BulkWriteResult;
|
|
568
|
-
const
|
|
569
|
-
return decorateBulkWriteResult(
|
|
568
|
+
const bulkWriteResult = new BulkWriteResult(getDefaultBulkwriteResult(), false);
|
|
569
|
+
return decorateBulkWriteResult(bulkWriteResult, validationErrors, results);
|
|
570
570
|
}
|
|
571
571
|
|
|
572
572
|
let error;
|
|
@@ -578,11 +578,16 @@ Connection.prototype.bulkWrite = async function bulkWrite(ops, options) {
|
|
|
578
578
|
results[validOpIndexes[i]] = null;
|
|
579
579
|
}
|
|
580
580
|
if (error) {
|
|
581
|
+
// client.bulkWrite() writeErrors is a Map keyed by index in validOps
|
|
582
|
+
error.writeErrors?.forEach((writeError, i) => {
|
|
583
|
+
results[validOpIndexes[i]] = writeError;
|
|
584
|
+
});
|
|
581
585
|
if (validationErrors.length > 0) {
|
|
582
586
|
decorateBulkWriteResult(error, validationErrors, results);
|
|
583
587
|
error.mongoose = error.mongoose || {};
|
|
584
588
|
error.mongoose.validationErrors = validationErrors;
|
|
585
589
|
}
|
|
590
|
+
throw error;
|
|
586
591
|
}
|
|
587
592
|
|
|
588
593
|
if (validationErrors.length > 0) {
|
|
@@ -661,7 +666,7 @@ Connection.prototype.withSession = async function withSession(executor) {
|
|
|
661
666
|
};
|
|
662
667
|
|
|
663
668
|
/**
|
|
664
|
-
*
|
|
669
|
+
* Starts a [MongoDB session](https://www.mongodb.com/docs/manual/reference/server-sessions/)
|
|
665
670
|
* for benefits like causal consistency, [retryable writes](https://www.mongodb.com/docs/manual/core/retryable-writes/),
|
|
666
671
|
* and [transactions](https://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html).
|
|
667
672
|
*
|
|
@@ -696,7 +701,7 @@ Connection.prototype.startSession = async function startSession(options) {
|
|
|
696
701
|
};
|
|
697
702
|
|
|
698
703
|
/**
|
|
699
|
-
*
|
|
704
|
+
* Executes the wrapped async function
|
|
700
705
|
* in a transaction. Mongoose will commit the transaction if the
|
|
701
706
|
* async function executes successfully and attempt to retry if
|
|
702
707
|
* there was a retriable error.
|
|
@@ -6,7 +6,7 @@ const MongooseError = require('../error/index');
|
|
|
6
6
|
function processConnectionOptions(uri, options) {
|
|
7
7
|
const opts = options ? options : {};
|
|
8
8
|
const readPreference = opts.readPreference
|
|
9
|
-
? opts.readPreference
|
|
9
|
+
? getReadPreferenceMode(opts.readPreference)
|
|
10
10
|
: getUriReadPreference(uri);
|
|
11
11
|
|
|
12
12
|
const clonedOpts = clone(opts);
|
|
@@ -40,6 +40,11 @@ function setsSecondaryRead(prefString) {
|
|
|
40
40
|
return !!(prefString === 'secondary' || prefString === 'secondaryPreferred');
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
function getReadPreferenceMode(readPreference) {
|
|
44
|
+
// the MongoDB driver also accepts a `ReadPreference` instance or a `{ mode }` object
|
|
45
|
+
return typeof readPreference === 'string' ? readPreference : readPreference.mode;
|
|
46
|
+
}
|
|
47
|
+
|
|
43
48
|
function getUriReadPreference(connectionString) {
|
|
44
49
|
const exp = /(?:&|\?)readPreference=(\w+)(?:&|$)/;
|
|
45
50
|
const match = exp.exec(connectionString);
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
const utils = require('../../utils');
|
|
8
|
+
const get = require('../get');
|
|
8
9
|
|
|
9
10
|
module.exports = applyTimestampsToUpdate;
|
|
10
11
|
|
|
@@ -81,9 +82,9 @@ function applyTimestampsToUpdate(now, createdAt, updatedAt, currentUpdate, optio
|
|
|
81
82
|
|
|
82
83
|
if (!skipCreatedAt && createdAt) {
|
|
83
84
|
const overwriteImmutable = options?.overwriteImmutable;
|
|
84
|
-
const hasUserCreatedAt = currentUpdate
|
|
85
|
-
|| currentUpdate.$set
|
|
86
|
-
|| currentUpdate.$setOnInsert
|
|
85
|
+
const hasUserCreatedAt = get(currentUpdate, createdAt) != null
|
|
86
|
+
|| get(currentUpdate.$set, createdAt) != null
|
|
87
|
+
|| get(currentUpdate.$setOnInsert, createdAt) != null;
|
|
87
88
|
|
|
88
89
|
// If overwriteImmutable is true and user provided createdAt, keep their value
|
|
89
90
|
if (overwriteImmutable && hasUserCreatedAt) {
|
package/lib/model.js
CHANGED
|
@@ -1321,7 +1321,7 @@ Model.createCollection = async function createCollection(options) {
|
|
|
1321
1321
|
* toCreate; // Array of strings containing names of indexes that `syncIndexes()` will create
|
|
1322
1322
|
*
|
|
1323
1323
|
* @param {object} [options] options to pass to `ensureIndexes()`
|
|
1324
|
-
* @param {boolean} [options.hideIndexes=false] set to `true` to hide indexes instead of dropping
|
|
1324
|
+
* @param {boolean} [options.hideIndexes=false] set to `true` to hide indexes instead of dropping
|
|
1325
1325
|
* @return {Promise}
|
|
1326
1326
|
* @api public
|
|
1327
1327
|
*/
|
|
@@ -1562,7 +1562,7 @@ function getIndexesToDrop(schema, schemaIndexes, dbIndexes) {
|
|
|
1562
1562
|
*
|
|
1563
1563
|
* @param {object} [options]
|
|
1564
1564
|
* @param {string[]} [options.toDrop] if specified, contains a list of index names to drop
|
|
1565
|
-
* @param {boolean} [options.hideIndexes=false] set to `true` to hide indexes instead of dropping
|
|
1565
|
+
* @param {boolean} [options.hideIndexes=false] set to `true` to hide indexes instead of dropping
|
|
1566
1566
|
* @return {Promise<string[]>} list of dropped or hidden index names
|
|
1567
1567
|
* @api public
|
|
1568
1568
|
*/
|
|
@@ -2454,7 +2454,7 @@ Model.$where = function $where() {
|
|
|
2454
2454
|
* @param {object|string|string[]} [options.projection=null] optional fields to return, see [`Query.prototype.select()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.select())
|
|
2455
2455
|
* @param {boolean} [options.new=false] if true, return the modified document rather than the original
|
|
2456
2456
|
* @param {object|string} [options.fields] Field selection. Equivalent to `.select(fields).findOneAndUpdate()`
|
|
2457
|
-
* @param {number} [options.maxTimeMS] puts a time limit on the query
|
|
2457
|
+
* @param {number} [options.maxTimeMS] puts a time limit on the query
|
|
2458
2458
|
* @param {object|string} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
|
|
2459
2459
|
* @param {boolean} [options.runValidators] if true, runs [update validators](https://mongoosejs.com/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema
|
|
2460
2460
|
* @param {boolean} [options.setDefaultsOnInsert=true] If `setDefaultsOnInsert` and `upsert` are true, mongoose will apply the [defaults](https://mongoosejs.com/docs/defaults.html) specified in the model's schema if a new document is created
|
|
@@ -2594,7 +2594,7 @@ Model.findByIdAndUpdate = function(id, update, options) {
|
|
|
2594
2594
|
* @param {boolean} [options.includeResultMetadata] if true, returns the full [ModifyResult from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/7.0/interfaces/ModifyResult.html) rather than just the document
|
|
2595
2595
|
* @param {object|string} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
|
|
2596
2596
|
* @param {object|string} [options.select] sets the document fields to return.
|
|
2597
|
-
* @param {number} [options.maxTimeMS] puts a time limit on the query
|
|
2597
|
+
* @param {number} [options.maxTimeMS] puts a time limit on the query
|
|
2598
2598
|
* @param {boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
|
|
2599
2599
|
* @return {Query}
|
|
2600
2600
|
* @api public
|
|
@@ -2674,7 +2674,7 @@ Model.findByIdAndDelete = function(id, options) {
|
|
|
2674
2674
|
* @param {object|string} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
|
|
2675
2675
|
* @param {boolean} [options.includeResultMetadata] if true, returns the full [ModifyResult from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/7.0/interfaces/ModifyResult.html) rather than just the document
|
|
2676
2676
|
* @param {object|string} [options.select] sets the document fields to return.
|
|
2677
|
-
* @param {number} [options.maxTimeMS] puts a time limit on the query
|
|
2677
|
+
* @param {number} [options.maxTimeMS] puts a time limit on the query
|
|
2678
2678
|
* @param {boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
|
|
2679
2679
|
* @return {Query}
|
|
2680
2680
|
* @api public
|
|
@@ -2938,8 +2938,7 @@ Model.insertOne = async function insertOne(doc, options) {
|
|
|
2938
2938
|
};
|
|
2939
2939
|
|
|
2940
2940
|
/**
|
|
2941
|
-
*
|
|
2942
|
-
* underlying collection for changes using
|
|
2941
|
+
* Watches the underlying collection for changes using
|
|
2943
2942
|
* [MongoDB change streams](https://www.mongodb.com/docs/manual/changeStreams/).
|
|
2944
2943
|
*
|
|
2945
2944
|
* This function does **not** trigger any middleware. In particular, it
|
|
@@ -2991,7 +2990,7 @@ Model.watch = function(pipeline, options) {
|
|
|
2991
2990
|
};
|
|
2992
2991
|
|
|
2993
2992
|
/**
|
|
2994
|
-
*
|
|
2993
|
+
* Starts a [MongoDB session](https://www.mongodb.com/docs/manual/reference/server-sessions/)
|
|
2995
2994
|
* for benefits like causal consistency, [retryable writes](https://www.mongodb.com/docs/manual/core/retryable-writes/),
|
|
2996
2995
|
* and [transactions](https://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html).
|
|
2997
2996
|
*
|
|
@@ -4431,12 +4430,6 @@ Model.validate = async function validate(obj, pathsOrOptions, context) {
|
|
|
4431
4430
|
continue;
|
|
4432
4431
|
}
|
|
4433
4432
|
|
|
4434
|
-
const pieces = path.indexOf('.') === -1 ? [path] : path.split('.');
|
|
4435
|
-
let cur = obj;
|
|
4436
|
-
for (let i = 0; i < pieces.length - 1; ++i) {
|
|
4437
|
-
cur = cur[pieces[i]];
|
|
4438
|
-
}
|
|
4439
|
-
|
|
4440
4433
|
const val = get(obj, path, void 0);
|
|
4441
4434
|
promises.push(
|
|
4442
4435
|
schemaType.doValidate(val, context, { path: path }).catch(err => {
|
package/lib/mongoose.js
CHANGED
|
@@ -497,7 +497,7 @@ Mongoose.prototype.disconnect = async function disconnect() {
|
|
|
497
497
|
};
|
|
498
498
|
|
|
499
499
|
/**
|
|
500
|
-
*
|
|
500
|
+
* Starts a [MongoDB session](https://www.mongodb.com/docs/manual/reference/server-sessions/)
|
|
501
501
|
* for benefits like causal consistency, [retryable writes](https://www.mongodb.com/docs/manual/core/retryable-writes/),
|
|
502
502
|
* and [transactions](https://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html).
|
|
503
503
|
*
|
package/lib/query.js
CHANGED
|
@@ -239,8 +239,6 @@ Object.defineProperty(Query.prototype, '_distinct', {
|
|
|
239
239
|
* mongoose.Query.use$geoWithin = false;
|
|
240
240
|
* ```
|
|
241
241
|
*
|
|
242
|
-
* MongoDB 2.4 deprecated the use of `$within`, replacing it with `$geoWithin`. Mongoose uses `$geoWithin` by default (which is 100% backward compatible with `$within`). If you are running an older version of MongoDB, set this flag to `false` so your `within()` queries continue to work.
|
|
243
|
-
*
|
|
244
242
|
* @see geoWithin https://www.mongodb.com/docs/manual/reference/operator/geoWithin/
|
|
245
243
|
* @default true
|
|
246
244
|
* @property use$geoWithin
|
|
@@ -1620,11 +1618,11 @@ Query.prototype.wtimeout = function wtimeout(ms) {
|
|
|
1620
1618
|
* #### Read Concern Level:
|
|
1621
1619
|
*
|
|
1622
1620
|
* ```
|
|
1623
|
-
* local
|
|
1624
|
-
* available
|
|
1625
|
-
* majority
|
|
1626
|
-
* linearizable
|
|
1627
|
-
* snapshot
|
|
1621
|
+
* local The query returns from the instance with no guarantee that the data has been written to a majority of the replica set members (i.e. may be rolled back).
|
|
1622
|
+
* available The query returns from the instance with no guarantee that the data has been written to a majority of the replica set members (i.e. may be rolled back).
|
|
1623
|
+
* majority The query returns the data that has been acknowledged by a majority of the replica set members. The documents returned by the read operation are durable, even in the event of failure.
|
|
1624
|
+
* linearizable The query returns data that reflects all successful majority-acknowledged writes that completed prior to the start of the read operation. The query may wait for concurrently executing writes to propagate to a majority of replica set members before returning results.
|
|
1625
|
+
* snapshot Only available for operations within multi-document transactions. Upon transaction commit with write concern "majority", the transaction operations are guaranteed to have read from a snapshot of majority-committed data.
|
|
1628
1626
|
* ```
|
|
1629
1627
|
*
|
|
1630
1628
|
* Aliases
|
|
@@ -1879,9 +1877,6 @@ Query.prototype.explain = function explain(verbose) {
|
|
|
1879
1877
|
* which allows the MongoDB server to use more than 100 MB for this query's `sort()`. This option can
|
|
1880
1878
|
* let you work around `QueryExceededMemoryLimitNoDiskUseAllowed` errors from the MongoDB server.
|
|
1881
1879
|
*
|
|
1882
|
-
* Note that this option requires MongoDB server >= 4.4. Setting this option is a no-op for MongoDB 4.2
|
|
1883
|
-
* and earlier.
|
|
1884
|
-
*
|
|
1885
1880
|
* Calling `query.allowDiskUse(v)` is equivalent to `query.setOptions({ allowDiskUse: v })`
|
|
1886
1881
|
*
|
|
1887
1882
|
* #### Example:
|
|
@@ -2687,7 +2682,7 @@ Query.prototype.merge = function(source) {
|
|
|
2687
2682
|
};
|
|
2688
2683
|
|
|
2689
2684
|
/**
|
|
2690
|
-
* Adds a collation to this op
|
|
2685
|
+
* Adds a collation to this op.
|
|
2691
2686
|
*
|
|
2692
2687
|
* @param {object} value
|
|
2693
2688
|
* @return {Query} this
|
|
@@ -3469,7 +3464,7 @@ function prepareDiscriminatorCriteria(query) {
|
|
|
3469
3464
|
* - `upsert`: bool - creates the object if it doesn't exist. defaults to false.
|
|
3470
3465
|
* - `fields`: {Object|String} - Field selection. Equivalent to `.select(fields).findOneAndUpdate()`
|
|
3471
3466
|
* - `sort`: if multiple docs are found by the filter, sets the sort order to choose which doc to update
|
|
3472
|
-
* - `maxTimeMS`: puts a time limit on the query
|
|
3467
|
+
* - `maxTimeMS`: puts a time limit on the query
|
|
3473
3468
|
* - `runValidators`: if true, runs [update validators](https://mongoosejs.com/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema.
|
|
3474
3469
|
* - `setDefaultsOnInsert`: `true` by default. If `setDefaultsOnInsert` and `upsert` are true, mongoose will apply the [defaults](https://mongoosejs.com/docs/defaults.html) specified in the model's schema if a new document is created.
|
|
3475
3470
|
* - `requireFilter`: bool - if true, throws an error if the filter is empty (`{}`). Defaults to false.
|
|
@@ -3685,7 +3680,7 @@ Query.prototype._findOneAndUpdate = async function _findOneAndUpdate() {
|
|
|
3685
3680
|
* #### Available options
|
|
3686
3681
|
*
|
|
3687
3682
|
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
3688
|
-
* - `maxTimeMS`: puts a time limit on the query
|
|
3683
|
+
* - `maxTimeMS`: puts a time limit on the query
|
|
3689
3684
|
* - `requireFilter`: bool - if true, throws an error if the filter is empty (`{}`). Defaults to false.
|
|
3690
3685
|
*
|
|
3691
3686
|
* #### Example:
|
|
@@ -3778,7 +3773,7 @@ Query.prototype._findOneAndDelete = async function _findOneAndDelete() {
|
|
|
3778
3773
|
* #### Available options
|
|
3779
3774
|
*
|
|
3780
3775
|
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
3781
|
-
* - `maxTimeMS`: puts a time limit on the query
|
|
3776
|
+
* - `maxTimeMS`: puts a time limit on the query
|
|
3782
3777
|
* - `includeResultMetadata`: if true, returns the full [ModifyResult from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/7.0/interfaces/ModifyResult.html) rather than just the document
|
|
3783
3778
|
* - `requireFilter`: bool - if true, throws an error if the filter is empty (`{}`). Defaults to false.
|
|
3784
3779
|
*
|
|
@@ -5542,7 +5537,6 @@ Query.prototype.tailable = function(val, opts) {
|
|
|
5542
5537
|
* @param {object} object Must contain a `type` property which is a String and a `coordinates` property which is an Array. See the examples.
|
|
5543
5538
|
* @return {Query} this
|
|
5544
5539
|
* @see $geometry https://www.mongodb.com/docs/manual/reference/operator/geometry/
|
|
5545
|
-
* @see Geospatial Support Enhancements https://www.mongodb.com/docs/manual/release-notes/2.4/#geospatial-support-enhancements
|
|
5546
5540
|
* @see MongoDB Geospatial Indexing https://www.mongodb.com/docs/manual/core/geospatial-indexes/
|
|
5547
5541
|
* @api public
|
|
5548
5542
|
*/
|
package/lib/schema/date.js
CHANGED
|
@@ -143,7 +143,7 @@ SchemaDate._defaultCaster = v => {
|
|
|
143
143
|
/**
|
|
144
144
|
* Declares a TTL index (rounded to the nearest second) for _Date_ types only.
|
|
145
145
|
*
|
|
146
|
-
* This sets the `expireAfterSeconds` index option
|
|
146
|
+
* This sets the `expireAfterSeconds` index option.
|
|
147
147
|
* This index type is only compatible with Date types.
|
|
148
148
|
*
|
|
149
149
|
* #### Example:
|