diff --git a/lib/schema/array.js b/lib/schema/array.js index f0fc7a9a3a4..e49963c76e9 100644 --- a/lib/schema/array.js +++ b/lib/schema/array.js @@ -659,14 +659,25 @@ handle.$and = createLogicalQueryOperatorHandler('$and'); handle.$nor = createLogicalQueryOperatorHandler('$nor'); function createLogicalQueryOperatorHandler(op) { - return function logicalQueryOperatorHandler(val, context) { + return function logicalQueryOperatorHandler(val) { if (!Array.isArray(val)) { throw new TypeError('conditional ' + op + ' requires an array'); } + const embeddedSchema = this.embeddedSchemaType?.schema; + if (embeddedSchema == null) { + // Arrays whose items have no schema (`[Mixed]`, `[String]`, `Array`, ...) + // contain arbitrary values, so `$or`/`$and`/`$nor` clauses are not + // subdocument paths. Casting them against the parent schema would strip + // unknown paths when `strictQuery` is on, cast fields that happen to + // share a name with a parent path, and throw when there is no query + // context, like in `bulkWrite()`. Leave the clauses as-is instead. + return val; + } + const ret = []; for (const obj of val) { - ret.push(cast(this.embeddedSchemaType.schema ?? context.schema, obj, null, this?.$$context)); + ret.push(cast(embeddedSchema, obj, null, this?.$$context)); } return ret; diff --git a/test/model.query.casting.test.js b/test/model.query.casting.test.js index ddcd1c8ae3b..ac544c3117f 100644 --- a/test/model.query.casting.test.js +++ b/test/model.query.casting.test.js @@ -830,6 +830,40 @@ describe('model query casting', function() { assert.deepStrictEqual(res.map(doc => doc.arr[1].id), ['two', 'three']); }); + it('keeps $or/$and/$nor clauses inside $elemMatch as-is for arrays without an item schema (gh-16754)', function() { + const Test = db.model('Test', new Schema({ + program: Number, + mixedParams: [Schema.Types.Mixed], + stringParams: [String], + bareArray: Array + }, { strictQuery: true })); + + const clauses = [ + { programId: 'p1' }, + { program: 'Program A' } + ]; + + for (const path of ['mixedParams', 'stringParams', 'bareArray']) { + for (const op of ['$or', '$and', '$nor']) { + const filter = Test.find({ [path]: { $elemMatch: { [op]: clauses } } }).cast(); + assert.deepStrictEqual(filter[path].$elemMatch[op], clauses); + } + } + }); + + it('still casts logical operator clauses inside $elemMatch for document arrays (gh-16754)', function() { + const Test = db.model('Test', new Schema({ + params: [new Schema({ id: mongoose.Schema.Types.ObjectId, program: String })] + }, { strictQuery: true })); + + const filter = Test.find({ + params: { $elemMatch: { $or: [{ id: '1'.repeat(24) }, { program: 'A' }] } } + }).cast(); + + assert.ok(filter.params.$elemMatch.$or[0].id instanceof mongoose.Types.ObjectId); + assert.strictEqual(filter.params.$elemMatch.$or[1].program, 'A'); + }); + it('should not throw a cast error when dealing with an array of objects in combination with $elemMatch and nested $and', async function() { const testSchema = new Schema({ arr: [Object] diff --git a/test/model.test.js b/test/model.test.js index fa62fec9bde..ef0fa570ac5 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -4876,6 +4876,28 @@ describe('Model', function() { ); }); + it('casts $or inside $elemMatch on arrays without an item schema (gh-16754)', async function() { + const M = db.model('Test', Schema({ + name: String, + params: [Schema.Types.Mixed] + }, { strictQuery: true })); + + await M.create([ + { name: 'match', params: [{ programId: 'p1' }] }, + { name: 'other', params: [{ programId: 'p2' }] } + ]); + + await M.bulkWrite([{ + updateOne: { + filter: { params: { $elemMatch: { $or: [{ programId: 'p1' }] } } }, + update: { $set: { name: 'updated' } } + } + }]); + + assert.ok(await M.exists({ name: 'updated' })); + assert.ok(await M.exists({ name: 'other' })); + }); + it('with timestamps and replaceOne (gh-5708)', async function() { const schema = new Schema({ num: Number }, { timestamps: true });