Consider this data
{
"_id" : ...,
"array" : [
{ "name" : "value1","flag" : true } ,
{ "name" : "value2","flag" : false }
]
}
I would like to switch the second element of the array (from false to true)
I know that I can update a specific element using the very useful $ positional operator, for example:
db.myCollection.update(
{'array.name':'value2'},
{
$set: {
'array.$.flag':true
}
},false,true);
But is there a way to use the $ positional operator also to set the value?
eg. like this?
db.myCollection.update(
{'array.name':'value2'},
{
$set: {
'array.$.flag':'!array.$.flag'
}
},false,true);
source
share