Run Raw Query using mongoDB Jenssegers Laravel
This is my first day with Mongodb (Laravel Jensseger), and I was lucky enough to figure it out. So, I wanted to query my Messages model:
// This is the SQL version$unreadMessageCount = Message::selectRaw('from_id as sender_id, count(from_id) as messages_count') ->where('to_id', auth()->id()) ->where('read', false) ->groupBy('from') ->get();// This is the Mongo version. The solution was figuring out the 'aggregate' concept in Mongo$unreadMessageCount = Message::raw(function($collection){ return $collection->aggregate([ [ '$match' => [ 'to_id' => auth()->id() ] ], [ '$group' => [ '_id' => '$from_id', 'messages_count' => [ '$sum' => 1 ] ] ] ]);});
Hope this helps.
In the Laravel Jenssegers Library in a section Raw Expressions is described want to create a raw expression.The raw expression accept an array object of condition. In your example the find method isn't right.