Document.prototype.set() on nested objects value erase existing previous values
See original GitHub issuePrerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
6.5.1
Node.js version
16.13.1
MongoDB server version
5.0.9
Description
The issue seems to impact the version 6 of mongoose. I recently discovered this bug since i updated to the last version 6 because of a discovered vulnerability that affect older versions to the 6.4…
Steps to Reproduce
Whenever you use the use set
with a flattened key/value annotation, it’s seems to work correctly.
await document.set({ "path.subpath": "some value" }).save()
this would work properly assuming that the path potentially contain other subpaths that would not be affected by this change. But if i would write it with a nested object annotation instead then:
await document.set({ path: { subpath: "some value" } }).save()
this would affect the rest of the element potentially contained in path and erase them. The temporary solution that i found here is to manually inject them with a spread operator.
await document.set({ path: { ...document.path, subpath: "some value" } }).save()
but this is not matching the behavior of the $set
from MongoDb and in some case it turn out to be very limiting.
Expected Behavior
await document.set({ path: { subpath: "some value" } }).save()
should not erase all the other elements contained within the path
key.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (1 by maintainers)
Top GitHub Comments
@ramos-ph this is expected behavior. You can set the
merge
option onset()
to merge instead of overwrite. See below changes to @IslandRhythms 's script: