-
With a model containing a prop which is an array of subdocuments: class Item {
subItems: SubItems[]
} and an operation updating the parent: await this.itemModel
.findOneAndUpdate({ _id: payload.id }, payload, { new: true })
.exec() Is there a way (in a single query) to prevent injecting a subddocument with an arbitrary (non mongo-defined) For example for an existing {
_id: "parent_id",
subItems: [{_id: 'xxx', label: 'hey'}]
} and a payload like so is sent for update: const payload = {
_id: "parent_id"
subItems: [
{_id: 'xxx', label: 'new label'},
// here yyy is a valid but non-existing ObjectID
{_id: 'yyy', label: 'another label'}
]
} Then the second subitem will now have I could add a |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I don't know but maybe you can use I don't know if works well, but you can try something like this: using the payload example that you show const payload = {
_id: 'parent_id',
subItems: [
{ _id: 'xxx', label: 'new label' },
{ _id: 'yyy', label: 'another label' }
]
}; run that query: await this.itemModel
.findOneAndUpdate({ _id: payload.id }, { $set: { subItems: payload.subItems } }, { new: true })
.exec() |
Beta Was this translation helpful? Give feedback.
-
Answered above |
Beta Was this translation helpful? Give feedback.
Answered above