Reputation: 23
{
"_id" : ObjectId("5925e2213daf48359cb5429e"),
"email" : "[email protected]",
"firstname" : "test3",
"friends" : [
{
"friend_email" : "[email protected]",
"status" : 1,
"fname" : "test2"
},
{
"friend_email" : "[email protected]",
"status" : 1,
"fname" : "test1"
}
]
}
My database looks like above. The "friends" can have many entries inside it. I have to search a particular entry (depending upon a variable value) with (suppose) friend_email = "[email protected]" and email = "[email protected]" and change its particular field "status" to 0. I have written this following update query,
collection_temp.update({
"email": email([email protected]) ,
"friends" :
{$elemMatch :
{"friend_email" : friend_req_sent_to ([email protected])}
}},
{"$set":
{"friends.$.status" : 0}
},
function(err, result) {
if (result) {
console.log("SUCCESS");
} else {
console.log("FAIL");
}
});
My query runs correctly but the status field does not change....please tell me where am I wrong?
Upvotes: 0
Views: 80
Reputation: 21161
The query works fine, you may be returning incorrect values from those functions you are using (email
and friend_req_sent_to
).
With all status
fields set to 1, running:
db.test.update({
email: "[email protected]",
friends: { $elemMatch: { friend_email: "[email protected]" } }
}, { $set: { "friends.$.status": 0 } })
Returns:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
And if I check with a find
I can see the document was properly updated.
Upvotes: 1