mongoose 7.8.11 → 7.8.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.umd.js +1 -1
- package/lib/cast.js +12 -8
- package/lib/document.js +11 -1
- package/lib/helpers/discriminator/getConstructor.js +4 -2
- package/lib/helpers/discriminator/getDiscriminatorByKey.js +17 -0
- package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +2 -1
- package/lib/helpers/indexes/isIndexEqual.js +10 -8
- package/lib/helpers/path/setDottedPath.js +5 -0
- package/lib/helpers/populate/getModelsMapForPopulate.js +2 -1
- package/lib/helpers/update/castArrayFilters.js +2 -1
- package/lib/model.js +4 -3
- package/lib/query.js +35 -21
- package/lib/queryhelpers.js +2 -1
- package/lib/schema/array.js +11 -7
- package/lib/schema.js +6 -1
- package/lib/types/DocumentArray/methods/index.js +11 -6
- package/lib/utils.js +6 -0
- package/package.json +2 -2
package/lib/cast.js
CHANGED
|
@@ -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 StrictModeError = require('./error/strict');
|
|
9
10
|
const Types = require('./schema/index');
|
|
@@ -29,6 +30,7 @@ const ALLOWED_GEOWITHIN_GEOJSON_TYPES = ['Polygon', 'MultiPolygon'];
|
|
|
29
30
|
* @param {Boolean|"throw"} [options.strict] Wheter to enable all strict options
|
|
30
31
|
* @param {Boolean|"throw"} [options.strictQuery] Enable strict Queries
|
|
31
32
|
* @param {Boolean} [options.upsert]
|
|
33
|
+
* @param {Boolean} [options.sanitizeFilter] avoid adding implicit query selectors ($in)
|
|
32
34
|
* @param {Query} [context] passed to setters
|
|
33
35
|
* @api private
|
|
34
36
|
*/
|
|
@@ -139,16 +141,18 @@ module.exports = function cast(schema, obj, options, context) {
|
|
|
139
141
|
pathLastHalf !== discriminatorKey) {
|
|
140
142
|
const discriminatorVal = get(obj, pathFirstHalf + '.' + discriminatorKey);
|
|
141
143
|
const discriminators = _schematype.schema.discriminators;
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
schematype = discriminators[discriminatorVal].path(pathLastHalf);
|
|
145
|
-
} else if (discriminatorVal != null &&
|
|
144
|
+
const discriminator = typeof discriminatorVal === 'string' ? getDiscriminatorByKey(discriminators, discriminatorVal) : null;
|
|
145
|
+
const discriminatorFromIn = discriminatorVal != null &&
|
|
146
146
|
Object.keys(discriminatorVal).length === 1 &&
|
|
147
147
|
Array.isArray(discriminatorVal.$in) &&
|
|
148
148
|
discriminatorVal.$in.length === 1 &&
|
|
149
|
-
typeof discriminatorVal.$in[0] === 'string'
|
|
150
|
-
discriminators
|
|
151
|
-
|
|
149
|
+
typeof discriminatorVal.$in[0] === 'string' ?
|
|
150
|
+
getDiscriminatorByKey(discriminators, discriminatorVal.$in[0]) :
|
|
151
|
+
null;
|
|
152
|
+
if (discriminator != null) {
|
|
153
|
+
schematype = discriminator.path(pathLastHalf);
|
|
154
|
+
} else if (discriminatorFromIn != null) {
|
|
155
|
+
schematype = discriminatorFromIn.path(pathLastHalf);
|
|
152
156
|
}
|
|
153
157
|
}
|
|
154
158
|
}
|
|
@@ -372,7 +376,7 @@ module.exports = function cast(schema, obj, options, context) {
|
|
|
372
376
|
|
|
373
377
|
}
|
|
374
378
|
}
|
|
375
|
-
} else if (Array.isArray(val) && ['Buffer', 'Array'].indexOf(schematype.instance) === -1) {
|
|
379
|
+
} else if (Array.isArray(val) && ['Buffer', 'Array'].indexOf(schematype.instance) === -1 && !options.sanitizeFilter) {
|
|
376
380
|
const casted = [];
|
|
377
381
|
const valuesArray = val;
|
|
378
382
|
|
package/lib/document.js
CHANGED
|
@@ -758,7 +758,11 @@ function init(self, obj, doc, opts, prefix) {
|
|
|
758
758
|
|
|
759
759
|
const value = obj[i];
|
|
760
760
|
if (!schemaType && utils.isPOJO(value)) {
|
|
761
|
-
// assume nested object
|
|
761
|
+
// assume nested object. Skip inherited properties like `Object.prototype.toString`
|
|
762
|
+
// to avoid setting properties on shared objects
|
|
763
|
+
if (!utils.object.hasOwnProperty(doc, i) && i in doc) {
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
762
766
|
if (!doc[i]) {
|
|
763
767
|
doc[i] = {};
|
|
764
768
|
if (!strict && !(i in docSchema.tree) && !(i in docSchema.methods) && !(i in docSchema.virtuals)) {
|
|
@@ -1726,6 +1730,12 @@ Document.prototype.$__set = function(pathToMark, path, options, constructing, pa
|
|
|
1726
1730
|
} else {
|
|
1727
1731
|
const isMap = obj instanceof Map;
|
|
1728
1732
|
let value = isMap ? obj.get(parts[i]) : obj[parts[i]];
|
|
1733
|
+
// Skip inherited properties like `Object.prototype.toString`, document methods,
|
|
1734
|
+
// or document internals like `$__schema` to avoid setting properties on shared
|
|
1735
|
+
// objects. Document properties are getters on the prototype, so check `_doc`.
|
|
1736
|
+
if (!isMap && !utils.object.hasOwnProperty(obj.$__ == null ? obj : obj._doc, parts[i]) && parts[i] in obj) {
|
|
1737
|
+
return;
|
|
1738
|
+
}
|
|
1729
1739
|
if (utils.isPOJO(value)) {
|
|
1730
1740
|
obj = value;
|
|
1731
1741
|
} else if (value && value instanceof Embedded) {
|
|
@@ -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.prototype.hasOwnProperty.call(discriminators, key) ? discriminators[key] : null;
|
|
17
|
+
};
|
|
@@ -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('.');
|
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
const get = require('../get');
|
|
4
4
|
const utils = require('../../utils');
|
|
5
|
+
|
|
6
|
+
const simpleCollation = Object.freeze({ locale: 'simple' });
|
|
7
|
+
|
|
5
8
|
/**
|
|
6
9
|
* Given a Mongoose index definition (key + options objects) and a MongoDB server
|
|
7
10
|
* index definition, determine if the two indexes are equal.
|
|
@@ -62,14 +65,13 @@ module.exports = function isIndexEqual(schemaIndexKeysObject, options, dbIndex)
|
|
|
62
65
|
continue;
|
|
63
66
|
}
|
|
64
67
|
if (key === 'collation') {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const
|
|
69
|
-
const
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
if (get(schemaCollation, opt) !== get(dbCollation, opt)) {
|
|
68
|
+
// No collation is equivalent to { locale: 'simple' }. MongoDB server 9
|
|
69
|
+
// reports { locale: 'simple' } for indexes created with no collation,
|
|
70
|
+
// older versions omit it.
|
|
71
|
+
const schemaCollation = options.collation ?? simpleCollation;
|
|
72
|
+
const dbCollation = dbIndex.collation ?? simpleCollation;
|
|
73
|
+
for (const opt of Object.keys(schemaCollation)) {
|
|
74
|
+
if (schemaCollation[opt] !== dbCollation[opt]) {
|
|
73
75
|
return false;
|
|
74
76
|
}
|
|
75
77
|
}
|
|
@@ -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.prototype.hasOwnProperty.call(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');
|
|
@@ -712,7 +713,7 @@ function _findRefPathForDiscriminators(doc, modelSchema, data, options, normaliz
|
|
|
712
713
|
modelNames = [];
|
|
713
714
|
for (const subdoc of subdocs) {
|
|
714
715
|
const discriminatorName = utils.getValue(discriminatorKey, subdoc);
|
|
715
|
-
const discriminator = schematype.caster.discriminators
|
|
716
|
+
const discriminator = getDiscriminatorByKey(schematype.caster.discriminators, discriminatorName);
|
|
716
717
|
const discriminatorSchema = discriminator && discriminator.schema;
|
|
717
718
|
if (discriminatorSchema == null) {
|
|
718
719
|
continue;
|
|
@@ -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');
|
|
@@ -63,7 +64,7 @@ function _castArrayFilters(arrayFilters, schema, strictQuery, updatedPathsByFilt
|
|
|
63
64
|
if (filterBaseSchema != null &&
|
|
64
65
|
filterBaseSchema.discriminators != null &&
|
|
65
66
|
filter[filterWildcardPath + '.' + filterBaseSchema.options.discriminatorKey]) {
|
|
66
|
-
filterBaseSchema = filterBaseSchema.discriminators
|
|
67
|
+
filterBaseSchema = getDiscriminatorByKey(filterBaseSchema.discriminators, filter[filterWildcardPath + '.' + filterBaseSchema.options.discriminatorKey]) || filterBaseSchema;
|
|
67
68
|
}
|
|
68
69
|
|
|
69
70
|
for (const key of keys) {
|
package/lib/model.js
CHANGED
|
@@ -42,6 +42,7 @@ const firstKey = require('./helpers/firstKey');
|
|
|
42
42
|
const each = require('./helpers/each');
|
|
43
43
|
const get = require('./helpers/get');
|
|
44
44
|
const getConstructorName = require('./helpers/getConstructorName');
|
|
45
|
+
const getDiscriminatorByKey = require('./helpers/discriminator/getDiscriminatorByKey');
|
|
45
46
|
const getDiscriminatorByValue = require('./helpers/discriminator/getDiscriminatorByValue');
|
|
46
47
|
const getModelsMapForPopulate = require('./helpers/populate/getModelsMapForPopulate');
|
|
47
48
|
const immediate = require('./helpers/immediate');
|
|
@@ -2878,7 +2879,7 @@ Model.create = async function create(doc, options) {
|
|
|
2878
2879
|
try {
|
|
2879
2880
|
const doc = args[i];
|
|
2880
2881
|
const Model = this.discriminators && doc[discriminatorKey] != null ?
|
|
2881
|
-
this.discriminators
|
|
2882
|
+
getDiscriminatorByKey(this.discriminators, doc[discriminatorKey]) || getDiscriminatorByValue(this.discriminators, doc[discriminatorKey]) :
|
|
2882
2883
|
this;
|
|
2883
2884
|
if (Model == null) {
|
|
2884
2885
|
throw new MongooseError(`Discriminator "${doc[discriminatorKey]}" not ` +
|
|
@@ -2905,7 +2906,7 @@ Model.create = async function create(doc, options) {
|
|
|
2905
2906
|
const promiseType = !immediateError ? Promise.allSettled.bind(Promise) : Promise.all.bind(Promise);
|
|
2906
2907
|
let p = promiseType(args.map(async doc => {
|
|
2907
2908
|
const Model = this.discriminators && doc[discriminatorKey] != null ?
|
|
2908
|
-
this.discriminators
|
|
2909
|
+
getDiscriminatorByKey(this.discriminators, doc[discriminatorKey]) || getDiscriminatorByValue(this.discriminators, doc[discriminatorKey]) :
|
|
2909
2910
|
this;
|
|
2910
2911
|
if (Model == null) {
|
|
2911
2912
|
throw new MongooseError(`Discriminator "${doc[discriminatorKey]}" not ` +
|
|
@@ -4757,7 +4758,7 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
|
|
|
4757
4758
|
}
|
|
4758
4759
|
|
|
4759
4760
|
// If discriminator key is set, use the discriminator instead (gh-7586)
|
|
4760
|
-
const Discriminator = model.discriminators
|
|
4761
|
+
const Discriminator = getDiscriminatorByKey(model.discriminators, doc[discriminatorKey]) ||
|
|
4761
4762
|
getDiscriminatorByValue(model.discriminators, doc[discriminatorKey]);
|
|
4762
4763
|
if (Discriminator != null) {
|
|
4763
4764
|
return new Discriminator(doc, fields, skipId);
|
package/lib/query.js
CHANGED
|
@@ -20,6 +20,7 @@ const castNumber = require('./cast/number');
|
|
|
20
20
|
const castUpdate = require('./helpers/query/castUpdate');
|
|
21
21
|
const clone = require('./helpers/clone');
|
|
22
22
|
const completeMany = require('./helpers/query/completeMany');
|
|
23
|
+
const getDiscriminatorByKey = require('./helpers/discriminator/getDiscriminatorByKey');
|
|
23
24
|
const getDiscriminatorByValue = require('./helpers/discriminator/getDiscriminatorByValue');
|
|
24
25
|
const hasDollarKeys = require('./helpers/query/hasDollarKeys');
|
|
25
26
|
const helpers = require('./queryhelpers');
|
|
@@ -37,6 +38,7 @@ const sanitizeFilter = require('./helpers/query/sanitizeFilter');
|
|
|
37
38
|
const sanitizeProjection = require('./helpers/query/sanitizeProjection');
|
|
38
39
|
const selectPopulatedFields = require('./helpers/query/selectPopulatedFields');
|
|
39
40
|
const setDefaultsOnInsert = require('./helpers/setDefaultsOnInsert');
|
|
41
|
+
const specialProperties = require('./helpers/specialProperties');
|
|
40
42
|
const updateValidators = require('./helpers/updateValidators');
|
|
41
43
|
const util = require('util');
|
|
42
44
|
const utils = require('./utils');
|
|
@@ -371,6 +373,14 @@ Query.prototype.clone = function() {
|
|
|
371
373
|
* @api public
|
|
372
374
|
*/
|
|
373
375
|
|
|
376
|
+
Query.prototype.where = function where(path) {
|
|
377
|
+
if (typeof path === 'string' && specialProperties.has(path)) {
|
|
378
|
+
throw new MongooseError(`Cannot use \`${path}\` as a query path`);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return Query.base.where.apply(this, arguments);
|
|
382
|
+
};
|
|
383
|
+
|
|
374
384
|
/**
|
|
375
385
|
* Specifies a `$slice` projection for an array.
|
|
376
386
|
*
|
|
@@ -747,7 +757,13 @@ Query.prototype.mod = function() {
|
|
|
747
757
|
path = arguments[0];
|
|
748
758
|
}
|
|
749
759
|
|
|
750
|
-
|
|
760
|
+
if (path != null && specialProperties.has(String(path))) {
|
|
761
|
+
throw new MongooseError(`Cannot use \`${path}\` as a query path`);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// Avoid reusing inherited properties like `toString`
|
|
765
|
+
const conds = (utils.object.hasOwnProperty(this._conditions, path) && this._conditions[path]) ||
|
|
766
|
+
(this._conditions[path] = {});
|
|
751
767
|
conds.$mod = val;
|
|
752
768
|
return this;
|
|
753
769
|
};
|
|
@@ -2220,14 +2236,10 @@ Query.prototype.mongooseOptions = function(v) {
|
|
|
2220
2236
|
*/
|
|
2221
2237
|
|
|
2222
2238
|
Query.prototype._castConditions = function() {
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
sanitizeFilterOpt = this.model.base.options.sanitizeFilter;
|
|
2228
|
-
} else {
|
|
2229
|
-
sanitizeFilterOpt = this._mongooseOptions.sanitizeFilter;
|
|
2230
|
-
}
|
|
2239
|
+
const sanitizeFilterOpt = this.model?.db.config?.sanitizeFilter ??
|
|
2240
|
+
this.model?.db.options?.sanitizeFilter ??
|
|
2241
|
+
this.model?.base.options.sanitizeFilter ??
|
|
2242
|
+
this._mongooseOptions.sanitizeFilter;
|
|
2231
2243
|
|
|
2232
2244
|
if (sanitizeFilterOpt) {
|
|
2233
2245
|
sanitizeFilter(this._conditions);
|
|
@@ -2625,11 +2637,7 @@ Query.prototype.findOne = function(conditions, projection, options) {
|
|
|
2625
2637
|
*/
|
|
2626
2638
|
|
|
2627
2639
|
Query.prototype._count = async function _count() {
|
|
2628
|
-
|
|
2629
|
-
this.cast(this.model);
|
|
2630
|
-
} catch (err) {
|
|
2631
|
-
this.error(err);
|
|
2632
|
-
}
|
|
2640
|
+
this._castConditions();
|
|
2633
2641
|
|
|
2634
2642
|
if (this.error()) {
|
|
2635
2643
|
throw this.error();
|
|
@@ -2655,11 +2663,7 @@ Query.prototype._count = async function _count() {
|
|
|
2655
2663
|
*/
|
|
2656
2664
|
|
|
2657
2665
|
Query.prototype._countDocuments = async function _countDocuments() {
|
|
2658
|
-
|
|
2659
|
-
this.cast(this.model);
|
|
2660
|
-
} catch (err) {
|
|
2661
|
-
this.error(err);
|
|
2662
|
-
}
|
|
2666
|
+
this._castConditions();
|
|
2663
2667
|
|
|
2664
2668
|
if (this.error()) {
|
|
2665
2669
|
throw this.error();
|
|
@@ -4719,7 +4723,7 @@ Query.prototype._castUpdate = function _castUpdate(obj, overwrite) {
|
|
|
4719
4723
|
schema.discriminators != null) {
|
|
4720
4724
|
const discriminatorValue = filter[schema.options.discriminatorKey];
|
|
4721
4725
|
const byValue = getDiscriminatorByValue(this.model.discriminators, discriminatorValue);
|
|
4722
|
-
schema = schema.discriminators
|
|
4726
|
+
schema = getDiscriminatorByKey(schema.discriminators, discriminatorValue) ||
|
|
4723
4727
|
(byValue && byValue.schema) ||
|
|
4724
4728
|
schema;
|
|
4725
4729
|
}
|
|
@@ -4922,6 +4926,10 @@ Query.prototype.cast = function(model, obj) {
|
|
|
4922
4926
|
opts.strictQuery = this.options.strictQuery;
|
|
4923
4927
|
}
|
|
4924
4928
|
}
|
|
4929
|
+
opts.sanitizeFilter = model.db.config?.sanitizeFilter ??
|
|
4930
|
+
model.db.options?.sanitizeFilter ??
|
|
4931
|
+
model.base.options.sanitizeFilter ??
|
|
4932
|
+
this._mongooseOptions.sanitizeFilter;
|
|
4925
4933
|
|
|
4926
4934
|
try {
|
|
4927
4935
|
return cast(model.schema, obj, opts, this);
|
|
@@ -5057,11 +5065,17 @@ Query.prototype.cursor = function cursor(opts) {
|
|
|
5057
5065
|
}
|
|
5058
5066
|
|
|
5059
5067
|
try {
|
|
5060
|
-
this.
|
|
5068
|
+
this._castConditions();
|
|
5061
5069
|
} catch (err) {
|
|
5070
|
+
// `_castConditions()` reports cast errors on the query, but `sanitizeFilter`
|
|
5071
|
+
// throws for filters it refuses outright, like `$where`.
|
|
5062
5072
|
return (new QueryCursor(this))._markError(err);
|
|
5063
5073
|
}
|
|
5064
5074
|
|
|
5075
|
+
if (this.error()) {
|
|
5076
|
+
return (new QueryCursor(this))._markError(this.error());
|
|
5077
|
+
}
|
|
5078
|
+
|
|
5065
5079
|
return new QueryCursor(this);
|
|
5066
5080
|
};
|
|
5067
5081
|
|
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 checkEmbeddedDiscriminatorKeyProjection =
|
|
8
9
|
require('./helpers/discriminator/checkEmbeddedDiscriminatorKeyProjection');
|
|
9
10
|
const get = require('./helpers/get');
|
|
@@ -105,7 +106,7 @@ exports.createModel = function createModel(model, doc, fields, userProvidedField
|
|
|
105
106
|
|
|
106
107
|
const value = doc[key];
|
|
107
108
|
if (key && value && model.discriminators) {
|
|
108
|
-
const discriminator = model.discriminators
|
|
109
|
+
const discriminator = getDiscriminatorByKey(model.discriminators, value) || getDiscriminatorByValue(model.discriminators, value);
|
|
109
110
|
if (discriminator) {
|
|
110
111
|
const _fields = clone(userProvidedFields);
|
|
111
112
|
exports.applyPaths(_fields, discriminator.schema);
|
package/lib/schema/array.js
CHANGED
|
@@ -19,6 +19,7 @@ const util = require('util');
|
|
|
19
19
|
const utils = require('../utils');
|
|
20
20
|
const castToNumber = require('./operators/helpers').castToNumber;
|
|
21
21
|
const geospatial = require('./operators/geospatial');
|
|
22
|
+
const getDiscriminatorByKey = require('../helpers/discriminator/getDiscriminatorByKey');
|
|
22
23
|
const getDiscriminatorByValue = require('../helpers/discriminator/getDiscriminatorByValue');
|
|
23
24
|
|
|
24
25
|
let MongooseArray;
|
|
@@ -497,9 +498,11 @@ SchemaArray.prototype._castForQuery = function(val, context) {
|
|
|
497
498
|
Constructor.schema &&
|
|
498
499
|
Constructor.schema.options &&
|
|
499
500
|
Constructor.schema.options.discriminatorKey) {
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
501
|
+
const discriminator = typeof val[Constructor.schema.options.discriminatorKey] === 'string' ?
|
|
502
|
+
getDiscriminatorByKey(Constructor.discriminators, val[Constructor.schema.options.discriminatorKey]) :
|
|
503
|
+
null;
|
|
504
|
+
if (discriminator) {
|
|
505
|
+
Constructor = discriminator;
|
|
503
506
|
} else {
|
|
504
507
|
const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, val[Constructor.schema.options.discriminatorKey]);
|
|
505
508
|
if (constructorByValue) {
|
|
@@ -618,10 +621,11 @@ function cast$elemMatch(val, context) {
|
|
|
618
621
|
this.casterConstructor &&
|
|
619
622
|
this.casterConstructor.schema &&
|
|
620
623
|
this.casterConstructor.schema.discriminators || {};
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
624
|
+
const discriminator = discriminatorKey != null && val[discriminatorKey] != null ?
|
|
625
|
+
getDiscriminatorByKey(discriminators, val[discriminatorKey]) :
|
|
626
|
+
null;
|
|
627
|
+
if (discriminator != null) {
|
|
628
|
+
return cast(discriminator, val, null, this && this.$$context);
|
|
625
629
|
}
|
|
626
630
|
const schema = this.casterConstructor.schema ?? context.schema;
|
|
627
631
|
return cast(schema, val, null, this && this.$$context);
|
package/lib/schema.js
CHANGED
|
@@ -2244,6 +2244,12 @@ Schema.prototype.virtual = function(name, options) {
|
|
|
2244
2244
|
if (name instanceof VirtualType || getConstructorName(name) === 'VirtualType') {
|
|
2245
2245
|
return this.virtual(name.path, name.options);
|
|
2246
2246
|
}
|
|
2247
|
+
const parts = name.split('.');
|
|
2248
|
+
for (const part of parts) {
|
|
2249
|
+
if (utils.specialProperties.has(part)) {
|
|
2250
|
+
throw new MongooseError('Cannot set special property `' + part + '` on a schema');
|
|
2251
|
+
}
|
|
2252
|
+
}
|
|
2247
2253
|
options = new VirtualOptions(options);
|
|
2248
2254
|
|
|
2249
2255
|
if (utils.hasUserDefinedProperty(options, ['ref', 'refPath'])) {
|
|
@@ -2330,7 +2336,6 @@ Schema.prototype.virtual = function(name, options) {
|
|
|
2330
2336
|
}
|
|
2331
2337
|
|
|
2332
2338
|
const virtuals = this.virtuals;
|
|
2333
|
-
const parts = name.split('.');
|
|
2334
2339
|
|
|
2335
2340
|
if (this.pathType(name) === 'real') {
|
|
2336
2341
|
throw new Error('Virtual path "' + name + '"' +
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const ArrayMethods = require('../../array/methods');
|
|
4
4
|
const Document = require('../../../document');
|
|
5
5
|
const castObjectId = require('../../../cast/objectid');
|
|
6
|
+
const getDiscriminatorByKey = require('../../../helpers/discriminator/getDiscriminatorByKey');
|
|
6
7
|
const getDiscriminatorByValue = require('../../../helpers/discriminator/getDiscriminatorByValue');
|
|
7
8
|
const internalToObjectOptions = require('../../../options').internalToObjectOptions;
|
|
8
9
|
const utils = require('../../../utils');
|
|
@@ -75,9 +76,11 @@ const methods = {
|
|
|
75
76
|
Constructor.schema &&
|
|
76
77
|
Constructor.schema.options &&
|
|
77
78
|
Constructor.schema.options.discriminatorKey) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
const discriminator = typeof value[Constructor.schema.options.discriminatorKey] === 'string' ?
|
|
80
|
+
getDiscriminatorByKey(Constructor.discriminators, value[Constructor.schema.options.discriminatorKey]) :
|
|
81
|
+
null;
|
|
82
|
+
if (discriminator) {
|
|
83
|
+
Constructor = discriminator;
|
|
81
84
|
} else {
|
|
82
85
|
const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, value[Constructor.schema.options.discriminatorKey]);
|
|
83
86
|
if (constructorByValue) {
|
|
@@ -268,9 +271,11 @@ const methods = {
|
|
|
268
271
|
Constructor.schema &&
|
|
269
272
|
Constructor.schema.options &&
|
|
270
273
|
Constructor.schema.options.discriminatorKey) {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
+
const discriminator = typeof obj[Constructor.schema.options.discriminatorKey] === 'string' ?
|
|
275
|
+
getDiscriminatorByKey(Constructor.discriminators, obj[Constructor.schema.options.discriminatorKey]) :
|
|
276
|
+
null;
|
|
277
|
+
if (discriminator) {
|
|
278
|
+
Constructor = discriminator;
|
|
274
279
|
} else {
|
|
275
280
|
const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, obj[Constructor.schema.options.discriminatorKey]);
|
|
276
281
|
if (constructorByValue) {
|
package/lib/utils.js
CHANGED
|
@@ -882,6 +882,12 @@ exports.mergeClone = function(to, fromObj) {
|
|
|
882
882
|
if (specialProperties.has(key)) {
|
|
883
883
|
continue;
|
|
884
884
|
}
|
|
885
|
+
// Skip inherited properties like `Object.prototype.toString` to avoid setting
|
|
886
|
+
// properties on shared objects. Document properties are getters on the
|
|
887
|
+
// prototype, so check `_doc`.
|
|
888
|
+
if (!exports.object.hasOwnProperty(to.$__ == null ? to : to._doc, key) && key in to) {
|
|
889
|
+
continue;
|
|
890
|
+
}
|
|
885
891
|
if (typeof to[key] === 'undefined') {
|
|
886
892
|
to[key] = clone(fromObj[key], {
|
|
887
893
|
transform: false,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mongoose",
|
|
3
3
|
"description": "Mongoose MongoDB ODM",
|
|
4
|
-
"version": "7.8.
|
|
4
|
+
"version": "7.8.13",
|
|
5
5
|
"author": "Guillermo Rauch <guillermo@learnboost.com>",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"mongodb",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"kareem": "2.5.1",
|
|
24
24
|
"mongodb": "5.9.2",
|
|
25
25
|
"mpath": "0.9.0",
|
|
26
|
-
"mquery": "5.0.
|
|
26
|
+
"mquery": "5.0.1",
|
|
27
27
|
"ms": "2.1.3",
|
|
28
28
|
"sift": "16.0.1"
|
|
29
29
|
},
|