mongoose 9.11.4 → 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/document.js +186 -287
- package/lib/helpers/cursor/waitForInitialization.js +17 -0
- package/lib/helpers/document/applyDefaults.js +6 -11
- package/lib/helpers/model/discriminator.js +3 -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/update/applyTimestampsToUpdate.js +1 -1
- package/lib/helpers/updateValidators.js +24 -16
- package/lib/model.js +24 -6
- package/lib/query.js +5 -52
- package/lib/queryHelpers.js +8 -1
- package/lib/schema/documentArray.js +14 -3
- package/lib/schema/number.js +3 -1
- package/lib/schema/string.js +3 -1
- package/lib/schema/subdocument.js +9 -1
- package/lib/schema.js +114 -4
- package/lib/stateMachine.js +5 -2
- package/lib/types/array/methods/index.js +22 -4
- package/lib/types/arraySubdocument.js +11 -9
- package/lib/types/map.js +17 -1
- package/lib/types/subdocument.js +5 -1
- package/package.json +15 -15
- package/types/types.d.ts +12 -0
|
@@ -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
|
+
};
|
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
const isNestedProjection = require('../projection/isNestedProjection');
|
|
4
4
|
|
|
5
5
|
module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildren, isBeforeSetters, pathsToSkip, options) {
|
|
6
|
-
|
|
6
|
+
// Only paths with a default can modify the doc, so skip iterating over every path in the schema.
|
|
7
|
+
const { paths: pathsWithDefaults, schemaTypes } = doc.$__schema._getPathsWithDefaults();
|
|
8
|
+
const numPaths = pathsWithDefaults.length;
|
|
7
9
|
const skipParentChangeTracking = options?.skipParentChangeTracking;
|
|
8
10
|
const skipId = doc.$__.skipId;
|
|
9
11
|
// Whether any path has a function default that must run **after** setters,
|
|
@@ -11,7 +13,8 @@ module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildre
|
|
|
11
13
|
// entirely when this comes back `false`.
|
|
12
14
|
let hasPostSetterDefaults = false;
|
|
13
15
|
|
|
14
|
-
for (
|
|
16
|
+
for (let i = 0; i < numPaths; ++i) {
|
|
17
|
+
const p = pathsWithDefaults[i];
|
|
15
18
|
let def;
|
|
16
19
|
let curPath = '';
|
|
17
20
|
|
|
@@ -19,20 +22,12 @@ module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildre
|
|
|
19
22
|
continue;
|
|
20
23
|
}
|
|
21
24
|
|
|
22
|
-
const type =
|
|
23
|
-
// `getDefault()` returns undefined when `defaultValue` is undefined, in which
|
|
24
|
-
// case this loop never modifies the doc, so skip traversing the path entirely.
|
|
25
|
-
if (type.defaultValue === undefined) {
|
|
26
|
-
continue;
|
|
27
|
-
}
|
|
25
|
+
const type = schemaTypes[i];
|
|
28
26
|
if (typeof type.defaultValue === 'function' && !type.defaultValue.$runBeforeSetters) {
|
|
29
27
|
hasPostSetterDefaults = true;
|
|
30
28
|
}
|
|
31
29
|
const path = type.splitPath();
|
|
32
30
|
const len = path.length;
|
|
33
|
-
if (path[len - 1] === '$*') {
|
|
34
|
-
continue;
|
|
35
|
-
}
|
|
36
31
|
|
|
37
32
|
let included = false;
|
|
38
33
|
let doc_ = doc._doc;
|
|
@@ -173,6 +173,9 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
|
|
|
173
173
|
for (const conflictingPath of conflictingPaths) {
|
|
174
174
|
delete schema.paths[conflictingPath];
|
|
175
175
|
}
|
|
176
|
+
schema._pathsWithDefaults = null;
|
|
177
|
+
schema._pathsWithTransforms = undefined;
|
|
178
|
+
schema._hasSelectedPaths = undefined;
|
|
176
179
|
|
|
177
180
|
// Rebuild schema models because schemas may have been merged re: #7884
|
|
178
181
|
schema.childSchemas.forEach(obj => {
|
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 &&
|
|
@@ -44,7 +44,7 @@ function applyTimestampsToUpdate(now, createdAt, updatedAt, currentUpdate, optio
|
|
|
44
44
|
|
|
45
45
|
if (Array.isArray(updates)) {
|
|
46
46
|
// Update with aggregation pipeline
|
|
47
|
-
if (updatedAt == null) {
|
|
47
|
+
if (updatedAt == null || skipUpdatedAt) {
|
|
48
48
|
return updates;
|
|
49
49
|
}
|
|
50
50
|
updates.push({ $set: { [updatedAt]: now } });
|
|
@@ -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');
|
|
@@ -1793,7 +1794,7 @@ function _ensureIndexes(model, options, callback) {
|
|
|
1793
1794
|
applyWriteConcern(model.schema, indexOptions);
|
|
1794
1795
|
applySchemaCollation(indexFields, indexOptions, model.schema.options);
|
|
1795
1796
|
|
|
1796
|
-
indexSingleStart(indexFields,
|
|
1797
|
+
indexSingleStart(indexFields, indexOptions);
|
|
1797
1798
|
|
|
1798
1799
|
// Just in case `createIndex()` throws a sync error
|
|
1799
1800
|
let promise = null;
|
|
@@ -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.
|
|
@@ -3228,8 +3229,12 @@ async function _insertMany(arr, options) {
|
|
|
3228
3229
|
|
|
3229
3230
|
if (error.writeErrors != null) {
|
|
3230
3231
|
for (let i = 0; i < error.writeErrors.length; ++i) {
|
|
3231
|
-
const
|
|
3232
|
-
|
|
3232
|
+
const writeError = error.writeErrors[i];
|
|
3233
|
+
const originalIndex = validDocIndexToOriginalIndex[writeError.index];
|
|
3234
|
+
// `index` is a getter-only property on the driver's `WriteError` that reads from `err`
|
|
3235
|
+
if (writeError.err != null) {
|
|
3236
|
+
writeError.err.index = originalIndex;
|
|
3237
|
+
}
|
|
3233
3238
|
if (!ordered) {
|
|
3234
3239
|
results[originalIndex] = error.writeErrors[i];
|
|
3235
3240
|
}
|
|
@@ -4391,7 +4396,7 @@ Model.validate = async function validate(obj, pathsOrOptions, context) {
|
|
|
4391
4396
|
const pieces = p.split('.');
|
|
4392
4397
|
let cur = pieces[0];
|
|
4393
4398
|
|
|
4394
|
-
for (const piece of pieces) {
|
|
4399
|
+
for (const piece of pieces.slice(1)) {
|
|
4395
4400
|
if (_pathsToValidate.has(cur)) {
|
|
4396
4401
|
return true;
|
|
4397
4402
|
}
|
|
@@ -4820,6 +4825,13 @@ function _execPopulateQuery(mod, match, select) {
|
|
|
4820
4825
|
queryOptions.perDocumentLimit = mod.options.perDocumentLimit;
|
|
4821
4826
|
}
|
|
4822
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
|
+
}
|
|
4823
4835
|
|
|
4824
4836
|
if (mod.count) {
|
|
4825
4837
|
delete queryOptions.skip;
|
|
@@ -4914,6 +4926,9 @@ function _execPopulateQuery(mod, match, select) {
|
|
|
4914
4926
|
delete query._deferredPopulate;
|
|
4915
4927
|
}
|
|
4916
4928
|
if (shouldDeferSubPopulate) {
|
|
4929
|
+
// Deferred sub-populate skips `preparePopulationOptionsMQ()`, so inherit read settings here
|
|
4930
|
+
subPopulate = utils.populate(subPopulate);
|
|
4931
|
+
setInheritedReadOptions(query, subPopulate);
|
|
4917
4932
|
deferredPopulates.push(subPopulate);
|
|
4918
4933
|
}
|
|
4919
4934
|
return { docs, deferredPopulates };
|
|
@@ -5293,7 +5308,7 @@ Model.__subclass = function subclass(conn, schema, collection) {
|
|
|
5293
5308
|
/**
|
|
5294
5309
|
* Apply changes made to this model's schema after this model was compiled.
|
|
5295
5310
|
* By default, adding virtuals and other properties to a schema after the model is compiled does nothing.
|
|
5296
|
-
* Call this function to apply virtuals and
|
|
5311
|
+
* Call this function to apply virtuals, properties, defaults, transforms, and `select` options that were added later.
|
|
5297
5312
|
*
|
|
5298
5313
|
* #### Example:
|
|
5299
5314
|
*
|
|
@@ -5327,6 +5342,9 @@ Model.recompileSchema = function recompileSchema() {
|
|
|
5327
5342
|
delete this.schema._defaultToObjectOptionsMap;
|
|
5328
5343
|
|
|
5329
5344
|
applyEmbeddedDiscriminators(this.schema, new WeakSet(), true);
|
|
5345
|
+
|
|
5346
|
+
// Must run after applying embedded discriminators, which add paths that `_hasSelectedPaths` checks
|
|
5347
|
+
this.schema._preCompile();
|
|
5330
5348
|
};
|
|
5331
5349
|
|
|
5332
5350
|
/**
|
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');
|
|
@@ -232,23 +233,6 @@ Object.defineProperty(Query.prototype, '_distinct', {
|
|
|
232
233
|
value: undefined
|
|
233
234
|
});
|
|
234
235
|
|
|
235
|
-
/**
|
|
236
|
-
* Flag to opt out of using `$geoWithin`.
|
|
237
|
-
*
|
|
238
|
-
* ```javascript
|
|
239
|
-
* mongoose.Query.use$geoWithin = false;
|
|
240
|
-
* ```
|
|
241
|
-
*
|
|
242
|
-
* @see geoWithin https://www.mongodb.com/docs/manual/reference/operator/geoWithin/
|
|
243
|
-
* @default true
|
|
244
|
-
* @property use$geoWithin
|
|
245
|
-
* @memberOf Query
|
|
246
|
-
* @static
|
|
247
|
-
* @api public
|
|
248
|
-
*/
|
|
249
|
-
|
|
250
|
-
Query.use$geoWithin = mquery.use$geoWithin;
|
|
251
|
-
|
|
252
236
|
/**
|
|
253
237
|
* Converts this query to a customized, reusable query constructor with all arguments and options retained.
|
|
254
238
|
*
|
|
@@ -291,7 +275,7 @@ Query.prototype.toConstructor = function toConstructor() {
|
|
|
291
275
|
if (!(this instanceof CustomQuery)) {
|
|
292
276
|
return new CustomQuery(criteria, options);
|
|
293
277
|
}
|
|
294
|
-
this._mongooseOptions =
|
|
278
|
+
this._mongooseOptions = cloneMongooseOptions(p._mongooseOptions);
|
|
295
279
|
Query.call(this, criteria, options || null, model, coll);
|
|
296
280
|
};
|
|
297
281
|
|
|
@@ -321,7 +305,7 @@ Query.prototype.toConstructor = function toConstructor() {
|
|
|
321
305
|
p._path = this._path;
|
|
322
306
|
p._distinct = this._distinct;
|
|
323
307
|
p._collection = this._collection;
|
|
324
|
-
p._mongooseOptions = this._mongooseOptions;
|
|
308
|
+
p._mongooseOptions = cloneMongooseOptions(this._mongooseOptions);
|
|
325
309
|
|
|
326
310
|
return CustomQuery;
|
|
327
311
|
};
|
|
@@ -369,7 +353,7 @@ Query.prototype.clone = function() {
|
|
|
369
353
|
q._path = this._path;
|
|
370
354
|
q._distinct = this._distinct;
|
|
371
355
|
q._collection = this._collection;
|
|
372
|
-
q._mongooseOptions = this._mongooseOptions;
|
|
356
|
+
q._mongooseOptions = cloneMongooseOptions(this._mongooseOptions);
|
|
373
357
|
|
|
374
358
|
return q;
|
|
375
359
|
};
|
|
@@ -903,14 +887,6 @@ Query.prototype.mod = function() {
|
|
|
903
887
|
*
|
|
904
888
|
* **MUST** be used after `where()`.
|
|
905
889
|
*
|
|
906
|
-
* #### Note:
|
|
907
|
-
*
|
|
908
|
-
* As of Mongoose 3.7, `$geoWithin` is always used for queries. To change this behavior, see [Query.use$geoWithin](https://mongoosejs.com/docs/api/query.html#Query.use$geoWithin).
|
|
909
|
-
*
|
|
910
|
-
* #### Note:
|
|
911
|
-
*
|
|
912
|
-
* In Mongoose 3.7, `within` changed from a getter to a function. If you need the old syntax, use [this](https://github.com/ebensing/mongoose-within).
|
|
913
|
-
*
|
|
914
890
|
* @method within
|
|
915
891
|
* @see $polygon https://www.mongodb.com/docs/manual/reference/operator/polygon/
|
|
916
892
|
* @see $box https://www.mongodb.com/docs/manual/reference/operator/box/
|
|
@@ -2179,29 +2155,6 @@ Query.prototype._optionsForExec = function(model) {
|
|
|
2179
2155
|
}
|
|
2180
2156
|
}
|
|
2181
2157
|
|
|
2182
|
-
if (this._mongooseOptions.populate) {
|
|
2183
|
-
if (options.readPreference) {
|
|
2184
|
-
for (const pop of Object.values(this._mongooseOptions.populate)) {
|
|
2185
|
-
if (pop.options?.readPreference === undefined) {
|
|
2186
|
-
if (!pop.options) {
|
|
2187
|
-
pop.options = {};
|
|
2188
|
-
}
|
|
2189
|
-
pop.options.readPreference = options.readPreference;
|
|
2190
|
-
}
|
|
2191
|
-
}
|
|
2192
|
-
}
|
|
2193
|
-
if (options.readConcern) {
|
|
2194
|
-
for (const pop of Object.values(this._mongooseOptions.populate)) {
|
|
2195
|
-
if (pop.options?.readConcern === undefined) {
|
|
2196
|
-
if (!pop.options) {
|
|
2197
|
-
pop.options = {};
|
|
2198
|
-
}
|
|
2199
|
-
pop.options.readConcern = options.readConcern;
|
|
2200
|
-
}
|
|
2201
|
-
}
|
|
2202
|
-
}
|
|
2203
|
-
}
|
|
2204
|
-
|
|
2205
2158
|
return options;
|
|
2206
2159
|
};
|
|
2207
2160
|
|
|
@@ -2490,7 +2443,7 @@ Query.prototype._find = async function _find() {
|
|
|
2490
2443
|
|
|
2491
2444
|
const cursor = await this.mongooseCollection.find(filter, options);
|
|
2492
2445
|
if (options.explain) {
|
|
2493
|
-
return cursor.explain();
|
|
2446
|
+
return cursor.explain(options.explain);
|
|
2494
2447
|
}
|
|
2495
2448
|
|
|
2496
2449
|
let docs = await cursor.toArray();
|
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
|
};
|
|
@@ -206,7 +208,12 @@ exports.applyPaths = function applyPaths(fields, schema, sanitizeProjection) {
|
|
|
206
208
|
const excluded = [];
|
|
207
209
|
const stack = [];
|
|
208
210
|
|
|
209
|
-
|
|
211
|
+
// Walking the schema is only necessary if the schema or one of its nested schemas
|
|
212
|
+
// has a schema-level `select`. `_hasSelectedPaths` is undefined if the schema
|
|
213
|
+
// hasn't been compiled.
|
|
214
|
+
if (schema._hasSelectedPaths !== false) {
|
|
215
|
+
analyzeSchema(schema);
|
|
216
|
+
}
|
|
210
217
|
switch (exclude) {
|
|
211
218
|
case true:
|
|
212
219
|
for (const fieldName of excluded) {
|
|
@@ -19,6 +19,7 @@ const handleSpreadDoc = require('../helpers/document/handleSpreadDoc');
|
|
|
19
19
|
const isOperator = require('../helpers/query/isOperator');
|
|
20
20
|
const utils = require('../utils');
|
|
21
21
|
const getConstructor = require('../helpers/discriminator/getConstructor');
|
|
22
|
+
const idGetter = require('../helpers/schema/idGetter');
|
|
22
23
|
const InvalidSchemaOptionError = require('../error/invalidSchemaOption');
|
|
23
24
|
|
|
24
25
|
const arrayAtomicsSymbol = require('../helpers/symbols').arrayAtomicsSymbol;
|
|
@@ -140,13 +141,17 @@ function _createConstructor(schema, options, baseClass) {
|
|
|
140
141
|
// compile an embedded document for this schema
|
|
141
142
|
function EmbeddedDocument() {
|
|
142
143
|
Subdocument.apply(this, arguments);
|
|
143
|
-
|
|
144
|
+
// `ArraySubdocument` already looked up the parent array's parent doc, so
|
|
145
|
+
// reuse it rather than going through the document array proxy again.
|
|
146
|
+
const parent = this[documentArrayParent];
|
|
147
|
+
if (parent == null) {
|
|
144
148
|
return;
|
|
145
149
|
}
|
|
146
|
-
this.$session(
|
|
150
|
+
this.$session(parent.$session());
|
|
147
151
|
}
|
|
148
152
|
|
|
149
|
-
|
|
153
|
+
// Must run before `$__setSchema()`, which compiles virtuals onto the prototype
|
|
154
|
+
schema.plugin(idGetter, { deduplicate: true });
|
|
150
155
|
|
|
151
156
|
const proto = baseClass?.prototype ?? Subdocument.prototype;
|
|
152
157
|
EmbeddedDocument.prototype = Object.create(proto);
|
|
@@ -212,6 +217,12 @@ SchemaDocumentArray.prototype.discriminator = function(name, schema, options) {
|
|
|
212
217
|
|
|
213
218
|
schema = discriminator(this.Constructor, name, schema, tiedValue, null, null, options?.overwriteExisting);
|
|
214
219
|
|
|
220
|
+
// The discriminator key has `select: true`, so `applyPaths()` needs to walk
|
|
221
|
+
// the parent schema even if the parent schema is already compiled.
|
|
222
|
+
if (this.parentSchema != null) {
|
|
223
|
+
this.parentSchema._hasSelectedPaths = true;
|
|
224
|
+
}
|
|
225
|
+
|
|
215
226
|
const EmbeddedDocument = _createConstructor(schema, null, this.Constructor);
|
|
216
227
|
EmbeddedDocument.baseCasterConstructor = this.Constructor;
|
|
217
228
|
|
package/lib/schema/number.js
CHANGED
|
@@ -323,7 +323,9 @@ SchemaNumber.prototype.enum = function(values, message) {
|
|
|
323
323
|
this.enumValidator = false;
|
|
324
324
|
}
|
|
325
325
|
|
|
326
|
-
if (values
|
|
326
|
+
if (values == null || values === false) {
|
|
327
|
+
this.enumValues = [];
|
|
328
|
+
delete this.options.enum;
|
|
327
329
|
return this;
|
|
328
330
|
}
|
|
329
331
|
|
package/lib/schema/string.js
CHANGED
|
@@ -218,7 +218,9 @@ SchemaString.prototype.enum = function() {
|
|
|
218
218
|
this.enumValidator = false;
|
|
219
219
|
}
|
|
220
220
|
|
|
221
|
-
if (arguments[0]
|
|
221
|
+
if (arguments[0] == null || arguments[0] === false) {
|
|
222
|
+
this.enumValues = [];
|
|
223
|
+
delete this.options.enum;
|
|
222
224
|
return this;
|
|
223
225
|
}
|
|
224
226
|
|
|
@@ -15,6 +15,7 @@ const castToNumber = require('./operators/helpers').castToNumber;
|
|
|
15
15
|
const discriminator = require('../helpers/model/discriminator');
|
|
16
16
|
const geospatial = require('./operators/geospatial');
|
|
17
17
|
const getConstructor = require('../helpers/discriminator/getConstructor');
|
|
18
|
+
const idGetter = require('../helpers/schema/idGetter');
|
|
18
19
|
const handleIdOption = require('../helpers/schema/handleIdOption');
|
|
19
20
|
const internalToObjectOptions = require('../options').internalToObjectOptions;
|
|
20
21
|
const isExclusive = require('../helpers/projection/isExclusive');
|
|
@@ -83,7 +84,8 @@ function _createConstructor(schema, baseClass, options) {
|
|
|
83
84
|
this.$session(parent.$session());
|
|
84
85
|
};
|
|
85
86
|
|
|
86
|
-
|
|
87
|
+
// Must run before `$__setSchema()`, which compiles virtuals onto the prototype
|
|
88
|
+
schema.plugin(idGetter, { deduplicate: true });
|
|
87
89
|
|
|
88
90
|
const proto = baseClass?.prototype ?? SubdocumentType.prototype;
|
|
89
91
|
_embedded.prototype = Object.create(proto);
|
|
@@ -347,6 +349,12 @@ SchemaSubdocument.prototype.discriminator = function(name, schema, options) {
|
|
|
347
349
|
|
|
348
350
|
schema = discriminator(this.Constructor, name, schema, value, null, null, options.overwriteExisting);
|
|
349
351
|
|
|
352
|
+
// The discriminator key has `select: true`, so `applyPaths()` needs to walk
|
|
353
|
+
// the parent schema even if the parent schema is already compiled.
|
|
354
|
+
if (this.parentSchema != null) {
|
|
355
|
+
this.parentSchema._hasSelectedPaths = true;
|
|
356
|
+
}
|
|
357
|
+
|
|
350
358
|
this.Constructor.discriminators[name] = _createConstructor(schema, this.Constructor);
|
|
351
359
|
|
|
352
360
|
return this.Constructor.discriminators[name];
|