I'm trying to update a field inside array of fields in mongodb. in this below structure, i want to change the values of subItems
which its value
field = item1
to be item
instead.
here's the structure
{
item: {...},
item: {
_id: ObjectId("234"),
subItems: [
{
_id: ObjectId("123"),
value: "item1",
name: "item1"
},
{
_id: ObjectId("435"),
value: "item2",
name: "item2"
}
]
}
}
I wrote code to do a bulkwrite
await db.collection('items').find({}).forEach((items) => {
items.subItems.forEach((item) => {
if (item.value === 'item1') {
console.log(items._id);
bulkOps.push({
updateOne: {
filter: { value: 'item1' },
update: { $set: { value: 'item', name: 'item' } },
},
});
}
});
});
In this array bulkOps
i got the updateOne
sentences of what i need, my question how to apply this to the database?