doctrine/mongodb

timeout/maxTimeMS settings for count()

pastk-fishpond opened this issue · 3 comments

Current implementation of \Doctrine\MongoDB\Collection::count() doesn't support options passing to http://php.net/manual/en/mongocollection.count.php

Hence its impossible to set custom timeout for count() queries except by using \MongoCursor::$timeout - but that's deprecated.

After deprecation of \MongoCursor::$timeout the whole timeout thing became very inconsistent - its suggested to use $cursor->timeout() but that works only for queries that return Cursor. I.e. for map-reduce queries an array is returned instead and hence ->timeout() thing won't work. In that case the only option is to set timeout during query building, e.g. $queryBuilder->getQuery(['timeout' => 60000])->execute(). I think there should be a single and straightforward way to pass options / set timeout etc. for any kind of query - that's what abstraction layer is for.

Hence its impossible to set custom timeout for count() queries except by using \MongoCursor::$timeout - but that's deprecated.

Indeed, Collection::count() still takes separate $limit and $skip parameters, which are deprecated since 1.6.0 (I missed porting this change over to doctrine/mongodb). We'll need to add support for an $options array as the second parameter, while still allowing $limit and $skip for BC (as the driver does).

I'll note that performing a count through query builder also calls Collection::count() directly, which means no timeout is supported.

AFAIK, the only way to do a count with a socket timeout would be setting the property on a Cursor and then using Cursor::count(). This would then call MongoCursor::count(), which does copy the cursor's server-side (i.e. $maxTimeMS) and client-side (i.e. socketTimeoutMS) timeouts (see: here).

for map-reduce queries an array is returned instead and hence ->timeout() thing won't work

If you're using the Collection::mapReduce() method in this library, you are able to use the maxTimeMS command option (which goes in the command document itself) as well as the socketTimeoutMS and timeout client options (received by MongoDB::command()'s second argument). For BC, our mapReduce() method takes a single options array, which we then split into command and client/driver options (see: here).

@pastk-fishpond: Please take a look at #224 and let me know if that addresses your needs.

@jmikola that should fix it, thanks!