0

I have Documents in my mongoDB collection. I need to fetch the count of data associated to the perticular user according to the date. Date is in MongoDB\BSON\UTCDateTime Object. For ex. I need this week's data

Im using jenssegers/mongodb packages for this, an tried using DB:: methods but failed. Tried with carbon too..

Here is the format stored in mongodb collection

{
    "_id": "02ce6ec6-50b1-4c3d-b961-32a3f452c9da",
    "rid": "WW3EvLk9TdwXwfK6AYTyEx5t4PfDWx9xfP",
    "msg": "Hello",
    "ts": {
        "$date": {
            "$numberLong": "1558085847819"
        }
    },
    "u": {
        "_id": "YTyEx5t4PfDWx9xfP",
        "username": "9738084090",
        "name": "Suhas"
    },
    "_updatedAt": {
        "$date": {
            "$numberLong": "1558085847832"
        }
    },
    "mentions": [],
    "channels": []
}

Simple like there are multiple entry for ex YTyEx5t4PfDWx9xfP, I need to get the total number of entries for that id for the given date.

As of now I'm fetching the data and in loop im counting it This is what I've tried

$weekStart = strtotime(Carbon::now()->startOfWeek()->format('d-m-Y'));
$weekEnd = time();
$videos = DB::connection('mongodb')->collection('collection_name')->>where('file.type','like','video%')->get();
$weekCounts['video'] = 0;
foreach ($videos as $video)
        {
            if ((string)$video['ts']/1000 >= $weekStart &&(string)$video['ts']/1000 <= $weekEnd)
            {
                $weekCounts['video']++;
            }
        }
return $weekCounts;

0 Answers0