I have more than 50 thousand records in mongodb collections. To avoid foreach loop in laravel I am using aggregate in my query.
$start = new MongoDate(strtotime("2015-10-01 00:00:00"));
$result = Pms::raw(function ($collection) use($start){
return $collection->aggregate(array(
array( '$project' => array( 'EventTS' => 1, 'MainsPower' => 1, '_id' => 0) ),
array(
'$unwind' => array(
'path' => '$MainsPower',
'includeArrayIndex' => "arrayIndex",
'preserveNullAndEmptyArrays' => true
)
),
array(
'$match' => array(
'EventTS' => array(
'$gte' => $start
)
)
),
array(
'$project' => array(
'MainsPower' => 1,
'timestamp' => array(
'$add' => array(
'$EventTS',
array( '$multiply' => array( 60000, '$arrayIndex' ) )
)
)
)
)
));
})->toArray();
I need to compare date with a collection, but if I use below code then my result set returns empty.
array(
'$match' => array(
'EventTS' => array(
'$gte' => $start
)
)
)
what's the right way to compare date in mongodb collection inside PHP with Laravel 5.2.
Attached is the Sample Document
{
"_id" : ObjectId("576165f58d8b8f39458b456a"),
"EventTS" : ISODate("2000-05-11T05:30:00.000+0000"),
"PanelID" : "A01000",
"MainsPower" : [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
NumberInt(147),
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
],
}
{
"_id" : ObjectId("576165f58d8b8f39458b456b"),
"EventTS" : ISODate("2016-06-08T18:30:00.000+0000"),
"PanelID" : "A01604",
"MainsPower" : [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
]
}