Closed blowfishlol closed 6 days ago
The fundamental issue here is that this.set("updater", getUser());
leaves updater._id
in default
state, even though _id
wasn't modified, because of how Mongoose applies defaults to new subdocuments before setting any values. We've found a workaround for that, but that workaround causes some issues with #4145 that we're working through.
Prerequisites
Mongoose version
8.4.4
Node.js version
18.19.0
MongoDB version
6.0.15 Community
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
14.4.1 (23E224)
Issue
I have the following code that have both save and bulkWrite pre-hooks installed to a schema:
In the example above, i will get an error:
This happens because in the
bulkWrite
pre-hook, im assigning$set.updater
asgetUser()
when theop
already containsupdatedBy._id
, which is set by the "save" hook:After the![image](https://github.com/Automattic/mongoose/assets/33506114/80da59e5-9759-4cfd-9f0e-66c9df1e1bd4)
save
prehook, before thebulkWrite
prehookAnd after the the![image](https://github.com/Automattic/mongoose/assets/33506114/eb90ef6e-db4d-4bf1-8ad1-9d80d4a035ae)
bulkWrite
prehook is doneIs there any i can stop the
bulkWrite
hook from firing if i save usingbulkSave()
? Im awarebulkSave
will fire bothsave
and usesbulkWrite
under the hood.Or i should just do a check in the
bulkWrite
middleware to see if theops
already contains theupdater._id
?Other thing that i realised when testing around is that adding _id explicitly into the
updateInfoSchema
will not cause this issue, since theops
inside thebulkWrite
prehook does not contain the udpate toupdater._id