Previously, Im using Mysql for database. Once data keep growing, i'm tried to migrate to MongoDB. Im using jenssegers/laravel-mongodb
just for analytics part. I found that it is difficult to fetch the data if i want to groupby per month, day, year and count it.
Mysql query
$query = Model::query()
->selectRaw('api_module, MONTH(created_at) as trans, Count(*) as counter');
->whereBetween('created_at', [$start, $end])
->whereNotNull('api_module')
->groupBy('trans', 'api_module')
->orderBy('api_module')
->get(['api_module', 'tran', 'counter']);
Data
{
"_id" : ObjectId("5e61f67d31b9ad636154396f"),
"fingerprint" : "hy9mZ959ovg42898pncA1VPhbqr6gzdQNKDbSVBix8IajZ9yY",
"device_id" : 16,
"device_uuid" : "aaaab-123123",
"api_url" : "http://example.test:8000/api/news",
"api_id" : null,
"api_method" : "GET",
"ip_address" : "127.0.0.1",
"user_agent" : "PostmanRuntime/7.22.0",
"api_module" : "news",
"api_module_studly" : "News",
"origin" : 1,
"request_header" : "ABC",
"request_body" : "ABCD",
"created_at" : "2020-03-06 15:06:37",
"updated_at" : "2020-03-06 15:06:37"
}
I have no idea to convert it into aggregate json form if i want to use Model::raw()
. It would be much appreciated if you could help to convert it from mysql to moloquent