PHPDaemon\Clients\Mongo\Pool::findCount PHP Метод

findCount() публичный Метод

Counts objects in collection
public findCount ( array $p, callable $cb ) : void
$p array Hash of properties (offset, limit, opts, where, col)
$cb callable Callback called when response received
Результат void
    public function findCount($p, $cb)
    {
        if (!isset($p['offset'])) {
            $p['offset'] = 0;
        }
        if (!isset($p['limit'])) {
            $p['limit'] = -1;
        }
        if (!isset($p['opts'])) {
            $p['opts'] = 0;
        }
        if (!isset($p['where'])) {
            $p['where'] = [];
        }
        if (mb_orig_strpos($p['col'], '.') === false) {
            $p['col'] = $this->dbname . '.' . $p['col'];
        }
        $e = explode('.', $p['col'], 2);
        $query = ['count' => $e[1], 'query' => $p['where'], 'fields' => ['_id' => 1]];
        if (isset($p[$k = 'rp'])) {
            $v = $p[$k];
            if (is_string($v)) {
                $v = ['mode' => $v];
            }
            $query['$readPreference'] = $v;
        }
        if (is_string($p['where'])) {
            $query['where'] = new \MongoCode($p['where']);
        } elseif (is_object($p['where']) || sizeof($p['where'])) {
            $query['query'] = $p['where'];
        }
        $cb = CallbackWrapper::wrap($cb);
        if ($this->safeMode) {
            static::safeModeEnc($query);
        }
        try {
            $this->request(self::OP_QUERY, pack('V', $p['opts']) . $e[0] . '.$cmd' . "" . pack('VV', $p['offset'], $p['limit']) . bson_encode($query) . (isset($p['fields']) ? bson_encode($p['fields']) : ''), true, null, function ($conn, $reqId = null) use($p, $cb) {
                if (!$conn) {
                    !$cb || $cb(['$err' => 'Connection error.']);
                    return;
                }
                $conn->requests[$reqId] = [$p['col'], $cb, true];
            });
        } catch (\MongoException $e) {
            Daemon::log('MongoClient exception: ' . $e->getMessage() . ': ' . $e->getTraceAsString());
            if ($cb !== null) {
                $cb(['$err' => $e->getMessage(), '$query' => $query, '$fields' => isset($p['fields']) ? $p['fields'] : null]);
            }
        }
    }