Skip to content

Commit c1606b6

Browse files
authored
Merge pull request #7207 from lineus/fix-7098
fixes #7098 don't skip casting the update for array of array values
2 parents e9d538e + 64c6d15 commit c1606b6

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/helpers/query/castUpdate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ function castUpdateVal(schema, val, op, $conditional, context, path) {
411411
return schema.castForQueryWrapper({
412412
val: val,
413413
context: context,
414-
$skipQueryCastForUpdate: val != null && schema.$isMongooseArray
414+
$skipQueryCastForUpdate: val != null && schema.$isMongooseArray && schema.$parentSchema
415415
});
416416
}
417417

test/model.update.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,6 +2536,48 @@ describe('model: update:', function() {
25362536
catch(done);
25372537
});
25382538

2539+
it('doesn\'t skip casting the query on nested arrays (gh-7098)', function() {
2540+
const nestedSchema = new Schema({
2541+
xyz: [[Number]]
2542+
});
2543+
const schema = new Schema({
2544+
xyz: [[{ type: Number }]],
2545+
nested: nestedSchema
2546+
});
2547+
2548+
const Model = db.model('gh-7098', schema);
2549+
2550+
const test = new Model({
2551+
xyz: [
2552+
[0, 1],
2553+
[2, 3],
2554+
[4, 5]
2555+
],
2556+
nested: {
2557+
xyz: [
2558+
[0, 1],
2559+
[2, 3],
2560+
[4, 5]
2561+
],
2562+
}
2563+
});
2564+
2565+
const cond = { _id: test._id };
2566+
const update = { $set: { 'xyz.1.0': '200', 'nested.xyz.1.0': '200' } };
2567+
const opts = { new: true };
2568+
2569+
return co(function*() {
2570+
let inserted = yield test.save();
2571+
inserted = inserted.toObject();
2572+
assert.deepStrictEqual(inserted.xyz, [[0, 1], [2, 3], [4, 5]]);
2573+
assert.deepStrictEqual(inserted.nested.xyz, [[0, 1], [2, 3], [4, 5]]);
2574+
let updated = yield Model.findOneAndUpdate(cond, update, opts);
2575+
updated = updated.toObject();
2576+
assert.deepStrictEqual(updated.xyz, [[0, 1], [200, 3], [4, 5]]);
2577+
assert.deepStrictEqual(updated.nested.xyz, [[0, 1], [200, 3], [4, 5]]);
2578+
});
2579+
});
2580+
25392581
it('defaults with overwrite and no update validators (gh-5384)', function(done) {
25402582
const testSchema = new mongoose.Schema({
25412583
name: String,

0 commit comments

Comments
 (0)