Handling unwnd for the non existing embedded document [duplicate]











up vote
1
down vote

favorite













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 MongoDBBSONObjectID($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' ],
'pipeline' => [
['$match' => [ '$expr'=> [ '$eq' => [ '$_id', '$$hostelid' ]]]],
['$project' => [ 'Name'=> 1, 'MasterKeyDetails' => 1, '_id'=> 1]]
],
'as' => 'assigned_master_keys.hostel_id'
]],
);

$this->collection = $this->db->EmployeesTbl;
$cursor = $this->collection->aggregate($pipeline);


The above code works fine for those employees where embedded document with name "assigned_master_keys" exists but not for those where "assigned_master_keys" does not exists. The page breaks. If I remove unwind from the above code, the page does not break but it does not fetch hostel data either.



Please help!!!










share|improve this question













marked as duplicate by Veeram mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 at 16:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • preserveNullAndEmptyArrays should be the answer. Try ['$unwind'=> ['path' => '$assigned_master_keys', 'preserveNullAndEmptyArrays' => true ]]
    – Anthony Winzlet
    Nov 11 at 17:14








  • 1




    Thankyou for great solution
    – Nisa Nisa
    Nov 11 at 17:21















up vote
1
down vote

favorite













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 MongoDBBSONObjectID($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' ],
'pipeline' => [
['$match' => [ '$expr'=> [ '$eq' => [ '$_id', '$$hostelid' ]]]],
['$project' => [ 'Name'=> 1, 'MasterKeyDetails' => 1, '_id'=> 1]]
],
'as' => 'assigned_master_keys.hostel_id'
]],
);

$this->collection = $this->db->EmployeesTbl;
$cursor = $this->collection->aggregate($pipeline);


The above code works fine for those employees where embedded document with name "assigned_master_keys" exists but not for those where "assigned_master_keys" does not exists. The page breaks. If I remove unwind from the above code, the page does not break but it does not fetch hostel data either.



Please help!!!










share|improve this question













marked as duplicate by Veeram mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 at 16:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • preserveNullAndEmptyArrays should be the answer. Try ['$unwind'=> ['path' => '$assigned_master_keys', 'preserveNullAndEmptyArrays' => true ]]
    – Anthony Winzlet
    Nov 11 at 17:14








  • 1




    Thankyou for great solution
    – Nisa Nisa
    Nov 11 at 17:21













up vote
1
down vote

favorite









up vote
1
down vote

favorite












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 MongoDBBSONObjectID($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' ],
'pipeline' => [
['$match' => [ '$expr'=> [ '$eq' => [ '$_id', '$$hostelid' ]]]],
['$project' => [ 'Name'=> 1, 'MasterKeyDetails' => 1, '_id'=> 1]]
],
'as' => 'assigned_master_keys.hostel_id'
]],
);

$this->collection = $this->db->EmployeesTbl;
$cursor = $this->collection->aggregate($pipeline);


The above code works fine for those employees where embedded document with name "assigned_master_keys" exists but not for those where "assigned_master_keys" does not exists. The page breaks. If I remove unwind from the above code, the page does not break but it does not fetch hostel data either.



Please help!!!










share|improve this question














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 MongoDBBSONObjectID($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' ],
'pipeline' => [
['$match' => [ '$expr'=> [ '$eq' => [ '$_id', '$$hostelid' ]]]],
['$project' => [ 'Name'=> 1, 'MasterKeyDetails' => 1, '_id'=> 1]]
],
'as' => 'assigned_master_keys.hostel_id'
]],
);

$this->collection = $this->db->EmployeesTbl;
$cursor = $this->collection->aggregate($pipeline);


The above code works fine for those employees where embedded document with name "assigned_master_keys" exists but not for those where "assigned_master_keys" does not exists. The page breaks. If I remove unwind from the above code, the page does not break but it does not fetch hostel data either.



Please help!!!





This question already has an answer here:




  • $unwind empty array

    2 answers








mongodb mongodb-query aggregation-framework mongodb-php php-mongodb






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 17:11









Nisa Nisa

186




186




marked as duplicate by Veeram mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 at 16:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Veeram mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 at 16:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • preserveNullAndEmptyArrays should be the answer. Try ['$unwind'=> ['path' => '$assigned_master_keys', 'preserveNullAndEmptyArrays' => true ]]
    – Anthony Winzlet
    Nov 11 at 17:14








  • 1




    Thankyou for great solution
    – Nisa Nisa
    Nov 11 at 17:21


















  • preserveNullAndEmptyArrays should be the answer. Try ['$unwind'=> ['path' => '$assigned_master_keys', 'preserveNullAndEmptyArrays' => true ]]
    – Anthony Winzlet
    Nov 11 at 17:14








  • 1




    Thankyou for great solution
    – Nisa Nisa
    Nov 11 at 17:21
















preserveNullAndEmptyArrays should be the answer. Try ['$unwind'=> ['path' => '$assigned_master_keys', 'preserveNullAndEmptyArrays' => true ]]
– Anthony Winzlet
Nov 11 at 17:14






preserveNullAndEmptyArrays should be the answer. Try ['$unwind'=> ['path' => '$assigned_master_keys', 'preserveNullAndEmptyArrays' => true ]]
– Anthony Winzlet
Nov 11 at 17:14






1




1




Thankyou for great solution
– Nisa Nisa
Nov 11 at 17:21




Thankyou for great solution
– Nisa Nisa
Nov 11 at 17:21












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Use $unwind with preserveNullAndEmptyArrays option



['$unwind'=> ['path' => '$assigned_master_keys', 'preserveNullAndEmptyArrays' => true ]]





share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Use $unwind with preserveNullAndEmptyArrays option



    ['$unwind'=> ['path' => '$assigned_master_keys', 'preserveNullAndEmptyArrays' => true ]]





    share|improve this answer

























      up vote
      1
      down vote



      accepted










      Use $unwind with preserveNullAndEmptyArrays option



      ['$unwind'=> ['path' => '$assigned_master_keys', 'preserveNullAndEmptyArrays' => true ]]





      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Use $unwind with preserveNullAndEmptyArrays option



        ['$unwind'=> ['path' => '$assigned_master_keys', 'preserveNullAndEmptyArrays' => true ]]





        share|improve this answer












        Use $unwind with preserveNullAndEmptyArrays option



        ['$unwind'=> ['path' => '$assigned_master_keys', 'preserveNullAndEmptyArrays' => true ]]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 at 16:45









        Anthony Winzlet

        12.8k41039




        12.8k41039















            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma