How to SELECT COUNT(column_name) in laravel mongodb

1k Views Asked by At

How can we retrieve the size of an array in the laravel-mongodb's get() function?

Update: Ultimately, I want to get the size of the array column for each qualified document

Example, for each document, I have an array of members data. Therefore, I would like to know for each document, how many members are there.

From Laravel's perspective, I could just load the entire member list for all qualified records and use count() to get the array size in blade template. But I wanna avoid that because it will cause loading issue if the member list for each record is huge.

1

There are 1 best solutions below

2
On

You can do Model::where(column, condition)->count(); if Model extends Jenssegers\Mongodb\Eloquent\Model

Or DB::collection(collectionName)->where(column, condition)->count();

You can find out more here: https://github.com/jenssegers/laravel-mongodb#readme