本文整理汇总了PHP中MongoCollection::count方法的典型用法代码示例。如果您正苦于以下问题:PHP MongoCollection::count方法的具体用法?PHP MongoCollection::count怎么用?PHP MongoCollection::count使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoCollection
的用法示例。
在下文中一共展示了MongoCollection::count方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: has
/**
* {@inheritdoc }
*/
public function has($key)
{
$tKey = $this->getKey($key);
$tNow = $this->getTtl();
return $this->collection->count(array('_id' => $tKey, 'ttl' => array('$gte' => $tNow))) > 0;
}
示例2: count
/**
* count.
*/
public function count($query = array(), $limit = 0, $skip = 0)
{
$this->time->start();
$return = parent::count($query, $limit, $skip);
$time = $this->time->stop();
$this->log(array('type' => 'count', 'query' => $query, 'limit' => $limit, 'skip' => $skip, 'time' => $time));
return $return;
}
示例3: count
/**
* Counts the number of documents in the model's collection
* @param array $options Options for the count query.
* @param array $limit Specifies an upper limit to the number returned
* @param array $skip Specifies a number of results to skip before starting the count.
* @return int Returns the number of documents matching the query.
*/
public function count($query = array(), $limit = 0, $skip = 0)
{
if ($query === null) {
$query = array();
} else {
$query = self::_sanitize($query);
}
return $this->collection->count($query, $limit, $skip);
}
示例4: count
/**
* Gets back the number of documents viewed, if nothing viewed then it's zero
*
* @param Array $criteria the condition for gretting how many back
* @return int
*/
public function count($criteria = array())
{
ValidatorsUtil::isNullOrEmpty($this->_mongoCollection, "Mongo collection isn't valid, have you set a collection?");
$documentCount = 0;
if (count($criteria) != 0) {
$documentCount = $this->_mongoCollection->count($criteria);
} else {
$documentCount = $this->_mongoCollection->count();
}
return $documentCount;
}
示例5: numberOfRecipients
/**
*/
public function numberOfRecipients($hours, $user = false)
{
$query = array(self::SUCCESS => 1, self::TS => array('$gt' => time() - $hours * 3600));
if ($user) {
$query[self::WHO] = $GLOBALS['registry']->getAuth();
}
try {
return $this->_db->count($query);
} catch (MongoException $e) {
return 0;
}
}
示例6: up
/**
* Removes documents from the client collection that have
* an lrs_id that does not exist in the lrs collection.
*
* @return void
*/
public function up()
{
$db = \DB::getMongoDB();
$clients = new MongoCollection($db, 'client');
$lrss = new MongoCollection($db, 'lrs');
$clientCursor = $clients->find([], ['lrs_id' => true]);
foreach ($clientCursor as $client) {
$count = $lrss->count(['_id' => $client['lrs_id']]);
if ($count == 0) {
$clients->remove(['_id' => $client['_id']]);
}
}
}
示例7: count
/**
* @param array|stdClass $query
* @param null $limit
* @param null $skip
* @return int
*/
public function count($query = [], $limit = null, $skip = null)
{
$options = $limit;
if (is_array($options) && isset($options['cache']) && $options['cache'] === false) {
return parent::count($query);
}
$hash = $this->hash($query);
$result = $this->cache->get($hash);
if (!$result) {
$result = parent::count($query);
$this->cache->set($hash, $result, $this->getCacheTime($options));
}
return $result;
}
示例8: testSave
public function testSave()
{
$this->object->save(array('x' => 1));
$a = $this->object->findOne();
$id1 = $a['_id'];
$a['x'] = 2;
$this->object->save($a);
$id2 = $a['_id'];
$this->assertEquals($id1, $id2);
$a['y'] = 3;
$this->object->save($a);
$this->assertEquals($this->object->count(), 1);
$a = $this->object->findOne();
$this->assertEquals($a['x'], 2);
}
示例9: count
/**
* Count queue messages.
*
* @param array $query in same format as \MongoCollection::find() where top level fields do not contain operators.
* Lower level fields can however. eg: valid {a: {$gt: 1}, "b.c": 3}, invalid {$and: [{...}, {...}]}
* @param bool|null $qr query a running message or not or all
*
* @return int the count
*
* @throws \InvalidArgumentException $qr was not null and not a bool
* @throws \InvalidArgumentException key in $query was not a string
*/
public function count(array $query, $qr = null)
{
if ($qr !== null && !is_bool($qr)) {
throw new \InvalidArgumentException('$qr was not null and not a bool');
}
$totalQuery = array();
if ($qr !== null) {
$totalQuery['qr'] = $qr;
}
foreach ($query as $key => $value) {
if (!is_string($key)) {
throw new \InvalidArgumentException('key in $query was not a string');
}
$totalQuery["payload.{$key}"] = $value;
}
return $this->_collection->count($totalQuery);
}
示例10: testGarbageCollection
public function testGarbageCollection()
{
$saveHandler = new MongoDB($this->mongo, $this->options);
$this->assertTrue($saveHandler->open('savepath', 'sessionname'));
$data = array('foo' => 'bar');
$this->assertTrue($saveHandler->write(123, serialize($data)));
$this->assertTrue($saveHandler->write(456, serialize($data)));
$this->assertEquals(2, $this->mongoCollection->count());
$saveHandler->gc(5);
$this->assertEquals(2, $this->mongoCollection->count());
/* Note: MongoDate uses micro-second precision, so even a maximum
* lifetime of zero would not match records that were just inserted.
* Use a negative number instead.
*/
$saveHandler->gc(-1);
$this->assertEquals(0, $this->mongoCollection->count());
}
示例11: read
/**
*/
public function read($id)
{
/* Check for session existence. Unfortunately needed because
* we need findAndModify() for its atomicity for locking, but this
* atomicity means we can't tell the difference between a
* non-existent session and a locked session. */
$exists = $this->_db->count(array(self::SID => $id));
$exist_check = false;
$i = 0;
/* Set a maximum unlocking time, to prevent runaway PHP processes. */
$max = ini_get('max_execution_time') * 10;
while (true) {
$data = array(self::LOCK => time(), self::SID => $id);
/* This call will either create the session if it doesn't exist,
* or will update the current session and lock it if not already
* locked. If a session exists, and is locked, $res will contain
* an empty set and we need to sleep and wait for lock to be
* removed. */
$res = $this->_db->findAndModify(array(self::SID => $id, self::LOCK => array('$exists' => $exist_check)), array($data), array(self::DATA => true), array('update' => array('$set' => $data), 'upsert' => !$exists));
if (!$exists || isset($res[self::DATA])) {
break;
}
/* After a second, check the timestamp to determine if this is
* a stale session. This can prevent long waits on a busted PHP
* process. */
if ($i == 10) {
$res = $this->_db->findOne(array(self::SID => $id), array(self::LOCK => true));
$max = isset($res[self::LOCK]) ? (time() - $res[self::LOCK]) * 10 : $i;
}
if (++$i >= $max) {
$exist_check = true;
} else {
/* Sleep for 0.1 second before trying again. */
usleep(100000);
}
}
$this->_locked = $id;
return isset($res[self::DATA]) ? $res[self::DATA]->bin : '';
}
示例12: size
/**
* Get the number of indexed terms
* @return integer Number of indexed terms
*/
public function size()
{
return $this->index->count();
}
示例13: update
/**
* 更新指定范围的数据
*
* @param array $criteria
* @param array $object
* @param array $options
*/
public function update($criteria, $object, array $options = NULL)
{
if (!is_array($criteria)) {
throw new \Exception('$criteria is array');
}
if (empty($object)) {
throw new \Exception('$object is empty');
}
$keys = array_keys($object);
foreach ($keys as $key) {
// $key = strtolower($key);
if (!in_array($key, $this->_updateHaystack, true)) {
throw new \Exception('$key must contain ' . join(',', $this->_updateHaystack));
}
}
$default = array('upsert' => self::upsert, 'multiple' => self::multiple, 'fsync' => self::fsync);
$options = $options === NULL ? $default : array_merge($default, $options);
$criteria = $this->appendQuery($criteria);
array_unset_recursive($object, array('_id', '__CREATE_TIME__', '__MODIFY_TIME__', '__REMOVED__'));
if (parent::count($criteria) == 0) {
if (isset($options['upsert']) && $options['upsert']) {
$criteria = $this->addSharedKeyToQuery($criteria);
parent::update($criteria, array('$set' => array('__CREATE_TIME__' => new \MongoDate(), '__MODIFY_TIME__' => new \MongoDate(), '__REMOVED__' => false)), $options);
}
} else {
unset($options['upsert']);
parent::update($criteria, array('$set' => array('__MODIFY_TIME__' => new \MongoDate())), $options);
}
return parent::update($criteria, $object, $options);
}
示例14: _call
protected function _call($command, array $arguments = array(), array $values = NULL)
{
$start = microtime(true);
$this->_connected or $this->connect();
extract($arguments);
if (isset($collection_name)) {
$c = new \MongoCollection($this->_db, $collection_name);
}
switch ($command) {
case 'ensure_index':
$r = $c->ensureIndex($keys, $options);
break;
case 'create_collection':
$r = $this->_db->createCollection($name, $capped, $size, $max);
break;
case 'drop_collection':
$r = $this->_db->dropCollection($name);
break;
case 'command':
$r = $this->_db->command($values);
break;
case 'execute':
$r = $this->_db->execute($code, $args);
break;
case 'batch_insert':
$r = $c->batchInsert($values);
break;
case 'count':
$r = $c->count($query);
break;
case 'find_one':
$r = $c->findOne($query, $fields);
break;
case 'find':
$r = $c->find($query, $fields);
break;
case 'group':
$r = $c->group($keys, $initial, $reduce, $condition);
break;
case 'update':
$r = $c->update($criteria, $values, $options);
break;
case 'insert':
$r = $c->insert($values, $options);
return $values;
break;
case 'remove':
$r = $c->remove($criteria, $options);
break;
case 'save':
$r = $c->save($values, $options);
break;
case 'get_file':
$r = $this->gridFS()->findOne($criteria);
break;
case 'get_files':
$r = $this->gridFS()->find($query, $fields);
break;
case 'set_file_bytes':
$r = $this->gridFS()->storeBytes($bytes, $extra, $options);
break;
case 'set_file':
$r = $this->gridFS()->storeFile($filename, $extra, $options);
break;
case 'remove_file':
$r = $this->gridFS()->remove($criteria, $options);
break;
}
$this->log($command, $start, $arguments);
return $r;
}
示例15: getCount
/**
*
* @return int
*/
public function getCount()
{
return $this->cacheGet('statistics/count', function () {
return $this->collection->count();
});
}