Reputation: 11
I need to run this in laravel.
db.blog.aggregate([
{
$project :
{
likes_count: {$size: { "$ifNull": [ "$like", [] ] } }
}
},
{
$sort: {"likes_count":-1}
}
])
How to implement this using jenssegers/laravel-mongodb package? Thank you
Upvotes: 0
Views: 3369
Reputation: 13549
Also, it could be solved through MongoDB raw queries, like this:
Product::raw()->find('your mongo raw statement')
Upvotes: 1
Reputation: 11
I had solved this:
$cursor = \DB::collection('blog')->raw()->aggregate([
['$project' => ['_id' => 0,
'likes' => array('$size' => array('$like')),
]
],['$sort' => array('likes' => -1)]
]);
Upvotes: 1