Suppose I have the following the document structure.
[
{
"_id": 1,
"depots": [
{
"_id": 1,
"isFavourite": true
},
{
"_id": 2,
"isFavourite": false
},
{
"_id": 3,
"isFavourite": true
},
{
"_id": 4,
"isFavourite": false
}
]
}
]
I want to write a single update query which filters for the document with _id: 1 and first sets every isFavourite value to false and then sets the second isFavourite value (or any specified index) to true.
The resulting document should look like this.
[
{
"_id": 1,
"depots": [
{
"_id": 1,
"isFavourite": false
},
{
"_id": 2,
"isFavourite": true
},
{
"_id": 3,
"isFavourite": false
},
{
"_id": 4,
"isFavourite": false
}
]
}
]
What I tried:
db.collection.update({
_id: 1
},
[
{
"$set": {
"depots.isFavourite": false
}
},
{
"$set": {
"depots.2.isFavourite": true
}
}
])
Yet strangely this does not work. See the linked playground for the result of this query.