When utilizing the $lookup
function, there is a possibility of it returning a value. In such cases, one must check the looked up value and see if a specific field is null; if it is, then it needs to be assigned a certain value.
An attempt was made with the following code:
{
$lookup:
{
from:
"item_templates",
localField:
"based_on",
foreignField:
"_id",
as:
"template"
}
},
{
$addFields:
{
"template.image.url":
{
$ifNull:
[
"$template.image.url",
"http://via.placeholder.com/400x700/d3d3d3/000000/?text=No%20Image&"
]
}
}
},
However, the above code does not seem to have any impact on the template.image.url
(which is an array of objects), instead returning an array with only one element set as null
.