Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/schema/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions test/model.query.casting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
22 changes: 22 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down