Julian Martinez Perez
Julian Martinez Perez

Reputation: 11

How to pass this MongoDb query to Laravel jenssegers/laravel-mongodb

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

Answers (2)

felipsmartins
felipsmartins

Reputation: 13549

Also, it could be solved through MongoDB raw queries, like this:

Product::raw()->find('your mongo raw statement')

Upvotes: 1

Julian Martinez Perez
Julian Martinez Perez

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

Related Questions