I'm using mongoose findOneAndUpdate() In mongodb i have a database with objects inside an array look like this:
patients model
{
"_id" : ObjectId("5cb939a3ba1d7d693846136c"),
"myArray" : [
{
"name" : "PW6178281935-20190425",
"treatment" : "beauty",
"value" : 0 <- i want to update this
},
{
"name" : "PW6178281935-24142444",
"treatment" : "muscle",
"value" : 0
}
]
},
{
"_id" : ObjectId("5cc123a3bb2d3123a932475f"),
"myArray" : [
{
"name" : "PW6178281935-43535555",
"treatment" : "helps",
"value" : 0
},
{
"name" : "PW6178281935-92732978",
"treatment" : "documents",
"value" : 0
}
]
}
i want to update value of an object with treatment = "beauty" of _id = "5cb939a3ba1d7d693846136c"
in schema "value" has default value of 0
i had tried with this but the value doesn't get update
patients.findOneAndUpdate({$and:[{'patients._id' : 5cb939a3ba1d7d693846136c}, {'myArray.treatment' : beauty}]}, { $set: { 'myArray.$.value': 424214 } }, { new: true });
Am I doing something wrong?
EDIT:
The answer from Frank Rose was right , the reason arrayFilters doesn't work for me because the project i was working on using mongoose 4.4 which doesn't support arrayFilters of mongoDB yet.If you want to use arrayFilters with your project please use mongoose version 5 or later.After i upgrade to mongoose 5.5 i was able to edit the object