Caibao Kuang
Caibao Kuang

Reputation: 47

MongoDb changes value in embedded doc

{ 
"_id" : ObjectId("54fd4ddaa037ba481d794f5e"), 
"question" : "let me go?", 
"choices" : [ 
    { 
        "text" : "yes", 
        "_id" : ObjectId("54fd4ddaa037ba481d794f60"), 
        "votes" : [ ] 
    }, { 
        "text" : "y", 
        "_id" : ObjectId("54fd4ddaa037ba481d794f5f"), 
        "votes" : [ ] 
    } 
], 
"__v" : 0 
}

I want to change the value "y" to "no", How? wish your answer thanks

Upvotes: 2

Views: 32

Answers (1)

chridam
chridam

Reputation: 103305

Use the $set operator together with the $ positional operator in your update to set elements in array without explicitly specifying the position of the element in the array i.e.

db.collection.update(
    { "question": "let me go?", "choices.text": "y" }, 
    { $set: { "choices.$.text": "no" } }
);

Upvotes: 1

Related Questions