mongoose 9.12.0 → 9.12.1
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/cursor/aggregationCursor.js +9 -5
- package/lib/cursor/queryCursor.js +8 -5
- package/lib/helpers/cursor/waitForInitialization.js +17 -0
- package/lib/helpers/pathTrie.js +2 -2
- package/lib/helpers/populate/setInheritedReadOptions.js +21 -0
- package/lib/helpers/query/cloneMongooseOptions.js +44 -0
- package/lib/helpers/schema/cleanPositionalOperators.js +5 -3
- package/lib/helpers/update/applyTimestampsToChildren.js +1 -1
- package/lib/helpers/updateValidators.js +24 -16
- package/lib/model.js +13 -2
- package/lib/query.js +4 -26
- package/lib/queryHelpers.js +2 -0
- package/lib/types/array/methods/index.js +18 -3
- package/lib/types/map.js +1 -1
- package/package.json +2 -1
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
const MongooseError = require('../error/mongooseError');
|
|
8
8
|
const Readable = require('stream').Readable;
|
|
9
9
|
const eachAsync = require('../helpers/cursor/eachAsync');
|
|
10
|
+
const waitForInitialization = require('../helpers/cursor/waitForInitialization');
|
|
10
11
|
const immediate = require('../helpers/immediate');
|
|
11
12
|
const kareem = require('kareem');
|
|
12
13
|
const util = require('util');
|
|
@@ -42,6 +43,7 @@ function AggregationCursor(agg) {
|
|
|
42
43
|
Readable.call(this, { autoDestroy: true, objectMode: true });
|
|
43
44
|
|
|
44
45
|
this.cursor = null;
|
|
46
|
+
this._initializationComplete = false;
|
|
45
47
|
this.agg = agg;
|
|
46
48
|
this._transforms = [];
|
|
47
49
|
const connection = agg._connection;
|
|
@@ -58,6 +60,7 @@ function AggregationCursor(agg) {
|
|
|
58
60
|
options = driverOptions;
|
|
59
61
|
}
|
|
60
62
|
this.cursor = connection.db.aggregate(agg._pipeline, options);
|
|
63
|
+
this._initializationComplete = true;
|
|
61
64
|
setImmediate(() => this.emit('cursor', this.cursor));
|
|
62
65
|
} else {
|
|
63
66
|
_init(model, this, agg);
|
|
@@ -108,8 +111,10 @@ function _getRawCursor(model, aggregationCursor, agg) {
|
|
|
108
111
|
}
|
|
109
112
|
const cursor = model.collection.aggregate(agg._pipeline, options);
|
|
110
113
|
aggregationCursor.cursor = cursor;
|
|
114
|
+
aggregationCursor._initializationComplete = true;
|
|
111
115
|
aggregationCursor.emit('cursor', cursor);
|
|
112
116
|
} catch (err) {
|
|
117
|
+
aggregationCursor._initializationComplete = true;
|
|
113
118
|
aggregationCursor._markError(err);
|
|
114
119
|
aggregationCursor.listeners('error').length > 0 && aggregationCursor.emit('error', aggregationCursor._error);
|
|
115
120
|
}
|
|
@@ -126,6 +131,7 @@ function _getRawCursor(model, aggregationCursor, agg) {
|
|
|
126
131
|
*/
|
|
127
132
|
|
|
128
133
|
function _handlePreHookError(queryCursor, err) {
|
|
134
|
+
queryCursor._initializationComplete = true;
|
|
129
135
|
if (err instanceof kareem.skipWrappedFunction) {
|
|
130
136
|
const resultValue = err.args[0];
|
|
131
137
|
if (resultValue != null && (!Array.isArray(resultValue) || resultValue.length)) {
|
|
@@ -275,16 +281,14 @@ AggregationCursor.prototype.close = async function close() {
|
|
|
275
281
|
|
|
276
282
|
AggregationCursor.prototype._destroy = function _destroy(_err, callback) {
|
|
277
283
|
let waitForCursor = null;
|
|
278
|
-
if (!this.
|
|
279
|
-
waitForCursor =
|
|
280
|
-
this.once('cursor', resolve);
|
|
281
|
-
});
|
|
284
|
+
if (!this._initializationComplete) {
|
|
285
|
+
waitForCursor = waitForInitialization(this);
|
|
282
286
|
} else {
|
|
283
287
|
waitForCursor = Promise.resolve();
|
|
284
288
|
}
|
|
285
289
|
|
|
286
290
|
waitForCursor
|
|
287
|
-
.then(() => this.cursor
|
|
291
|
+
.then(() => this.cursor?.close())
|
|
288
292
|
.then(() => {
|
|
289
293
|
this._closed = true;
|
|
290
294
|
callback();
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
const MongooseError = require('../error/mongooseError');
|
|
8
8
|
const Readable = require('stream').Readable;
|
|
9
9
|
const eachAsync = require('../helpers/cursor/eachAsync');
|
|
10
|
+
const waitForInitialization = require('../helpers/cursor/waitForInitialization');
|
|
10
11
|
const helpers = require('../queryHelpers');
|
|
11
12
|
const kareem = require('kareem');
|
|
12
13
|
const immediate = require('../helpers/immediate');
|
|
@@ -44,6 +45,7 @@ function QueryCursor(query) {
|
|
|
44
45
|
Readable.call(this, { autoDestroy: true, objectMode: true });
|
|
45
46
|
|
|
46
47
|
this.cursor = null;
|
|
48
|
+
this._initializationComplete = false;
|
|
47
49
|
this.skipped = false;
|
|
48
50
|
this.query = query;
|
|
49
51
|
this._closed = false;
|
|
@@ -56,6 +58,7 @@ function QueryCursor(query) {
|
|
|
56
58
|
|
|
57
59
|
const onPreComplete = (err) => {
|
|
58
60
|
if (err != null) {
|
|
61
|
+
this._initializationComplete = true;
|
|
59
62
|
if (err instanceof kareem.skipWrappedFunction) {
|
|
60
63
|
const resultValue = err.args[0];
|
|
61
64
|
if (resultValue != null && (!Array.isArray(resultValue) || resultValue.length)) {
|
|
@@ -123,8 +126,10 @@ function _getRawCursor(query, queryCursor) {
|
|
|
123
126
|
try {
|
|
124
127
|
const cursor = query.model.collection.find(query._conditions, queryCursor.options);
|
|
125
128
|
queryCursor.cursor = cursor;
|
|
129
|
+
queryCursor._initializationComplete = true;
|
|
126
130
|
queryCursor.emit('cursor', cursor);
|
|
127
131
|
} catch (err) {
|
|
132
|
+
queryCursor._initializationComplete = true;
|
|
128
133
|
queryCursor._markError(err);
|
|
129
134
|
queryCursor.listeners('error').length > 0 && queryCursor.emit('error', queryCursor._error);
|
|
130
135
|
}
|
|
@@ -271,17 +276,15 @@ QueryCursor.prototype.close = async function close() {
|
|
|
271
276
|
|
|
272
277
|
QueryCursor.prototype._destroy = function _destroy(_err, callback) {
|
|
273
278
|
let waitForCursor = null;
|
|
274
|
-
if (!this.
|
|
275
|
-
waitForCursor =
|
|
276
|
-
this.once('cursor', resolve);
|
|
277
|
-
});
|
|
279
|
+
if (!this._initializationComplete) {
|
|
280
|
+
waitForCursor = waitForInitialization(this);
|
|
278
281
|
} else {
|
|
279
282
|
waitForCursor = Promise.resolve();
|
|
280
283
|
}
|
|
281
284
|
|
|
282
285
|
waitForCursor
|
|
283
286
|
.then(() => {
|
|
284
|
-
return this.cursor
|
|
287
|
+
return this.cursor?.close();
|
|
285
288
|
})
|
|
286
289
|
.then(() => {
|
|
287
290
|
this._closed = true;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = function waitForInitialization(cursor) {
|
|
4
|
+
return new Promise(resolve => {
|
|
5
|
+
const onInitialized = () => {
|
|
6
|
+
// A casting error can be emitted while cursor creation is still pending.
|
|
7
|
+
if (!cursor._initializationComplete) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
cursor.removeListener('cursor', onInitialized);
|
|
11
|
+
cursor.removeListener('error', onInitialized);
|
|
12
|
+
resolve();
|
|
13
|
+
};
|
|
14
|
+
cursor.on('cursor', onInitialized);
|
|
15
|
+
cursor.on('error', onInitialized);
|
|
16
|
+
});
|
|
17
|
+
};
|
package/lib/helpers/pathTrie.js
CHANGED
|
@@ -71,7 +71,7 @@ class PathTrie {
|
|
|
71
71
|
|
|
72
72
|
/**
|
|
73
73
|
* Check if `path` or any of its ancestor paths exist in the trie. `path` is
|
|
74
|
-
* either a single segment string or a pre-split array of segments
|
|
74
|
+
* either a single segment string or a pre-split array of segments - lookups
|
|
75
75
|
* never split, callers are responsible for splitting dotted paths.
|
|
76
76
|
* For example, with a trie containing 'profile':
|
|
77
77
|
* matchesPathOrAncestor(['profile', 'firstName']) === true
|
|
@@ -89,7 +89,7 @@ class PathTrie {
|
|
|
89
89
|
/**
|
|
90
90
|
* Check if `path`, any of its ancestor paths, or any of its descendant paths
|
|
91
91
|
* exist in the trie. `path` is either a single segment string or a pre-split
|
|
92
|
-
* array of segments
|
|
92
|
+
* array of segments - lookups never split, callers are responsible for
|
|
93
93
|
* splitting dotted paths. For example:
|
|
94
94
|
* new PathTrie(['profile']).overlapsPath(['profile', 'firstName']) === true
|
|
95
95
|
* new PathTrie(['profile.firstName']).overlapsPath('profile') === true
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*!
|
|
4
|
+
* Store the read preference and read concern that the populate options `pops` inherit from
|
|
5
|
+
* `query`'s explicit settings. Schema-level `read` and `readConcern` defaults are not inherited:
|
|
6
|
+
* each populate query applies its own model's schema defaults. `_execPopulateQuery()` applies
|
|
7
|
+
* these unless the populate options set their own.
|
|
8
|
+
*
|
|
9
|
+
* Store them separately from `pop.options` because a query's clones share its populate options:
|
|
10
|
+
* inherited values written to `pop.options` would look like explicit settings to later executions.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
module.exports = function setInheritedReadOptions(query, pops) {
|
|
14
|
+
const options = query.options;
|
|
15
|
+
const readPreference = 'readPreference' in options ? options.readPreference : undefined;
|
|
16
|
+
|
|
17
|
+
for (const pop of pops) {
|
|
18
|
+
pop._inheritedReadPreference = readPreference;
|
|
19
|
+
pop._inheritedReadConcern = options.readConcern;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const PopulateOptions = require('../../options/populateOptions');
|
|
4
|
+
const clone = require('../clone');
|
|
5
|
+
const specialProperties = require('../specialProperties');
|
|
6
|
+
|
|
7
|
+
/*!
|
|
8
|
+
* Deep clone a query's `_mongooseOptions` so a cloned query doesn't share state with the
|
|
9
|
+
* original. `clone()` returns class instances as-is, so explicitly copy `PopulateOptions`,
|
|
10
|
+
* including nested populate: populate options are mutable, and executing a query writes
|
|
11
|
+
* per-execution state like `session` and `lean` into them.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
module.exports = function cloneMongooseOptions(mongooseOptions) {
|
|
15
|
+
const { populate, ...rest } = mongooseOptions;
|
|
16
|
+
const ret = clone(rest);
|
|
17
|
+
if (populate != null) {
|
|
18
|
+
ret.populate = {};
|
|
19
|
+
for (const path of Object.keys(populate)) {
|
|
20
|
+
if (specialProperties.has(path)) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
ret.populate[path] = clonePopulate(populate[path]);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return ret;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function clonePopulate(pop) {
|
|
30
|
+
if (Array.isArray(pop)) {
|
|
31
|
+
return pop.map(clonePopulate);
|
|
32
|
+
}
|
|
33
|
+
if (pop == null || typeof pop !== 'object') {
|
|
34
|
+
return pop;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Skip `_docs` and `_childDocs`: they hold results of previous executions, see gh-11641
|
|
38
|
+
const { populate, _docs, _childDocs, ...rest } = pop;
|
|
39
|
+
const ret = pop instanceof PopulateOptions ? new PopulateOptions(rest) : clone(rest);
|
|
40
|
+
if (populate !== undefined) {
|
|
41
|
+
ret.populate = clonePopulate(populate);
|
|
42
|
+
}
|
|
43
|
+
return ret;
|
|
44
|
+
}
|
|
@@ -6,7 +6,9 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
module.exports = function cleanPositionalOperators(path) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
// Most paths have no positional operators, and a single-char search is much cheaper than running the regexp
|
|
10
|
+
if (path.indexOf('$') === -1) {
|
|
11
|
+
return path;
|
|
12
|
+
}
|
|
13
|
+
return path.replace(/\.\$(\[[^\]]*\])?(?=\.|$)/g, '.0');
|
|
12
14
|
};
|
|
@@ -45,7 +45,7 @@ function applyTimestampsToChildren(now, update, schema) {
|
|
|
45
45
|
|
|
46
46
|
function _applyTimestampToUpdateOperator(op) {
|
|
47
47
|
for (const key of Object.keys(op)) {
|
|
48
|
-
const $path = schema.path(key
|
|
48
|
+
const $path = schema.path(cleanPositionalOperators(key));
|
|
49
49
|
if (op[key] &&
|
|
50
50
|
$path &&
|
|
51
51
|
$path.$isMongooseDocumentArray &&
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
* Module dependencies.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const ValidationError = require('../error/validation');
|
|
8
7
|
const cleanPositionalOperators = require('./schema/cleanPositionalOperators');
|
|
9
8
|
const flatten = require('./common').flatten;
|
|
10
9
|
|
|
@@ -81,7 +80,7 @@ module.exports = async function updateValidators(query, schema, castedDoc, optio
|
|
|
81
80
|
const validatorsToExecute = [];
|
|
82
81
|
const validationErrors = [];
|
|
83
82
|
|
|
84
|
-
|
|
83
|
+
let alreadyValidated = null;
|
|
85
84
|
|
|
86
85
|
const context = query;
|
|
87
86
|
for (let i = 0; i < numUpdates; ++i) {
|
|
@@ -93,6 +92,9 @@ module.exports = async function updateValidators(query, schema, castedDoc, optio
|
|
|
93
92
|
if (schemaPath.instance === 'Mixed' && schemaPath.path !== updates[i]) {
|
|
94
93
|
continue;
|
|
95
94
|
}
|
|
95
|
+
if (isPull[updates[i]] && schemaPath.$isMongooseArray) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
96
98
|
|
|
97
99
|
if (v && Array.isArray(v.$in)) {
|
|
98
100
|
v.$in.forEach((v, i) => {
|
|
@@ -104,13 +106,13 @@ module.exports = async function updateValidators(query, schema, castedDoc, optio
|
|
|
104
106
|
);
|
|
105
107
|
});
|
|
106
108
|
} else {
|
|
107
|
-
if (
|
|
108
|
-
schemaPath.$isMongooseArray) {
|
|
109
|
+
if (alreadyValidated != null && hasValidatedAncestor(alreadyValidated, updates[i])) {
|
|
109
110
|
continue;
|
|
110
111
|
}
|
|
111
112
|
|
|
112
113
|
if (schemaPath.$isMongooseDocumentArrayElement && v?.$__ != null) {
|
|
113
|
-
alreadyValidated
|
|
114
|
+
alreadyValidated ??= new Set();
|
|
115
|
+
alreadyValidated.add(updates[i]);
|
|
114
116
|
validatorsToExecute.push(
|
|
115
117
|
schemaPath.doValidate(v, context, { updateValidator: true }).catch(err => {
|
|
116
118
|
if (err.errors) {
|
|
@@ -126,21 +128,12 @@ module.exports = async function updateValidators(query, schema, castedDoc, optio
|
|
|
126
128
|
})
|
|
127
129
|
);
|
|
128
130
|
} else {
|
|
129
|
-
const isAlreadyValidated = alreadyValidated.find(path => updates[i].startsWith(path + '.'));
|
|
130
|
-
if (isAlreadyValidated) {
|
|
131
|
-
continue;
|
|
132
|
-
}
|
|
133
131
|
if (schemaPath.$isSingleNested) {
|
|
134
|
-
alreadyValidated
|
|
132
|
+
alreadyValidated ??= new Set();
|
|
133
|
+
alreadyValidated.add(updates[i]);
|
|
135
134
|
}
|
|
136
135
|
validatorsToExecute.push(
|
|
137
136
|
schemaPath.doValidate(v, context, { updateValidator: true }).catch(err => {
|
|
138
|
-
if (schemaPath.schema != null &&
|
|
139
|
-
schemaPath.schema.options.storeSubdocValidationError === false &&
|
|
140
|
-
err instanceof ValidationError) {
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
137
|
if (err) {
|
|
145
138
|
err.path = updates[i];
|
|
146
139
|
validationErrors.push(err);
|
|
@@ -184,3 +177,18 @@ module.exports = async function updateValidators(query, schema, castedDoc, optio
|
|
|
184
177
|
await Promise.all(validatorsToExecute);
|
|
185
178
|
return validationErrors;
|
|
186
179
|
};
|
|
180
|
+
|
|
181
|
+
/*!
|
|
182
|
+
* Returns true if `path` is underneath a path in `alreadyValidated`. Checks each
|
|
183
|
+
* ancestor of `path` with a Set lookup, so the cost depends on the depth of `path`
|
|
184
|
+
* rather than on the number of subdocuments validated so far.
|
|
185
|
+
*/
|
|
186
|
+
|
|
187
|
+
function hasValidatedAncestor(alreadyValidated, path) {
|
|
188
|
+
for (let i = path.indexOf('.'); i !== -1; i = path.indexOf('.', i + 1)) {
|
|
189
|
+
if (alreadyValidated.has(path.slice(0, i))) {
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return false;
|
|
194
|
+
}
|
package/lib/model.js
CHANGED
|
@@ -72,6 +72,7 @@ const prepareDiscriminatorPipeline = require('./helpers/aggregate/prepareDiscrim
|
|
|
72
72
|
const pushNestedArrayPaths = require('./helpers/model/pushNestedArrayPaths');
|
|
73
73
|
const removeDeselectedForeignField = require('./helpers/populate/removeDeselectedForeignField');
|
|
74
74
|
const setDottedPath = require('./helpers/path/setDottedPath');
|
|
75
|
+
const setInheritedReadOptions = require('./helpers/populate/setInheritedReadOptions');
|
|
75
76
|
const splitPopulateQuery = require('./helpers/populate/splitPopulateQuery');
|
|
76
77
|
const { buildMiddlewareFilter } = require('./helpers/buildMiddlewareFilter');
|
|
77
78
|
const util = require('util');
|
|
@@ -1928,7 +1929,7 @@ Model.discriminators;
|
|
|
1928
1929
|
* #### Example:
|
|
1929
1930
|
*
|
|
1930
1931
|
* await Character.find(Character.translateAliases({
|
|
1931
|
-
*
|
|
1932
|
+
* nombre: 'Eddard Stark' // Alias for 'name'
|
|
1932
1933
|
* });
|
|
1933
1934
|
*
|
|
1934
1935
|
* By default, `translateAliases()` overwrites raw fields with aliased fields.
|
|
@@ -4395,7 +4396,7 @@ Model.validate = async function validate(obj, pathsOrOptions, context) {
|
|
|
4395
4396
|
const pieces = p.split('.');
|
|
4396
4397
|
let cur = pieces[0];
|
|
4397
4398
|
|
|
4398
|
-
for (const piece of pieces) {
|
|
4399
|
+
for (const piece of pieces.slice(1)) {
|
|
4399
4400
|
if (_pathsToValidate.has(cur)) {
|
|
4400
4401
|
return true;
|
|
4401
4402
|
}
|
|
@@ -4824,6 +4825,13 @@ function _execPopulateQuery(mod, match, select) {
|
|
|
4824
4825
|
queryOptions.perDocumentLimit = mod.options.perDocumentLimit;
|
|
4825
4826
|
}
|
|
4826
4827
|
Object.assign(queryOptions, mod.options.options);
|
|
4828
|
+
// Inherit the parent query's read settings unless populate options set them, see `setInheritedReadOptions()`
|
|
4829
|
+
if (queryOptions.readPreference == null && mod.options._inheritedReadPreference != null) {
|
|
4830
|
+
queryOptions.readPreference = mod.options._inheritedReadPreference;
|
|
4831
|
+
}
|
|
4832
|
+
if (queryOptions.readConcern == null && mod.options._inheritedReadConcern != null) {
|
|
4833
|
+
queryOptions.readConcern = mod.options._inheritedReadConcern;
|
|
4834
|
+
}
|
|
4827
4835
|
|
|
4828
4836
|
if (mod.count) {
|
|
4829
4837
|
delete queryOptions.skip;
|
|
@@ -4918,6 +4926,9 @@ function _execPopulateQuery(mod, match, select) {
|
|
|
4918
4926
|
delete query._deferredPopulate;
|
|
4919
4927
|
}
|
|
4920
4928
|
if (shouldDeferSubPopulate) {
|
|
4929
|
+
// Deferred sub-populate skips `preparePopulationOptionsMQ()`, so inherit read settings here
|
|
4930
|
+
subPopulate = utils.populate(subPopulate);
|
|
4931
|
+
setInheritedReadOptions(query, subPopulate);
|
|
4921
4932
|
deferredPopulates.push(subPopulate);
|
|
4922
4933
|
}
|
|
4923
4934
|
return { docs, deferredPopulates };
|
package/lib/query.js
CHANGED
|
@@ -20,6 +20,7 @@ const castArrayFilters = require('./helpers/update/castArrayFilters');
|
|
|
20
20
|
const castNumber = require('./cast/number');
|
|
21
21
|
const castUpdate = require('./helpers/query/castUpdate');
|
|
22
22
|
const clone = require('./helpers/clone');
|
|
23
|
+
const cloneMongooseOptions = require('./helpers/query/cloneMongooseOptions');
|
|
23
24
|
const decorateUpdateWithVersionKey = require('./helpers/update/decorateUpdateWithVersionKey');
|
|
24
25
|
const getDiscriminatorByValue = require('./helpers/discriminator/getDiscriminatorByValue');
|
|
25
26
|
const helpers = require('./queryHelpers');
|
|
@@ -274,7 +275,7 @@ Query.prototype.toConstructor = function toConstructor() {
|
|
|
274
275
|
if (!(this instanceof CustomQuery)) {
|
|
275
276
|
return new CustomQuery(criteria, options);
|
|
276
277
|
}
|
|
277
|
-
this._mongooseOptions =
|
|
278
|
+
this._mongooseOptions = cloneMongooseOptions(p._mongooseOptions);
|
|
278
279
|
Query.call(this, criteria, options || null, model, coll);
|
|
279
280
|
};
|
|
280
281
|
|
|
@@ -304,7 +305,7 @@ Query.prototype.toConstructor = function toConstructor() {
|
|
|
304
305
|
p._path = this._path;
|
|
305
306
|
p._distinct = this._distinct;
|
|
306
307
|
p._collection = this._collection;
|
|
307
|
-
p._mongooseOptions = this._mongooseOptions;
|
|
308
|
+
p._mongooseOptions = cloneMongooseOptions(this._mongooseOptions);
|
|
308
309
|
|
|
309
310
|
return CustomQuery;
|
|
310
311
|
};
|
|
@@ -352,7 +353,7 @@ Query.prototype.clone = function() {
|
|
|
352
353
|
q._path = this._path;
|
|
353
354
|
q._distinct = this._distinct;
|
|
354
355
|
q._collection = this._collection;
|
|
355
|
-
q._mongooseOptions = this._mongooseOptions;
|
|
356
|
+
q._mongooseOptions = cloneMongooseOptions(this._mongooseOptions);
|
|
356
357
|
|
|
357
358
|
return q;
|
|
358
359
|
};
|
|
@@ -2154,29 +2155,6 @@ Query.prototype._optionsForExec = function(model) {
|
|
|
2154
2155
|
}
|
|
2155
2156
|
}
|
|
2156
2157
|
|
|
2157
|
-
if (this._mongooseOptions.populate) {
|
|
2158
|
-
if (options.readPreference) {
|
|
2159
|
-
for (const pop of Object.values(this._mongooseOptions.populate)) {
|
|
2160
|
-
if (pop.options?.readPreference === undefined) {
|
|
2161
|
-
if (!pop.options) {
|
|
2162
|
-
pop.options = {};
|
|
2163
|
-
}
|
|
2164
|
-
pop.options.readPreference = options.readPreference;
|
|
2165
|
-
}
|
|
2166
|
-
}
|
|
2167
|
-
}
|
|
2168
|
-
if (options.readConcern) {
|
|
2169
|
-
for (const pop of Object.values(this._mongooseOptions.populate)) {
|
|
2170
|
-
if (pop.options?.readConcern === undefined) {
|
|
2171
|
-
if (!pop.options) {
|
|
2172
|
-
pop.options = {};
|
|
2173
|
-
}
|
|
2174
|
-
pop.options.readConcern = options.readConcern;
|
|
2175
|
-
}
|
|
2176
|
-
}
|
|
2177
|
-
}
|
|
2178
|
-
}
|
|
2179
|
-
|
|
2180
2158
|
return options;
|
|
2181
2159
|
};
|
|
2182
2160
|
|
package/lib/queryHelpers.js
CHANGED
|
@@ -14,6 +14,7 @@ const getDiscriminatorByValue =
|
|
|
14
14
|
const isDefiningProjection = require('./helpers/projection/isDefiningProjection');
|
|
15
15
|
const clone = require('./helpers/clone');
|
|
16
16
|
const isPathSelectedInclusive = require('./helpers/projection/isPathSelectedInclusive');
|
|
17
|
+
const setInheritedReadOptions = require('./helpers/populate/setInheritedReadOptions');
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
20
|
* Prepare a set of path options for query population. This is the MongooseQuery
|
|
@@ -61,6 +62,7 @@ exports.preparePopulationOptionsMQ = function preparePopulationOptionsMQ(query,
|
|
|
61
62
|
pop[i]._localModel = query.model;
|
|
62
63
|
}
|
|
63
64
|
}
|
|
65
|
+
setInheritedReadOptions(query, pop);
|
|
64
66
|
|
|
65
67
|
return pop;
|
|
66
68
|
};
|
|
@@ -743,8 +743,7 @@ const methods = {
|
|
|
743
743
|
if (isOverwrite) {
|
|
744
744
|
atomic.$each = values;
|
|
745
745
|
|
|
746
|
-
if ((atomics.$push
|
|
747
|
-
atomics.$push.$position != atomic.$position) {
|
|
746
|
+
if (_cannotMergePush(atomics.$push, atomic)) {
|
|
748
747
|
if (atomic.$position != null) {
|
|
749
748
|
[].splice.apply(arr, [atomic.$position, 0].concat(values));
|
|
750
749
|
ret = arr.length;
|
|
@@ -762,9 +761,13 @@ const methods = {
|
|
|
762
761
|
} else {
|
|
763
762
|
atomic = values;
|
|
764
763
|
ret = _basePush.apply(arr, values);
|
|
764
|
+
if (atomics.$push?.$position != null) {
|
|
765
|
+
this._registerAtomic('$set', this);
|
|
766
|
+
}
|
|
765
767
|
}
|
|
766
768
|
|
|
767
|
-
|
|
769
|
+
// add to a pending `$each` instead of replacing it (gh-16618)
|
|
770
|
+
this._registerAtomic('$push', atomics.$push?.$each?.length ? values : atomic);
|
|
768
771
|
|
|
769
772
|
return ret;
|
|
770
773
|
},
|
|
@@ -1077,6 +1080,18 @@ function _depopulateIfNecessary(arr, docs) {
|
|
|
1077
1080
|
}
|
|
1078
1081
|
}
|
|
1079
1082
|
|
|
1083
|
+
/*!
|
|
1084
|
+
* A `{ $each }` push can only join a pending `$push` when neither has
|
|
1085
|
+
* `$position`, `$slice` or `$sort` (gh-14244, gh-16618)
|
|
1086
|
+
*/
|
|
1087
|
+
|
|
1088
|
+
function _cannotMergePush(pending, atomic) {
|
|
1089
|
+
if (!pending?.$each?.length) {
|
|
1090
|
+
return false;
|
|
1091
|
+
}
|
|
1092
|
+
return [pending, atomic].some(push => push.$position != null || push.$slice != null || push.$sort != null);
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1080
1095
|
const returnVanillaArrayMethods = [
|
|
1081
1096
|
'filter',
|
|
1082
1097
|
'flat',
|
package/lib/types/map.js
CHANGED
|
@@ -21,7 +21,7 @@ const populateModelSymbol = require('../helpers/symbols').populateModelSymbol;
|
|
|
21
21
|
class MongooseMap extends Map {
|
|
22
22
|
constructor(v, path, doc, schemaType) {
|
|
23
23
|
if (getConstructorName(v) === 'Object') {
|
|
24
|
-
v = Object.keys(v).
|
|
24
|
+
v = Object.keys(v).map(key => [key, v[key]]);
|
|
25
25
|
}
|
|
26
26
|
super(v);
|
|
27
27
|
this.$__parent = doc?.$__ != null ? doc : null;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mongoose",
|
|
3
3
|
"description": "Mongoose MongoDB ODM",
|
|
4
|
-
"version": "9.12.
|
|
4
|
+
"version": "9.12.1",
|
|
5
5
|
"author": "Guillermo Rauch <guillermo@learnboost.com>",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"mongodb",
|
|
@@ -88,6 +88,7 @@
|
|
|
88
88
|
"docs:prepare:publish:8x": "env DOCS_DEPLOY=true npm run docs:generate && git checkout gh-pages && rm -rf ./docs/8.x && mv ./tmp ./docs/8.x",
|
|
89
89
|
"docs:check-links": "linkinator http://127.0.0.1:8089 --silent --recurse --skip cdn.carbonads.com --skip m.servedby-buysellads.com --skip \"127\\.0\\.0\\.1:8089/docs/\\d+.x\"",
|
|
90
90
|
"docs:update-mongodb-links": "node ./scripts/update-mongodb-links.js",
|
|
91
|
+
"checkLibCharset": "node ./scripts/checkLibCharset.js",
|
|
91
92
|
"lint": "eslint .",
|
|
92
93
|
"lint-js": "eslint . --ext .js --ext .cjs",
|
|
93
94
|
"lint-ts": "eslint . --ext .ts",
|