Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Mongodb Raw mongo query with date

I am working on laravel 5.1 and using jessenger mongodb package. I am using raw query to fetch data but i am confused how to use date with that as currently it is returning null result.

 $resultSet = DB::connection('mongodb')->collection('wc_mycollection')->raw(function ($collection){
            return $collection->aggregate([
                [
                    '$match'=>[
                        'created_at'=>[
                            '$gte' => Previous day midnight,
                            '$lt' => Current Time
                        ]
                    ]
                ],
                [
                    '$group' => [
                        '_id' => '$some_id',

                    ]
                ]
            ]);
        });

what should i do?

like image 953
Sameer Sheikh Avatar asked Nov 09 '22 20:11

Sameer Sheikh


2 Answers

Try this option:

'$match'=>[
      'created_at'=>[
             '$gte' => new Date("2016-10-02T00:00:00.000Z"),
             '$lt' => new Date("2016-11-02T00:00:00.000Z")
      ]
]

See, if that works.

like image 109
Gaurav Dave Avatar answered Nov 14 '22 22:11

Gaurav Dave


There is a really nice date handling package in laravel called Carbon that you could use with your queries. If you want to get records from start of today, use Carbon's startOfDay() property or to get the previous date midnight, use Carbon::yesterday()->endOfDay().

Joining all of this together you can construct your pipeline as:

$previousDayMidnight = Carbon::yesterday()->endOfDay(); // or $startOfToday = Carbon::now()->startOfDay()
$currentTime = Carbon::now();
$result = DB::collection('wc_mycollection')->raw(function($collection)
{
    return $collection->aggregate(array(
        array(
            '$match' => array(
                'created_at' => array(
                    '$gte' => $previousDayMidnight, // or $startOfToday
                    '$lt' => $currentDateTime
                )
            )
        ),
        array(
            '$group' => array(
                '_id' => '$some_id',
                'count' => array(
                    '$sum' => 1
                )
            )
        )   
    ));
});

Another approach would be to natively use MongoDate objects, you could try

$start = new MongoDate(strtotime(date('Y-m-d H:i:s', '-1 days')));
$end = new MongoDate(strtotime(date('Y-m-d H:i:s')));
$result = DB::collection('wc_mycollection')->raw(function($collection)
    {
        return $collection->aggregate(array(
            array(
                '$match' => array(
                    'created_at' => array(
                        '$gte' => $start, 
                        '$lt' => $end
                    )
                )
            ),
            array(
                '$group' => array(
                    '_id' => '$some_id',
                    'count' => array(
                        '$sum' => 1
                    )
                )
            )   
        ));
    });
like image 23
chridam Avatar answered Nov 14 '22 23:11

chridam