Skip to content

Commit 9a41011

Browse files
authored
Merge pull request #14066 from Automattic/vkarpov15/gh-13977
fix: handle update validators and single nested doc with numberic paths
2 parents 49b406c + 855cee0 commit 9a41011

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/schema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2577,7 +2577,7 @@ Schema.prototype._getSchema = function(path) {
25772577
if (parts[p] === '$' || isArrayFilter(parts[p])) {
25782578
if (p + 1 === parts.length) {
25792579
// comments.$
2580-
return foundschema;
2580+
return foundschema.$embeddedSchemaType;
25812581
}
25822582
// comments.$.comments.$.title
25832583
ret = search(parts.slice(p + 1), foundschema.schema);

test/model.updateOne.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,6 +3069,22 @@ describe('model: updateOne: ', function() {
30693069
const doc = await Test.findById(_id);
30703070
assert.equal(doc.$myKey, 'gh13786');
30713071
});
3072+
it('works with update validators and single nested doc with numberic paths (gh-13977)', async function() {
3073+
const subdoc = new mongoose.Schema({
3074+
1: { type: String, required: true, validate: () => true }
3075+
});
3076+
const schema = new mongoose.Schema({ subdoc });
3077+
const Test = db.model('Test', schema);
3078+
3079+
const _id = new mongoose.Types.ObjectId();
3080+
await Test.updateOne(
3081+
{ _id },
3082+
{ subdoc: { 1: 'foobar' } },
3083+
{ upsert: true, runValidators: true }
3084+
);
3085+
const doc = await Test.findById(_id);
3086+
assert.equal(doc.subdoc['1'], 'foobar');
3087+
});
30723088
});
30733089

30743090
async function delay(ms) {

0 commit comments

Comments
 (0)