nested $ifNull not working

i’m trying to implement a nested $ifNull structure to handle various potential data fields.

module_data: {
$ifNull: [
“$renewal”,
{
$ifNull: [
“$type_of_alert”,
{
$ifNull: [
“$department_type”,
{
$ifNull: [
“$costomer_type”,
{
$ifNull: [
“$company_size”,
{
$ifNull: [
“$naigp_code”,
{
$ifNull: [
“$naisc_code”,
{
$ifNull: [
“$relationship_type”,
{
$ifNull: [
“$drill_type”,
{
$ifNull: [
“$office”,
{
$ifNull: [
“$customer”,
{
$ifNull: [
“$facility”,
{
$ifNull: [
“$linked_facility”,
{
$ifNull: [
“$superadmin”,
{
$ifNull: [
“$department”,
{
$ifNull: [
“$linked_department”,
{
$ifNull: [
“$customer_document_type”,
{
$ifNull: [
“$superadmin_role”,
{
$ifNull: [
“$camera_info”,
{
$ifNull: [
“$document”,
{
$ifNull: [
“$customer_payment”,
“”
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}

but last one nested operation not working

Hello,

At present the code is difficult to read.
Please improve its format.
You may get guidance from the link below.

Thanks
WeDoTheBest4You

1 Like

If you look in documentation, you can simple use one $ifNull and put fields in array one by one

db.inventory.aggregate(
   [
      {
         $project: {
            item: 1,
            value: { $ifNull: [ "$description", "$quantity", "Unspecified" ] }
         }
      }
   ]
)

Source: https://www.mongodb.com/docs/manual/reference/operator/aggregation/ifNull/#multiple-input-expressions

Hello,

Thank you for the update.

Nested $ifNull works fine.
Please see the test case below.

In the test case, while the key data1 is based on implicitly nested $ifNull, data2 is based on an explicitly nested $ifNull structure. Both produce the same results. If the results are the same, would we still need to pursue the explicit $ifNull structure ? If you have any other findings, please share it.

However if you still would like to proceed on your way with the explicit $ifNull structure, as you know, please try to build it in some way by which the syntactical issues if any can be caught instantly.

As we all can guess easily:

  1. Start from scratch
  2. Build the first conditional
  3. Do test it immediately, to check for syntactical issues.
  4. Then build the first or next nested conditional, repeat step 3 until it is done.

This is just the one of the ways you can guess. The whole point is to catch the syntactical issues instantly.

t.find({},{_id:0});
[ { a: 'a' }, { b: 'b' }, { c: 'c' }, { } ]

t.aggregate([{
    $project: {
        _id: 0,
        data1: {
            $ifNull: ['$a', '$b', '$c', 'Not found']
        },
        data2: {
            $ifNull: ['$a', {
                $ifNull: ['$b', {
                    $ifNull: ['$c', {
                        $ifNull: ['$d', 'Not found']
                    }]
                }]
            }]
        }
    }
}]);

[
  { data1: 'a', data2: 'a' },
  { data1: 'b', data2: 'b' },
  { data1: 'c', data2: 'c' },
  { data1: 'Not found', data2: 'Not found' }
]

Thanks
WeDoTheBest4You