Handling unwind for the non existing embedded document [duplicate]
This question already has an answer here: $unwind empty array 2 answers I am performing aggregation on employees table to fetch some hostel details with projection like $query = ['_id' => new MongoDB\BSON\ObjectID($this->EmployeeId)]; $pipeline = array( ['$match' => $query], [ '$addFields'=> [ 'assigned_master_keys'=> [ '$cond'=> [ 'if'=> [ '$ne'=> [ [ '$type'=> '$assigned_master_keys' ], 'array' ] ], 'then'=> [], 'else'=> '$assigned_master_keys' ] ] ]], ['$unwind'=> '$assigned_master_keys'], ['$lookup' => [ 'from' => 'HostelTbl', 'let' => [ 'hostelid' => '$assigned_master_keys.hostel_id' ],