In an aggregation stage I want to set several fields in case a condition results to true like this:
{
$set: {
$cond: {
if: {
$ne: ["$isLastReceiveTime", null]
},
then: {
complete: true,
completedTime: "$$NOW"
},
else: {
complete: "$$REMOVE",
completedTime: "$$REMOVE"
}
}
}
}
But that does not work since $set expects a field name and not an operator like $cond …
Currently I’m doing it this way:
{
complete: {
$cond: {
if: {
$ne: ["$isLastReceiveTime", null]
},
then: true,
else: "$$REMOVE"
}
},
completedTime: {
$cond: {
if: {
$ne: ["$isLastReceiveTime", null]
},
then: "$$NOW",
else: "$$REMOVE"
}
}
}
But this is really not nice and maintainable …