Fluinc
Fluinc

Reputation: 491

PHP MongoDB Collection Size

I am using the PHP MongoDB Library v1.6 and I want to be able to get the size of a specific collection. I have looked at the documentation of the library and didn't see anything that would return the size. Is this possible with this library? If so could someone provide some insight on how to achieve this.

This is how I would do it from the mongo shell:

db.collection.dataSize()

Upvotes: 2

Views: 319

Answers (1)

Fluinc
Fluinc

Reputation: 491

Using aggregations with the $collStats stage you can get the collection size.

Example:

$pipeline = [
    [
        '$collStats' => [
            'storageStats' =>
                ['scale' => 1024]
        ]
    ]
];
$storageStats = $collection->aggregate($pipeline)->toArray();
var_dump($storageStats);

Upvotes: 2

Related Questions