MongoCollection::count PHP Method

count() public method

Counts the number of documents in this collection
public count ( array | stdClass $query = [], array $options = [] ) : integer
$query array | stdClass
$options array
return integer Returns the number of documents matching the query.
    public function count($query = [], $options = [])
    {
        try {
            // Handle legacy mode - limit and skip as second and third parameters, respectively
            if (!is_array($options)) {
                $limit = $options;
                $options = [];
                if ($limit !== null) {
                    $options['limit'] = (int) $limit;
                }
                if (func_num_args() > 2) {
                    $options['skip'] = (int) func_get_args()[2];
                }
            }
            return $this->collection->count(TypeConverter::fromLegacy($query), $options);
        } catch (\MongoDB\Driver\Exception\Exception $e) {
            throw ExceptionConverter::toLegacy($e);
        }
    }

Usage Example

Beispiel #1
1
 /**
  * {@inheritdoc }
  */
 public function has($key)
 {
     $tKey = $this->getKey($key);
     $tNow = $this->getTtl();
     return $this->collection->count(array('_id' => $tKey, 'ttl' => array('$gte' => $tNow))) > 0;
 }
All Usage Examples Of MongoCollection::count