Designing Empty Fields in Mangod Aggregation
I am using aggregation to find the result from MongoDB using the following query.
$y=2000;
$project['pud_year']=array('$year'=>'$pud');
$conditions=array('aggregate'=>array(
array('$match'=>array('pid'=>$project_id)),
array('$project'=>$project),
array('$match'=>array('pud'=>$y)),
));
$this->paginate=array('limit' => 50,'conditions'=>$conditions);
$test=$this->paginate();
But I am getting an error can't convert from BSON type EOO to Date
.
After searching, I found that in the aggregation, the predicted date field should not be empty anywhere in the full collection. But in my case pud
it is optional. When I remove the pud projection from the request, it works fine.
If I do not project MongoDate to $year
, then I cannot get results for 2000 only.
Please help me find a solution for this.
source to share
You can use $ ifNull, I believe. This will give you the option to return the default date when $ pud is NULL. It works well with aggregation. Please check it out - http://docs.mongodb.org/manual/reference/operator/aggregation/ifNull/
source to share