I want to update the only where "sku": "abcd" the "recived": 34, to "recived": 50,
I tried to solve it using updateOne(
) But unfortunately I get errors
{
"_id": {
"$oid": "62684e70e89eca781cc4f384"
},
"user": "ronen",
"roles": {
"User": 2001,
"Editor": 1984,
"Admin": 5150
},
"Stock": [
{
"sku": "777",
"productname": "rone4n",
"sendout": 5,
"recived": 3,
"totalinstock": 55,
"location": "A770770",
"_id": {
"$oid": "626a9bb384cd350c6e784794"
}
},
{
"sku": "abcd",
"productname": "ron",
"sendout": 43,
"recived": 34,
"totalinstock": 444,
"location": "fff",
"_id": {
"$oid": "626adab728fcc6005453a481"
}
}
],
}
I prefer to do this on the MongoDB side
I expect to get something like this
"sku": "abcd",
"productname": "ron",
"sendout": 43,
"recived": 50,
"totalinstock": 444,
"location": "fff",
"_id": {
"$oid": "626adab728fcc6005453a481"
CodePudding user response:
You can use .update()
method with $set
to update the value.
db.collection.update({
"stock.sku": "abcd"
},
{
"$set": {
"stock.$.recived": 50
}
})
You can check mongo playground here
CodePudding user response:
I found the answer at the end After reading online Thanks to whoever helped I attach the code Maybe it will help others
await User.updateOne(
{ Stock:
{ $elemMatch:
{ sku:req.body.sku}}},
{ $set: {"Stock.$.recived": addrecived}
}
)
}