I have documents in my collection in the format
{
"_id": "61a02dc3e044cc34ce8a3a2f",
"name": "Product 1",
"status": true,
"items": [
{
"_id": "61a02dc3e044cc34ce8a3a30",
"foodName": "Item 1",
"price": 10,
"status": true
},
{
"_id": "61a02dc3e044cc34ce8a3a31",
"foodName": "Item 2",
"price": 20,
"status": false
}
]
}
Now lets say I want to toggle the status
of where items._id
: 61a02dc3e044cc34ce8a3a31
. I tried with the positional operator but it simply doesn’t give me the result
This is the query I tried
db.collection.update({
_id: "61a02dc3e044cc34ce8a3a2f",
"items._id": "61a02dc3e044cc34ce8a3a31"
},
{
$set: {
"items.$.status": {
$not: [
{
$eq: [
"$items.$.status",
true
]
}
]
}
}
})
I get back the below which is not what I want
{
"_id": "61a02dc3e044cc34ce8a3a2f",
"items": [
{
"_id": "61a02dc3e044cc34ce8a3a30",
"foodName": "Item 1",
"price": 10,
"status": true
},
{
"_id": "61a02dc3e044cc34ce8a3a31",
"foodName": "Item 2",
"price": 20,
"status": {
"$not": [
{
"$eq": [
"$items.$.status",
true
]
}
]
}
}
],
"name": "Product 1",
"status": true
}
I mean it doesn’t read the conditions just assigns the condition object as the value. Here is the mongo playground link I tested with
Is this not possible. Any alternatives?