0

I have a mongo database which has data like this

{ "_id" : ObjectId("5e0202d3fae94619c020c390"), "sys" : "2019-12-19", "time" : ISODate("2019-12-19T11:28:30.454Z"), "time_rcvd" : ISODate("2019-12-19T11:28:30.454Z"), "msg" : "", "syslog_fac" : 23, "syslog_sever" : 6, "syslog_tag" : "11:", "procid" : "11", "pid" : "-", "level" : "INFO", "logtype" : "Traffic LOGS", "date" : "2019-12-19", "Module" : "01SECLOG", "Desc" : "IPVer", "Severity_Level" : "6", "Protocol" : "tcp", "SourceIP" : "10.10.2.101", "Country" : "172.217.169.194", "DestinationIP" : "172.217.169.194", "SourcePort" : "39554", "DestinationPort" : "443", "SourceNatIP" : "192.168.101.2", "SourceNatPort" : "4128", "BeginTime" : "1576754847", "EndTime" : "1576754894", "SendPkts" : "59", "SendBytes" : "11041", "RcvPkts" : "81", "RcvBytes" : "8095", "SourceVpnID" : "0", "DestinationVpnID" : "0", "SourceZone" : "guest", "DestinationZone" : "untrust", "PolicyName" : "guest_to_untrust", "CloseReason" : "tcp-rst", "ApplicationName" : "Google_Service.", "VSys" : "", "User" : "", "Profile" : "", "Type" : "", "Category" : "", "SubCategory" : "", "Page" : "", "Host" : "", "Referer" : "", "Item" : "", "Action" : "" }

I am applying this query to get data from it, using laravel aggregate

$data = Logss::raw(function($collection)use ($topLength,$start)
     {
   return $collection->aggregate([
        [

        '$group'    => [
            '_id'   => '$ApplicationName',

            'count' => [
                '$sum'  => 1
            ]
        ]
    ],
    ['$sort' => ['count' => -1]],

    ['$limit'=> $topLength]
]);
}); 

I can get data without a problem, but I decided to make a filter of this data depending on the date that user select like

{ last_hour , today , yesterday , this_week , last_week , this_month , last_month , this_year };

when user select today, for example, I will use carbon to get today's date

$today = new Carbon('today');

and it is return

2019-12-25 00:00:00

but I don't know how to do that and retrieve only today's records,
I used this code but return nothings

 $data = Logss::raw(function($collection)use ($topLength)
{
return $collection->aggregate([
 [

        '$group'    => [
            '_id'   => '$ApplicationName',

            'count' => [
                '$sum'  => 1
            ]
        ]
    ],
    ['$sort' => ['count' => -1]],
 [ '$match' => [ 'time' =>['$gt'=>' ISODate("2017-10-01")' ]]],
    ['$limit'=> $topLength]
]);
}); 
stack over
  • 89
  • 1
  • 7

2 Answers2

0

Try

    $dt = Carbon::create(2012, 1, 31, 0);
    echo $dt->addHours(24);                  // 2012-02-04 00:00:00
    echo $dt->addHour();                     // 2012-02-04 01:00:00
    echo $dt->subHour();                     // 2012-02-04 00:00:00
    echo $dt->subHours(24);                  // 2012-02-03 00:00:00

Document

story ks
  • 855
  • 1
  • 16
  • 39
0

Here what I tried. Hope to help you out :)

 db.getCollection('collectionName').find({
    created: {
        $gte: '2019-10-28 00:00:00',
        $lt:  '2019-10-29 10:22:10'
    }
})
PHP Ninja
  • 1,055
  • 2
  • 9
  • 28