$addfields with $cond is not working

I had it working like this:

db.getCollection("Test").aggregate([
{
    $addFields:{
        assignee2:{
            $cond:{
                if:{$gt:['$assignee', null]},
                then:'$assignee',
                else:{}
            }
        }        
    }
}
])

You could also use $ifNull:

db.getCollection("Test").aggregate([
{
    $addFields:{
        assignee2:{
            $ifNull:[
                '$assignee',
                {}
            ]
        }        
    }
}
])

There is a similar post here:

2 Likes