The mongoose update operation did not report an error but could not be updated.

problem description

I want to update a field in the subdocument array, which has been written according to the official document but cannot be updated. Please take a look at it. Thank you!

related codes

Update method:

modle.updateOne({
    "fruit.price":12,
    _id:1
},{
    $set:{
        "fruit.$.sum":30
    }
})

model instance:

{
     _id:1,
     fruit:[{
         price:12,
         sum:23,
         date:"2018.7.6",
         add:Array
     },
     {
         price:5,
         sum:40,
         date:"2018.7.6",
         add:Array
     }]
     
 }

return result:

{ Ok:1, nModified:0, n:0}

Why didn"t the update succeed when I found one? In addition, what should I do if I want to insert a value into the add field? Thank you!

Jun.16,2021

nModified:0 didn't match. fruit is an array, so what is "fruit.price": price, ?


is true. {nModified: 0} indicates that it is not found.
see that your comments are talking about the use of $, and there is nothing wrong with the document, so there is no problem with using it. This statement can be executed successfully in
shell.
so I think the problem may lie in your definition of model, so you might as well post it.

Menu