mongoose 9.11.3 → 9.12.0
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/cast.js +4 -2
- package/lib/document.js +197 -288
- package/lib/helpers/discriminator/getConstructor.js +4 -2
- package/lib/helpers/discriminator/getDiscriminatorByKey.js +17 -0
- package/lib/helpers/document/applyDefaults.js +6 -11
- package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +2 -1
- package/lib/helpers/model/discriminator.js +3 -0
- package/lib/helpers/path/setDottedPath.js +5 -0
- package/lib/helpers/populate/getModelsMapForPopulate.js +2 -1
- package/lib/helpers/query/castUpdate.js +4 -3
- package/lib/helpers/schema/getPath.js +3 -1
- package/lib/helpers/update/applyTimestampsToUpdate.js +1 -1
- package/lib/helpers/update/castArrayFilters.js +2 -1
- package/lib/model.js +18 -10
- package/lib/query.js +24 -38
- package/lib/queryHelpers.js +8 -2
- package/lib/schema/array.js +4 -2
- package/lib/schema/documentArray.js +20 -7
- 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 +120 -5
- package/lib/stateMachine.js +5 -2
- package/lib/types/array/methods/index.js +4 -1
- package/lib/types/arraySubdocument.js +11 -9
- package/lib/types/documentArray/methods/index.js +11 -6
- package/lib/types/map.js +16 -0
- package/lib/types/subdocument.js +5 -1
- package/lib/utils.js +6 -0
- package/package.json +15 -16
- package/types/types.d.ts +12 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const getDiscriminatorByKey = require('./getDiscriminatorByKey');
|
|
3
4
|
const getDiscriminatorByValue = require('./getDiscriminatorByValue');
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -15,8 +16,9 @@ module.exports = function getConstructor(Constructor, value, defaultDiscriminato
|
|
|
15
16
|
}
|
|
16
17
|
if (Constructor.discriminators &&
|
|
17
18
|
discriminatorValue != null) {
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
const discriminator = getDiscriminatorByKey(Constructor.discriminators, discriminatorValue);
|
|
20
|
+
if (discriminator) {
|
|
21
|
+
Constructor = discriminator;
|
|
20
22
|
} else {
|
|
21
23
|
const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, discriminatorValue);
|
|
22
24
|
if (constructorByValue) {
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns the discriminator registered under `key`, ignoring inherited properties
|
|
5
|
+
* like `constructor` or `toString`, because `key` may come from user data.
|
|
6
|
+
*
|
|
7
|
+
* @param {object} discriminators
|
|
8
|
+
* @param {string} key
|
|
9
|
+
* @api private
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
module.exports = function getDiscriminatorByKey(discriminators, key) {
|
|
13
|
+
if (discriminators == null || key == null) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
return Object.hasOwn(discriminators, key) ? discriminators[key] : null;
|
|
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;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const getDiscriminatorByKey = require('../discriminator/getDiscriminatorByKey');
|
|
3
4
|
const get = require('../get');
|
|
4
5
|
const getSchemaDiscriminatorByValue = require('../discriminator/getSchemaDiscriminatorByValue');
|
|
5
6
|
|
|
@@ -40,7 +41,7 @@ module.exports = function getEmbeddedDiscriminatorPath(doc, path, options) {
|
|
|
40
41
|
const discriminators = schemaType.schema.discriminators;
|
|
41
42
|
const discriminatorKey = doc.get(subpath + '.' +
|
|
42
43
|
get(schemaType, 'schema.options.discriminatorKey'));
|
|
43
|
-
if (discriminatorKey == null || discriminators
|
|
44
|
+
if (discriminatorKey == null || getDiscriminatorByKey(discriminators, discriminatorKey) == null) {
|
|
44
45
|
continue;
|
|
45
46
|
}
|
|
46
47
|
const rest = parts.slice(i + 1).join('.');
|
|
@@ -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 => {
|
|
@@ -20,6 +20,11 @@ module.exports = function setDottedPath(obj, path, val) {
|
|
|
20
20
|
if (specialProperties.has(part)) {
|
|
21
21
|
continue;
|
|
22
22
|
}
|
|
23
|
+
// Skip inherited properties like `Object.prototype.toString` to avoid
|
|
24
|
+
// setting properties on shared objects
|
|
25
|
+
if (!Object.hasOwn(cur, part) && part in cur) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
23
28
|
if (cur[part] == null) {
|
|
24
29
|
cur[part] = {};
|
|
25
30
|
}
|
|
@@ -4,6 +4,7 @@ const MongooseError = require('../../error/index');
|
|
|
4
4
|
const SkipPopulateValue = require('./skipPopulateValue');
|
|
5
5
|
const clone = require('../clone');
|
|
6
6
|
const get = require('../get');
|
|
7
|
+
const getDiscriminatorByKey = require('../discriminator/getDiscriminatorByKey');
|
|
7
8
|
const getDiscriminatorByValue = require('../discriminator/getDiscriminatorByValue');
|
|
8
9
|
const getConstructorName = require('../getConstructorName');
|
|
9
10
|
const getSchemaTypes = require('./getSchemaTypes');
|
|
@@ -846,7 +847,7 @@ function _findRefPathForDiscriminators(doc, modelSchema, data, options, normaliz
|
|
|
846
847
|
modelNames = [];
|
|
847
848
|
for (const subdoc of subdocs) {
|
|
848
849
|
const discriminatorName = utils.getValue(discriminatorKey, subdoc);
|
|
849
|
-
const discriminator = schematype.Constructor.discriminators
|
|
850
|
+
const discriminator = getDiscriminatorByKey(schematype.Constructor.discriminators, discriminatorName);
|
|
850
851
|
const discriminatorSchema = discriminator?.schema;
|
|
851
852
|
if (discriminatorSchema == null) {
|
|
852
853
|
continue;
|
|
@@ -8,6 +8,7 @@ const ValidationError = require('../../error/validation');
|
|
|
8
8
|
const castNumber = require('../../cast/number');
|
|
9
9
|
const cast = require('../../cast');
|
|
10
10
|
const getConstructorName = require('../getConstructorName');
|
|
11
|
+
const getDiscriminatorByKey = require('../discriminator/getDiscriminatorByKey');
|
|
11
12
|
const getDiscriminatorByValue = require('../discriminator/getDiscriminatorByValue');
|
|
12
13
|
const getEmbeddedDiscriminatorPath = require('./getEmbeddedDiscriminatorPath');
|
|
13
14
|
const handleImmutable = require('./handleImmutable');
|
|
@@ -71,7 +72,7 @@ module.exports = function castUpdate(schema, obj, options, context, filter) {
|
|
|
71
72
|
schema.discriminators != null) {
|
|
72
73
|
const discriminatorValue = filter[schema.options.discriminatorKey];
|
|
73
74
|
const byValue = getDiscriminatorByValue(context.model.discriminators, discriminatorValue);
|
|
74
|
-
schema = schema.discriminators
|
|
75
|
+
schema = getDiscriminatorByKey(schema.discriminators, discriminatorValue) ||
|
|
75
76
|
byValue?.schema ||
|
|
76
77
|
schema;
|
|
77
78
|
} else if (schema != null &&
|
|
@@ -80,7 +81,7 @@ module.exports = function castUpdate(schema, obj, options, context, filter) {
|
|
|
80
81
|
schema.discriminators != null) {
|
|
81
82
|
const discriminatorValue = obj[schema.options.discriminatorKey];
|
|
82
83
|
const byValue = getDiscriminatorByValue(context.model.discriminators, discriminatorValue);
|
|
83
|
-
schema = schema.discriminators
|
|
84
|
+
schema = getDiscriminatorByKey(schema.discriminators, discriminatorValue) ||
|
|
84
85
|
byValue?.schema ||
|
|
85
86
|
schema;
|
|
86
87
|
} else if (schema != null &&
|
|
@@ -90,7 +91,7 @@ module.exports = function castUpdate(schema, obj, options, context, filter) {
|
|
|
90
91
|
schema.discriminators != null) {
|
|
91
92
|
const discriminatorValue = obj.$set[schema.options.discriminatorKey];
|
|
92
93
|
const byValue = getDiscriminatorByValue(context.model.discriminators, discriminatorValue);
|
|
93
|
-
schema = schema.discriminators
|
|
94
|
+
schema = getDiscriminatorByKey(schema.discriminators, discriminatorValue) ||
|
|
94
95
|
byValue?.schema ||
|
|
95
96
|
schema;
|
|
96
97
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const getDiscriminatorByKey = require('../discriminator/getDiscriminatorByKey');
|
|
4
|
+
|
|
3
5
|
const numberRE = /^\d+$/;
|
|
4
6
|
|
|
5
7
|
/**
|
|
@@ -30,7 +32,7 @@ module.exports = function getPath(schema, path, discriminatorValueMap) {
|
|
|
30
32
|
isArray = true;
|
|
31
33
|
}
|
|
32
34
|
if (discriminatorValueMap && discriminatorValueMap[cur]) {
|
|
33
|
-
schema = schema.discriminators
|
|
35
|
+
schema = getDiscriminatorByKey(schema.discriminators, discriminatorValueMap[cur]) ?? schema;
|
|
34
36
|
}
|
|
35
37
|
cur = '';
|
|
36
38
|
} else if (schematype?.instance === 'Mixed') {
|
|
@@ -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 } });
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const getDiscriminatorByKey = require('../discriminator/getDiscriminatorByKey');
|
|
3
4
|
const castFilterPath = require('../query/castFilterPath');
|
|
4
5
|
const cleanPositionalOperators = require('../schema/cleanPositionalOperators');
|
|
5
6
|
const getPath = require('../schema/getPath');
|
|
@@ -89,7 +90,7 @@ function _castArrayFilters(arrayFilters, schema, strictQuery, updatedPathsByFilt
|
|
|
89
90
|
let filterBaseSchema = baseSchematype != null ? baseSchematype.schema : null;
|
|
90
91
|
if (filterBaseSchema?.discriminators != null &&
|
|
91
92
|
filter[filterWildcardPath + '.' + filterBaseSchema.options.discriminatorKey]) {
|
|
92
|
-
filterBaseSchema = filterBaseSchema.discriminators
|
|
93
|
+
filterBaseSchema = getDiscriminatorByKey(filterBaseSchema.discriminators, filter[filterWildcardPath + '.' + filterBaseSchema.options.discriminatorKey]) || filterBaseSchema;
|
|
93
94
|
discriminatorValueMap[baseFilterPath] = filter[filterWildcardPath + '.' + filterBaseSchema.options.discriminatorKey];
|
|
94
95
|
}
|
|
95
96
|
|
package/lib/model.js
CHANGED
|
@@ -50,6 +50,7 @@ const discriminator = require('./helpers/model/discriminator');
|
|
|
50
50
|
const each = require('./helpers/each');
|
|
51
51
|
const get = require('./helpers/get');
|
|
52
52
|
const getConstructorName = require('./helpers/getConstructorName');
|
|
53
|
+
const getDiscriminatorByKey = require('./helpers/discriminator/getDiscriminatorByKey');
|
|
53
54
|
const getDiscriminatorByValue = require('./helpers/discriminator/getDiscriminatorByValue');
|
|
54
55
|
const getModelsMapForPopulate = require('./helpers/populate/getModelsMapForPopulate');
|
|
55
56
|
const immediate = require('./helpers/immediate');
|
|
@@ -1792,7 +1793,7 @@ function _ensureIndexes(model, options, callback) {
|
|
|
1792
1793
|
applyWriteConcern(model.schema, indexOptions);
|
|
1793
1794
|
applySchemaCollation(indexFields, indexOptions, model.schema.options);
|
|
1794
1795
|
|
|
1795
|
-
indexSingleStart(indexFields,
|
|
1796
|
+
indexSingleStart(indexFields, indexOptions);
|
|
1796
1797
|
|
|
1797
1798
|
// Just in case `createIndex()` throws a sync error
|
|
1798
1799
|
let promise = null;
|
|
@@ -2793,7 +2794,7 @@ Model.create = async function create(doc, options) {
|
|
|
2793
2794
|
let toSave = doc;
|
|
2794
2795
|
|
|
2795
2796
|
const Model = this.discriminators && doc[discriminatorKey] != null ?
|
|
2796
|
-
this.discriminators
|
|
2797
|
+
getDiscriminatorByKey(this.discriminators, doc[discriminatorKey]) || getDiscriminatorByValue(this.discriminators, doc[discriminatorKey]) :
|
|
2797
2798
|
this;
|
|
2798
2799
|
if (Model == null) {
|
|
2799
2800
|
throw new MongooseError(`Discriminator "${doc[discriminatorKey]}" not ` +
|
|
@@ -2814,7 +2815,7 @@ Model.create = async function create(doc, options) {
|
|
|
2814
2815
|
try {
|
|
2815
2816
|
const doc = args[i];
|
|
2816
2817
|
const Model = this.discriminators && doc[discriminatorKey] != null ?
|
|
2817
|
-
this.discriminators
|
|
2818
|
+
getDiscriminatorByKey(this.discriminators, doc[discriminatorKey]) || getDiscriminatorByValue(this.discriminators, doc[discriminatorKey]) :
|
|
2818
2819
|
this;
|
|
2819
2820
|
if (Model == null) {
|
|
2820
2821
|
throw new MongooseError(`Discriminator "${doc[discriminatorKey]}" not ` +
|
|
@@ -2839,7 +2840,7 @@ Model.create = async function create(doc, options) {
|
|
|
2839
2840
|
} else if (!immediateError) {
|
|
2840
2841
|
res = await Promise.allSettled(args.map(async doc => {
|
|
2841
2842
|
const Model = this.discriminators && doc[discriminatorKey] != null ?
|
|
2842
|
-
this.discriminators
|
|
2843
|
+
getDiscriminatorByKey(this.discriminators, doc[discriminatorKey]) || getDiscriminatorByValue(this.discriminators, doc[discriminatorKey]) :
|
|
2843
2844
|
this;
|
|
2844
2845
|
if (Model == null) {
|
|
2845
2846
|
throw new MongooseError(`Discriminator "${doc[discriminatorKey]}" not ` +
|
|
@@ -2860,7 +2861,7 @@ Model.create = async function create(doc, options) {
|
|
|
2860
2861
|
let firstError = null;
|
|
2861
2862
|
res = await Promise.all(args.map(async doc => {
|
|
2862
2863
|
const Model = this.discriminators && doc[discriminatorKey] != null ?
|
|
2863
|
-
this.discriminators
|
|
2864
|
+
getDiscriminatorByKey(this.discriminators, doc[discriminatorKey]) || getDiscriminatorByValue(this.discriminators, doc[discriminatorKey]) :
|
|
2864
2865
|
this;
|
|
2865
2866
|
if (Model == null) {
|
|
2866
2867
|
throw new MongooseError(`Discriminator "${doc[discriminatorKey]}" not ` +
|
|
@@ -2923,7 +2924,7 @@ Model.insertOne = async function insertOne(doc, options) {
|
|
|
2923
2924
|
|
|
2924
2925
|
const discriminatorKey = this.schema.options.discriminatorKey;
|
|
2925
2926
|
const Model = this.discriminators && doc[discriminatorKey] != null ?
|
|
2926
|
-
this.discriminators
|
|
2927
|
+
getDiscriminatorByKey(this.discriminators, doc[discriminatorKey]) || getDiscriminatorByValue(this.discriminators, doc[discriminatorKey]) :
|
|
2927
2928
|
this;
|
|
2928
2929
|
if (Model == null) {
|
|
2929
2930
|
throw new MongooseError(
|
|
@@ -3227,8 +3228,12 @@ async function _insertMany(arr, options) {
|
|
|
3227
3228
|
|
|
3228
3229
|
if (error.writeErrors != null) {
|
|
3229
3230
|
for (let i = 0; i < error.writeErrors.length; ++i) {
|
|
3230
|
-
const
|
|
3231
|
-
|
|
3231
|
+
const writeError = error.writeErrors[i];
|
|
3232
|
+
const originalIndex = validDocIndexToOriginalIndex[writeError.index];
|
|
3233
|
+
// `index` is a getter-only property on the driver's `WriteError` that reads from `err`
|
|
3234
|
+
if (writeError.err != null) {
|
|
3235
|
+
writeError.err.index = originalIndex;
|
|
3236
|
+
}
|
|
3232
3237
|
if (!ordered) {
|
|
3233
3238
|
results[originalIndex] = error.writeErrors[i];
|
|
3234
3239
|
}
|
|
@@ -5070,7 +5075,7 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
|
|
|
5070
5075
|
}
|
|
5071
5076
|
|
|
5072
5077
|
// If discriminator key is set, use the discriminator instead (gh-7586)
|
|
5073
|
-
const Discriminator = model.discriminators
|
|
5078
|
+
const Discriminator = getDiscriminatorByKey(model.discriminators, doc[discriminatorKey]) ||
|
|
5074
5079
|
getDiscriminatorByValue(model.discriminators, doc[discriminatorKey]);
|
|
5075
5080
|
if (Discriminator != null) {
|
|
5076
5081
|
return new Discriminator(doc, fields, skipId);
|
|
@@ -5292,7 +5297,7 @@ Model.__subclass = function subclass(conn, schema, collection) {
|
|
|
5292
5297
|
/**
|
|
5293
5298
|
* Apply changes made to this model's schema after this model was compiled.
|
|
5294
5299
|
* By default, adding virtuals and other properties to a schema after the model is compiled does nothing.
|
|
5295
|
-
* Call this function to apply virtuals and
|
|
5300
|
+
* Call this function to apply virtuals, properties, defaults, transforms, and `select` options that were added later.
|
|
5296
5301
|
*
|
|
5297
5302
|
* #### Example:
|
|
5298
5303
|
*
|
|
@@ -5326,6 +5331,9 @@ Model.recompileSchema = function recompileSchema() {
|
|
|
5326
5331
|
delete this.schema._defaultToObjectOptionsMap;
|
|
5327
5332
|
|
|
5328
5333
|
applyEmbeddedDiscriminators(this.schema, new WeakSet(), true);
|
|
5334
|
+
|
|
5335
|
+
// Must run after applying embedded discriminators, which add paths that `_hasSelectedPaths` checks
|
|
5336
|
+
this.schema._preCompile();
|
|
5329
5337
|
};
|
|
5330
5338
|
|
|
5331
5339
|
/**
|
package/lib/query.js
CHANGED
|
@@ -232,23 +232,6 @@ Object.defineProperty(Query.prototype, '_distinct', {
|
|
|
232
232
|
value: undefined
|
|
233
233
|
});
|
|
234
234
|
|
|
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
235
|
/**
|
|
253
236
|
* Converts this query to a customized, reusable query constructor with all arguments and options retained.
|
|
254
237
|
*
|
|
@@ -432,6 +415,14 @@ Query.prototype.clone = function() {
|
|
|
432
415
|
* @api public
|
|
433
416
|
*/
|
|
434
417
|
|
|
418
|
+
Query.prototype.where = function where(path) {
|
|
419
|
+
if (typeof path === 'string' && specialProperties.has(path)) {
|
|
420
|
+
throw new MongooseError(`Cannot use \`${path}\` as a query path`);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
return Query.base.where.apply(this, arguments);
|
|
424
|
+
};
|
|
425
|
+
|
|
435
426
|
/**
|
|
436
427
|
* Specifies a `$slice` projection for an array.
|
|
437
428
|
*
|
|
@@ -812,7 +803,13 @@ Query.prototype.mod = function() {
|
|
|
812
803
|
path = arguments[0];
|
|
813
804
|
}
|
|
814
805
|
|
|
815
|
-
|
|
806
|
+
if (path != null && specialProperties.has(String(path))) {
|
|
807
|
+
throw new MongooseError(`Cannot use \`${path}\` as a query path`);
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
// Avoid reusing inherited properties like `toString`
|
|
811
|
+
const conds = (Object.hasOwn(this._conditions, path) && this._conditions[path]) ||
|
|
812
|
+
(this._conditions[path] = {});
|
|
816
813
|
conds.$mod = val;
|
|
817
814
|
return this;
|
|
818
815
|
};
|
|
@@ -889,14 +886,6 @@ Query.prototype.mod = function() {
|
|
|
889
886
|
*
|
|
890
887
|
* **MUST** be used after `where()`.
|
|
891
888
|
*
|
|
892
|
-
* #### Note:
|
|
893
|
-
*
|
|
894
|
-
* 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).
|
|
895
|
-
*
|
|
896
|
-
* #### Note:
|
|
897
|
-
*
|
|
898
|
-
* 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).
|
|
899
|
-
*
|
|
900
889
|
* @method within
|
|
901
890
|
* @see $polygon https://www.mongodb.com/docs/manual/reference/operator/polygon/
|
|
902
891
|
* @see $box https://www.mongodb.com/docs/manual/reference/operator/box/
|
|
@@ -2405,14 +2394,10 @@ Query.prototype.mongooseOptions = function(v) {
|
|
|
2405
2394
|
*/
|
|
2406
2395
|
|
|
2407
2396
|
Query.prototype._castConditions = function() {
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
sanitizeFilterOpt = this.model.base.options.sanitizeFilter;
|
|
2413
|
-
} else {
|
|
2414
|
-
sanitizeFilterOpt = this._mongooseOptions.sanitizeFilter;
|
|
2415
|
-
}
|
|
2397
|
+
const sanitizeFilterOpt = this.model?.db.config?.sanitizeFilter ??
|
|
2398
|
+
this.model?.db.options?.sanitizeFilter ??
|
|
2399
|
+
this.model?.base.options.sanitizeFilter ??
|
|
2400
|
+
this._mongooseOptions.sanitizeFilter;
|
|
2416
2401
|
|
|
2417
2402
|
if (sanitizeFilterOpt) {
|
|
2418
2403
|
sanitizeFilter(this._conditions);
|
|
@@ -2480,7 +2465,7 @@ Query.prototype._find = async function _find() {
|
|
|
2480
2465
|
|
|
2481
2466
|
const cursor = await this.mongooseCollection.find(filter, options);
|
|
2482
2467
|
if (options.explain) {
|
|
2483
|
-
return cursor.explain();
|
|
2468
|
+
return cursor.explain(options.explain);
|
|
2484
2469
|
}
|
|
2485
2470
|
|
|
2486
2471
|
let docs = await cursor.toArray();
|
|
@@ -5243,9 +5228,10 @@ Query.prototype.cast = function(model, obj) {
|
|
|
5243
5228
|
if ('strictQuery' in this._mongooseOptions) {
|
|
5244
5229
|
opts.strictQuery = this._mongooseOptions.strictQuery;
|
|
5245
5230
|
}
|
|
5246
|
-
|
|
5247
|
-
|
|
5248
|
-
|
|
5231
|
+
opts.sanitizeFilter = model.db.config?.sanitizeFilter ??
|
|
5232
|
+
model.db.options?.sanitizeFilter ??
|
|
5233
|
+
model.base.options.sanitizeFilter ??
|
|
5234
|
+
this._mongooseOptions.sanitizeFilter;
|
|
5249
5235
|
|
|
5250
5236
|
try {
|
|
5251
5237
|
return cast(model.schema, obj, opts, this);
|
package/lib/queryHelpers.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* Module dependencies
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
const getDiscriminatorByKey = require('./helpers/discriminator/getDiscriminatorByKey');
|
|
7
8
|
const PopulateOptions = require('./options/populateOptions');
|
|
8
9
|
const checkEmbeddedDiscriminatorKeyProjection =
|
|
9
10
|
require('./helpers/discriminator/checkEmbeddedDiscriminatorKeyProjection');
|
|
@@ -98,7 +99,7 @@ exports.createModel = function createModel(model, doc, fields, userProvidedField
|
|
|
98
99
|
|
|
99
100
|
const value = doc[key];
|
|
100
101
|
if (key && value && model.discriminators) {
|
|
101
|
-
const discriminator = model.discriminators
|
|
102
|
+
const discriminator = getDiscriminatorByKey(model.discriminators, value) || getDiscriminatorByValue(model.discriminators, value);
|
|
102
103
|
if (discriminator) {
|
|
103
104
|
const _fields = clone(userProvidedFields);
|
|
104
105
|
exports.applyPaths(_fields, discriminator.schema);
|
|
@@ -205,7 +206,12 @@ exports.applyPaths = function applyPaths(fields, schema, sanitizeProjection) {
|
|
|
205
206
|
const excluded = [];
|
|
206
207
|
const stack = [];
|
|
207
208
|
|
|
208
|
-
|
|
209
|
+
// Walking the schema is only necessary if the schema or one of its nested schemas
|
|
210
|
+
// has a schema-level `select`. `_hasSelectedPaths` is undefined if the schema
|
|
211
|
+
// hasn't been compiled.
|
|
212
|
+
if (schema._hasSelectedPaths !== false) {
|
|
213
|
+
analyzeSchema(schema);
|
|
214
|
+
}
|
|
209
215
|
switch (exclude) {
|
|
210
216
|
case true:
|
|
211
217
|
for (const fieldName of excluded) {
|
package/lib/schema/array.js
CHANGED
|
@@ -22,6 +22,7 @@ const util = require('util');
|
|
|
22
22
|
const utils = require('../utils');
|
|
23
23
|
const castToNumber = require('./operators/helpers').castToNumber;
|
|
24
24
|
const geospatial = require('./operators/geospatial');
|
|
25
|
+
const getDiscriminatorByKey = require('../helpers/discriminator/getDiscriminatorByKey');
|
|
25
26
|
const getDiscriminatorByValue = require('../helpers/discriminator/getDiscriminatorByValue');
|
|
26
27
|
|
|
27
28
|
let MongooseArray;
|
|
@@ -507,8 +508,9 @@ SchemaArray.prototype._castForQuery = function(val, context) {
|
|
|
507
508
|
const discriminators = embeddedSchemaType?.discriminators;
|
|
508
509
|
|
|
509
510
|
if (val && discriminators && typeof discriminatorKey === 'string') {
|
|
510
|
-
|
|
511
|
-
|
|
511
|
+
const discriminator = getDiscriminatorByKey(discriminators, val[discriminatorKey]);
|
|
512
|
+
if (discriminator) {
|
|
513
|
+
embeddedSchemaType = discriminator;
|
|
512
514
|
} else {
|
|
513
515
|
const constructorByValue = getDiscriminatorByValue(discriminators, val[discriminatorKey]);
|
|
514
516
|
if (constructorByValue) {
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* Module dependencies.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
const getDiscriminatorByKey = require('../helpers/discriminator/getDiscriminatorByKey');
|
|
7
8
|
const CastError = require('../error/cast');
|
|
8
9
|
const DocumentArrayElement = require('./documentArrayElement');
|
|
9
10
|
const EventEmitter = require('events').EventEmitter;
|
|
@@ -18,6 +19,7 @@ const handleSpreadDoc = require('../helpers/document/handleSpreadDoc');
|
|
|
18
19
|
const isOperator = require('../helpers/query/isOperator');
|
|
19
20
|
const utils = require('../utils');
|
|
20
21
|
const getConstructor = require('../helpers/discriminator/getConstructor');
|
|
22
|
+
const idGetter = require('../helpers/schema/idGetter');
|
|
21
23
|
const InvalidSchemaOptionError = require('../error/invalidSchemaOption');
|
|
22
24
|
|
|
23
25
|
const arrayAtomicsSymbol = require('../helpers/symbols').arrayAtomicsSymbol;
|
|
@@ -139,13 +141,17 @@ function _createConstructor(schema, options, baseClass) {
|
|
|
139
141
|
// compile an embedded document for this schema
|
|
140
142
|
function EmbeddedDocument() {
|
|
141
143
|
Subdocument.apply(this, arguments);
|
|
142
|
-
|
|
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) {
|
|
143
148
|
return;
|
|
144
149
|
}
|
|
145
|
-
this.$session(
|
|
150
|
+
this.$session(parent.$session());
|
|
146
151
|
}
|
|
147
152
|
|
|
148
|
-
|
|
153
|
+
// Must run before `$__setSchema()`, which compiles virtuals onto the prototype
|
|
154
|
+
schema.plugin(idGetter, { deduplicate: true });
|
|
149
155
|
|
|
150
156
|
const proto = baseClass?.prototype ?? Subdocument.prototype;
|
|
151
157
|
EmbeddedDocument.prototype = Object.create(proto);
|
|
@@ -211,6 +217,12 @@ SchemaDocumentArray.prototype.discriminator = function(name, schema, options) {
|
|
|
211
217
|
|
|
212
218
|
schema = discriminator(this.Constructor, name, schema, tiedValue, null, null, options?.overwriteExisting);
|
|
213
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
|
+
|
|
214
226
|
const EmbeddedDocument = _createConstructor(schema, null, this.Constructor);
|
|
215
227
|
EmbeddedDocument.baseCasterConstructor = this.Constructor;
|
|
216
228
|
|
|
@@ -652,10 +664,11 @@ function cast$elemMatch(val, context) {
|
|
|
652
664
|
// If so, use the discriminator schema. See gh-7449
|
|
653
665
|
const discriminatorKey = this?.Constructor?.schema?.options?.discriminatorKey;
|
|
654
666
|
const discriminators = this?.Constructor?.schema?.discriminators || {};
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
667
|
+
const discriminator = discriminatorKey != null && val[discriminatorKey] != null ?
|
|
668
|
+
getDiscriminatorByKey(discriminators, val[discriminatorKey]) :
|
|
669
|
+
null;
|
|
670
|
+
if (discriminator != null) {
|
|
671
|
+
return cast(discriminator, val, null, this?.$$context);
|
|
659
672
|
}
|
|
660
673
|
|
|
661
674
|
const schema = this.Constructor.schema ?? context.schema;
|
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];
|