Efficiently counting members of a collection

I’d expect count() to be fast and efficient. In the mongo shell, this is true.

In C++, I wrote this:

mongocxx::client client;
mongocxx::database db = MongoInit(client, ...);

bsoncxx::document::view empty_filter;
mongocxx::collection collection = db[collection_name];
int64_t collection_count = 

This sometime takes a very long time. Am I doing something wrong?