How to conditionally unset the value mongodb

How can we setup conditionally unset value

if watched.3 exists then remover 3 and fans = -2 else add watched.3 and fans +1

db.collection.update({
_id: ObjectId(“60e80c96b9c55e7a01898f5c”)
},
[
{
“$set”: {
“fans”: {
“$add”: [
“$fans”,
{
“$cond”: {
“if”: {
“$ne”: [
“$watched.3”,
undefined
]
},
“then”: 1,
“else”: -2
}
}
]
}
}
},
{
“$unset”: “watched.3”
}
])

Hi @Better_Hub_Zone ,

This sounds like an application logic that should be done in a 2 statements if condition. Its overcomplex to push it to an aggregation.

Now if the watched was an array you can use $filter or $map to run through the arrays and filter out objects and save them later on in the pipeline.

Consider this.

Pavel