I got quite an annoying problem, and I can't seem to find any solution for my specific case. In my mongo agggregation pipeline, I'd like to add a field, and depending on the existence of another field's subfield, I'd like to assign the value of that other field's subfield, or 1 if that other field's subfield does not exist.
Here is what I tried:
pipelineStages.push(
{$addFields:
{field:
{$cond: [
{$ne: ["$otherField.subField", null]},
"$otherField.subField", 1]
}
}
}
);
However, it only works fine for the case that the field exists. In the other, that new field is not added to the pipeline at all. Any ideas what I'm doing wrong?