Laravel Mongodb Raw mongo date request

I am working on laravel 5.1 and using jessenger mongodb package. I use a raw query to retrieve the data, but I'm confused about how to use a date with this, as it currently returns a 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?

+4
source share
2 answers

Laravel has a really nice Carbon date processing package that you could use with your queries. If you want to receive records from the beginning of today, use the Carbon property or use the to get the previous date midnight . startOfDay() Carbon::yesterday()->endOfDay()

, :

$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
                )
            )
        )   
    ));
});

, MongoDate,

$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
                    )
                )
            )   
        ));
    });
0

:

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

, .

0

Source: https://habr.com/ru/post/1628247/


All Articles