Sivaprakash D
Sivaprakash D

Reputation: 328

Mongoose update query - Mongoose/Mongodb

I have a collection called student, now I want to update the object value inside array based on condition. Can anyone help me to figure this logic please.

Student:

{
_id: "5996d10e0b992e5def651db4"
name: "Siva"
mark:[
{subject:"Tamil",mark:"50"},
{subject:"English",mark:"25"},
{subject:"Science",mark:"25"},
]
__v: 36
}

Expected Result: I want to update only Tamil mark by checking the subject value...

Upvotes: 0

Views: 31

Answers (1)

Ayoub
Ayoub

Reputation: 1435

You can update sub-array element or object by using positional operator $, in your example it should something like this:

Student.update(
    { "mark.subject": "Tamil" },
    { "$set": { "mark.$.mark": "60" } }
)

You can read more documentation here

Upvotes: 1

Related Questions