Reputation: 959
I have this document in mongo:
{
id: objectId,
list: [
{
id: internalObjectId1,
enabled: true
},
{
id: internalObjectId2,
enabled: false
}]
}
I need to change the enabled field. How can I do it?
Upvotes: 0
Views: 42
Reputation: 103435
Use the positional $ operator. Suppose you have the following document in the collection whose list
element value is an array of embedded documents:
{
"_id" : ObjectId("551be1a04db8a16ac729432e"),
"list" : [
{
"id" : ObjectId("54f43159c922ac0b4387ef9c"),
"enabled" : true
},
{
"id" : ObjectId("54f43159c922ac0b4387ef9d"),
"enabled" : false
}
]
}
The following will update the value of the enabled
field in the embedded document with the id
of 54f43159c922ac0b4387ef9d
to true
:
db.collection.update(
{
"_id": ObjectId("551be1a04db8a16ac729432e"),
"list.id": ObjectId("54f43159c922ac0b4387ef9d")
},
{
"$set": {"list.$.enabled": true}
}
)
Upvotes: 1