Handling unwnd 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 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!!!
mongodb mongodb-query aggregation-framework mongodb-php php-mongodb
marked as duplicate by Veeram
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.
add a comment |
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!!!
mongodb mongodb-query aggregation-framework mongodb-php php-mongodb
marked as duplicate by Veeram
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
add a comment |
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!!!
mongodb mongodb-query aggregation-framework mongodb-php php-mongodb
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
mongodb mongodb-query aggregation-framework mongodb-php php-mongodb
asked Nov 11 at 17:11
Nisa Nisa
186
186
marked as duplicate by Veeram
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
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
Use $unwind
with preserveNullAndEmptyArrays
option
['$unwind'=> ['path' => '$assigned_master_keys', 'preserveNullAndEmptyArrays' => true ]]
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use $unwind
with preserveNullAndEmptyArrays
option
['$unwind'=> ['path' => '$assigned_master_keys', 'preserveNullAndEmptyArrays' => true ]]
add a comment |
Use $unwind
with preserveNullAndEmptyArrays
option
['$unwind'=> ['path' => '$assigned_master_keys', 'preserveNullAndEmptyArrays' => true ]]
add a comment |
Use $unwind
with preserveNullAndEmptyArrays
option
['$unwind'=> ['path' => '$assigned_master_keys', 'preserveNullAndEmptyArrays' => true ]]
Use $unwind
with preserveNullAndEmptyArrays
option
['$unwind'=> ['path' => '$assigned_master_keys', 'preserveNullAndEmptyArrays' => true ]]
answered Nov 14 at 16:45
Anthony Winzlet
13.7k41239
13.7k41239
add a comment |
add a comment |
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