From 0d322484b70f3eb2145bb54a91ae42db782f66ba Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Wed, 25 Oct 2023 18:01:45 -0400 Subject: [PATCH 1/5] Update array.js --- lib/schema/array.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/schema/array.js b/lib/schema/array.js index f0bcfe6c150..893c78f7b95 100644 --- a/lib/schema/array.js +++ b/lib/schema/array.js @@ -623,8 +623,8 @@ function cast$elemMatch(val, context) { discriminators[val[discriminatorKey]] != null) { return cast(discriminators[val[discriminatorKey]], val, null, this && this.$$context); } - - return cast(this.casterConstructor.schema, val, null, this && this.$$context); + const schema = this.casterConstructor.schema ?? context.schema; + return cast(schema, val, null, this && this.$$context); } const handle = SchemaArray.prototype.$conditionalHandlers = {}; From 141c28886e1f90d9a5b3e987d36c4eb8b51bda9b Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Wed, 25 Oct 2023 18:03:52 -0400 Subject: [PATCH 2/5] Update model.query.casting.test.js --- test/model.query.casting.test.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/test/model.query.casting.test.js b/test/model.query.casting.test.js index a88e74dde52..d180f34df93 100644 --- a/test/model.query.casting.test.js +++ b/test/model.query.casting.test.js @@ -754,7 +754,7 @@ describe('model query casting', function() { assert.strictEqual(doc.outerArray[0].innerArray[0], 'onetwothree'); }); }); - 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() { + it('should not throw a cast error when dealing with an array of an array of strings in combination with $elemMatch and $not (gh-13880)', async function() { const testSchema = new Schema({ arr: [[String]] }); @@ -766,6 +766,30 @@ describe('model query casting', function() { assert(res); assert(res[0].arr); }); + it('should not throw a cast error when dealing with an array of objects in combination with $elemMatch', async function() { + const testSchema = new Schema({ + arr: [Object] + }); + + const Test = db.model('Test', testSchema); + const obj1 = new Test({ arr: [{ id: 'one'}, { id: 'two' }] }); + await obj1.save(); + + const obj2 = new Test({ arr: [{ id: 'two'}, { id: 'three' }] }); + await obj2.save(); + + const obj3 = new Test({ arr: [{ id: 'three'}, { id: 'four' }] }); + await obj3.save(); + + const res = await Test.find({ + arr: { + $elemMatch: { + $or: [{ id: 'one' }, { id: 'two' }], + }, + }, + }); + assert.ok(res); + }) }); function _geojsonPoint(coordinates) { From ba58d85c417ed71d3c4cf551e70ebf48d4ad9deb Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Wed, 25 Oct 2023 18:04:08 -0400 Subject: [PATCH 3/5] Update model.query.casting.test.js --- test/model.query.casting.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/model.query.casting.test.js b/test/model.query.casting.test.js index d180f34df93..23500031942 100644 --- a/test/model.query.casting.test.js +++ b/test/model.query.casting.test.js @@ -766,7 +766,7 @@ describe('model query casting', function() { assert(res); assert(res[0].arr); }); - it('should not throw a cast error when dealing with an array of objects in combination with $elemMatch', async function() { + it('should not throw a cast error when dealing with an array of objects in combination with $elemMatch (gh-13974)', async function() { const testSchema = new Schema({ arr: [Object] }); From 34768cdd48a669d19bc332e04ca9c3483a1f1d3f Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Wed, 25 Oct 2023 18:06:07 -0400 Subject: [PATCH 4/5] fix: lint --- test/model.query.casting.test.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/model.query.casting.test.js b/test/model.query.casting.test.js index 23500031942..8ade63c57a5 100644 --- a/test/model.query.casting.test.js +++ b/test/model.query.casting.test.js @@ -772,24 +772,24 @@ describe('model query casting', function() { }); const Test = db.model('Test', testSchema); - const obj1 = new Test({ arr: [{ id: 'one'}, { id: 'two' }] }); + const obj1 = new Test({ arr: [{ id: 'one' }, { id: 'two' }] }); await obj1.save(); - const obj2 = new Test({ arr: [{ id: 'two'}, { id: 'three' }] }); + const obj2 = new Test({ arr: [{ id: 'two' }, { id: 'three' }] }); await obj2.save(); - const obj3 = new Test({ arr: [{ id: 'three'}, { id: 'four' }] }); + const obj3 = new Test({ arr: [{ id: 'three' }, { id: 'four' }] }); await obj3.save(); const res = await Test.find({ arr: { $elemMatch: { - $or: [{ id: 'one' }, { id: 'two' }], - }, - }, + $or: [{ id: 'one' }, { id: 'two' }] + } + } }); assert.ok(res); - }) + }); }); function _geojsonPoint(coordinates) { From 42afe580274d8a5bcfb02c68e30864cfc5f0f1cf Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 26 Oct 2023 17:00:05 -0400 Subject: [PATCH 5/5] Update model.query.casting.test.js --- test/model.query.casting.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/model.query.casting.test.js b/test/model.query.casting.test.js index 8ade63c57a5..c287089461c 100644 --- a/test/model.query.casting.test.js +++ b/test/model.query.casting.test.js @@ -787,8 +787,9 @@ describe('model query casting', function() { $or: [{ id: 'one' }, { id: 'two' }] } } - }); + }).sort({ _id: 1 }); assert.ok(res); + assert.deepStrictEqual(res.map(doc => doc.arr[1].id), ['two', 'three']); }); });