-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pre validate: is not triggert on all sub documents when set via _.set() with a nested path #14591
Closed
2 tasks done
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
IslandRhythms
added
the
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
label
May 15, 2024
const mongoose = require('mongoose');
const { Schema, model } = mongoose;
const createSchema = (path,subSchema) => {
const schema = new Schema({[path]: subSchema})
schema.pre('validate', function () {
console.debug(' - pre.validate ::',path)
})
schema.pre('save', function () {
console.debug(' - pre.save ::',path)
})
return schema
}
const e = createSchema('e', {type: String})
const d = createSchema('d', {type: e})
const c = createSchema('c', {type: d})
const b = createSchema('b', {type: c})
const a = createSchema('a', {type: b})
const NestedModelHooksTestModel = model('NestedModelHooksTest', a)
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
// Variant 1
console.debug('Creating doc: {a: {b: {c: {d: {e: "SomeDate"}}}}}')
const newData = {a: {b: {c: {d: {e: new Date().toString()}}}}}
const doc = await new NestedModelHooksTestModel(newData).save()
// Variant 2
console.warn('Updating via new value: {a: {b: {c: {d: {e: "Some override value"}}}}}')
doc.set({a: {b: {c: {d: {e: "Some override value"}}}}})
await doc.save()
// Variant 3
console.warn('Updating via doc: doc.a.b.c.d.e = \'updated nested value 2\'')
doc.a.b.c.d.e = 'updated nested value 2'
await doc.save()
// Variant 4
console.warn('Updating via set: {\'a.b.c.d.e\': \'updated nested value\'}')
doc.set({'a.b.c.d.e': 'updated nested value'})
await doc.save()
}
run(); |
vkarpov15
added a commit
that referenced
this issue
May 17, 2024
…subdoc when modifying after save() Fix #14591
vkarpov15
added a commit
that referenced
this issue
May 24, 2024
fix(document): fire pre validate hooks on 5 level deep single nested subdoc when modifying after save()
This was referenced Jun 22, 2024
This was referenced Jun 22, 2024
This was referenced Jul 17, 2024
This was referenced Jul 18, 2024
This was referenced Jul 22, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites
Mongoose version
8.3.4
Node.js version
v20
MongoDB server version
5.0
Typescript version (if applicable)
5.4.5
Description
If I have a multi nested document like this:
doc = {a: {b: {c: {d: {e: string} } } } }
And then I change a value via
await doc._set({'a.b.c.d.e': 'value'}).save()
the value is saved correctly in the nestede
subDocument. But during the saving process not all thepre('validate', () => ..)
hooks get triggered.Only on the first (
a
) and second subdocument (b
) the pre validate hook is triggered.Steps to Reproduce
Expected Behavior
For every variant I expected the following output:
This is indeed the output for variants 1, 2 and 3
But when using
doc.set({'a.b.c.d.e': 'updated nested value'})
, aka variant 4, the value is saved correctly but not all the pre validate hooks are triggered:I was expecting to see the following pre validate hooks get triggered as well:
The text was updated successfully, but these errors were encountered: