/** * {@inheritdoc } */ public function has($key) { $tKey = $this->getKey($key); $tNow = $this->getTtl(); return $this->collection->count(array('_id' => $tKey, 'ttl' => array('$gte' => $tNow))) > 0; }
/** * 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; }
/** * 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); }
/** * 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; }
/** */ 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; } }
/** * 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']]); } } }
/** * @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; }
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); }
/** * 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); }
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()); }
/** */ 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 : ''; }
/** * Get the number of indexed terms * @return integer Number of indexed terms */ public function size() { return $this->index->count(); }
/** * 更新指定范围的数据 * * @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); }
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; }
/** * * @return int */ public function getCount() { return $this->cacheGet('statistics/count', function () { return $this->collection->count(); }); }
/** @proxy */ public function count(array $query = array(), $limit = 0, $skip = 0) { return $this->mongoCollection->count($query, $limit, $skip); }
<div class="container fluid"> <div class="row"> <div class="fixed col-sm-3 text-left"> <div class="panel panel-default"> <div class="panel-heading"><h4>Filter your search</h4></div> <div class="panel-body"> <div class="row"> <!--Dropdown for city of the events--> <div class="col-sm-8"> <div class="dropdown"> <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown"><span>Location </span> <span class="caret"></span></button> <ul class="dropdown-menu scrollable-menu"> <?php for ($index = 0; $index <= sizeof($list_locations) - 1; $index++) { $location_count = $events->count(array('location' => $list_locations[$index])); echo '<div class="checkboxdiv">'; echo '<label><input type="checkbox" value="" class="cBox" id="Location: '; echo $list_locations[$index]; echo '"> '; echo $list_locations[$index] . ' (' . $location_count . ')'; echo '</label>'; echo '</div>'; } ?> </ul> </div> </div> <!-- Dropdown menu for tag of the events --> <div class="col-sm-8">
public function testSkipCache() { $this->assertEquals(2, $this->collection->count(['foo' => 1])); $this->collection->insert(['foo' => 1]); $this->assertEquals(3, $this->collection->count(['foo' => 1], ['cache' => false])); }
$db = $connect->selectDB('ICCv1'); // $collection = new MongoCollection($db, 'idatabase_collection_5372fccc49961910538b4570'); $collection = new MongoCollection($db, 'idatabase_collection_5372fccc49961910538b4570'); $collection->ensureIndex(array('location' => '2d')); // $cursor = $collection->find(array( // '$and' => array( // 0 => array( // '__REMOVED__' => FALSE // ), // 1 => array( // '$and' => array( // 0 => array( // 'location' => array( // '$near' => array( // 0 => 123, // 1 => 123 // ), // '$maxDistance' => 0.089992800575954 // ) // ) // ) // ) // ) // )); $cursor = $collection->count(array('location' => array('$near' => array(0 => 123, 1 => 123), '$maxDistance' => 0.089992800575954), '__REMOVED__' => false)); var_dump($cursor); //var_dump($cursor->count()); //var_dump(iterator_to_array($cursor, false)); } catch (Exception $e) { var_dump($e); }
public function count($collectionName) { #gelen $tableName isimli collectiona bağlantı gerçekleştirelim. $Collection = new MongoCollection($this->mongodb, $collectionName); //yeni kayıt ekleme try { $dataCount = $Collection->count(); } catch (MongoCursorException $e) { die('İnsert yaparken teknik bir sorunla karşılaşıldı ' . $e->getMessage()); } return $dataCount; }
public function count($criteria = []) { return $this->collection->count($criteria); }
/** * Returns the number of entities available. * @return integer the number of entities */ public function count() { return $this->collection->count(); }
function __construct($host = 'localhost', $port = 27017) { $this->connection = new \MongoClient('mongodb://' . $host . ':' . $port); $this->documents = $this->connection->search->documents; $this->size = $this->documents->count(); }