Skip to content

Commit 3dc87cb

Browse files
committed
fix: handle casting $elemMatch underneath $not underneath another $elemMatch
Fix #13880
1 parent d8429aa commit 3dc87cb

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ module.exports = {
1313
'*.min.js',
1414
'**/docs/js/native.js',
1515
'!.*',
16-
'node_modules'
16+
'node_modules',
17+
'.git'
1718
],
1819
overrides: [
1920
{

lib/cast.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,21 @@ module.exports = function cast(schema, obj, options, context) {
309309
while (k--) {
310310
$cond = ks[k];
311311
nested = val[$cond];
312-
313-
if ($cond === '$not') {
312+
if ($cond === '$elemMatch') {
313+
if (nested && schematype != null && schematype.schema != null) {
314+
cast(schematype.schema, nested, options, context);
315+
} else if (nested && schematype != null && schematype.$isMongooseArray) {
316+
if (utils.isPOJO(nested) && nested.$not != null) {
317+
cast(schema, nested, options, context);
318+
} else {
319+
val[$cond] = schematype.castForQuery(
320+
$cond,
321+
nested,
322+
context
323+
);
324+
}
325+
}
326+
} else if ($cond === '$not') {
314327
if (nested && schematype) {
315328
_keys = Object.keys(nested);
316329
if (_keys.length && isOperator(_keys[0])) {
@@ -337,6 +350,7 @@ module.exports = function cast(schema, obj, options, context) {
337350
context
338351
);
339352
}
353+
340354
}
341355
}
342356
} else if (Array.isArray(val) && ['Buffer', 'Array'].indexOf(schematype.instance) === -1) {

test/model.query.casting.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,18 @@ describe('model query casting', function() {
754754
assert.strictEqual(doc.outerArray[0].innerArray[0], 'onetwothree');
755755
});
756756
});
757+
it('should not throw a cast error when dealing with an array of an array of strings in combination with $elemMach and $not (gh-13880)', async function() {
758+
const testSchema = new Schema({
759+
arr: [[String]]
760+
});
761+
const Test = db.model('Test', testSchema);
762+
const doc = new Test({ arr: [[1, 2, 3], [2, 3, 4]] });
763+
await doc.save();
764+
const query = { arr: { $elemMatch: { $not: { $elemMatch: { $eq: '1' } } } } };
765+
const res = await Test.find(query);
766+
assert(res);
767+
assert(res[0].arr);
768+
});
757769
});
758770

759771
function _geojsonPoint(coordinates) {

0 commit comments

Comments
 (0)