/** * @param QueryInterface $query * @param ModelInterface $model * @return ModelListInterface * @throws NotSupportedFilterException */ public function find(QueryInterface $query, ModelInterface $model) { $queryArray = array(); foreach ($query->getFilters() as $filter) { if (!in_array(get_class($filter), self::$supportedFilters)) { throw new NotSupportedFilterException(sprintf('%s filter is not supported or unknown.', get_class($filter))); } $queryArray[$filter->getFieldName()] = $filter->getValue(); } $list = new ModelList(); $cursor = $this->collection->find($queryArray); if ($query->getLimit() !== null) { $cursor->limit($query->getLimit()); } if ($query->getOffset() !== null) { $cursor->skip($query->getOffset()); } foreach ($cursor as $doc) { unset($doc['_id']); /** @var ModelInterface $item */ $item = new $model(); $item->loadData($doc); if ($item instanceof SavableModelInterface) { $item->markAsStored(); } $list->addListItem($item); } return $list; }
/** * Logs a message. * * @param string $message Message * @param string $priority Message priority */ protected function doLog($message, $priority) { $document = array('message' => $message, 'priority' => $priority); $event = new sfEvent($this, 'mongodblog.pre_insert'); $this->dispatcher->filter($event, $document); $this->collection->insert($event->getReturnValue(), $this->options['save']); }
function sendMongo($steamId, $type) { //function mongoConnect($steamId,$type,$collection){ switch ($type) { case 1: //gets the player Summaries $fetch_pInfo = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?\t\tkey=238E8D6B70BF7499EE36312EF39F91AA&steamids={$steamId}"; break; case 2: //gets the players Game Library $fetch_pInfo = "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=238E8D6B70BF7499EE36312EF39F91AA&steamid={$steamId}&format=json"; break; /* case 3://gets the players achievements //steam api for players achievements. break; case 4://may have to get other stats, //will use this maybe for players over all steam rank. break; */ } //end switch data $jsonO = file_get_contents($fetch_pInfo); var_dump($jsonO); $connection = new MongoClient("mongodb://*****:*****@ds041643.mongolab.com:41643/steamdata"); $mongodb = $connection->selectDB('steamdata'); $collection = new MongoCollection($mongodb, 'gameLibrary'); //var_dump($connection); //var_dump($collection); $send = array(); $send["_id"] = $steamId; $send["info"] = $jsonO; $collection->insert($send); }
/** * @unused? * generalized mongo query * @param array $query * @param MongoCollection $collection * @return array * @throws modelException */ protected function findByArray($query = [], \MongoCollection $collection) { if (count($query) < 1) { throw new modelException('array is required'); } return iterator_to_array($collection->find($query)); }
/** * Zapisuję dokonane zmiany w bazie * * @return void */ public function save() { $aMods = []; // dla modelu z modułami if ($this->aModules !== null) { // obsługa modułów foreach ($this->aModules as $mModule) { if (!$mModule instanceof Module) { continue; } $aChanges = $mModule->beforeSave(); $aMods[] = $mModule; if (!empty($aChanges)) { $this->mergeChanges($aChanges); } } } $aChanges = $this->getChanges(); // zapisuję jeśli się coś zmieniło if (!empty($aChanges)) { $this->oCollection->update(['_id' => $this->getId(true)], $this->getChanges()); $this->clearChanges(); // post save foreach ($aMods as $oModule) { $oModule->afterSave(); } } }
public function deleteExceedingQuota($quota) { $threshold = 0.9 * $quota; $currentSize = $this->getPictureTotalSize(); $counter = 0; if ($currentSize > $threshold) { $toPurge = $currentSize - $threshold; // cleaning $cursor = $this->collection->find(['-class' => 'picture'], ['storageKey' => true, 'size' => true])->sort(['_id' => 1]); // starting from older pictures $sum = 0; foreach ($cursor as $item) { if ($sum < $toPurge) { $this->storage->remove($item['storageKey']); $this->collection->remove(['_id' => $item['_id']]); $sum += $item['size']; $counter++; } else { // when we have deleted enough old pictures to reach the exceeding size to purge break; } } } return $counter; }
function mkonereg($db, $colname, $grouparr) { $ops = array(); // $ops[] = ['$match' => $query]; // $ops[] = ['$sort' => ['year'=> -1, 'month'=> -1]]; $ops[] = ['$group' => $grouparr]; $option = ['allowDiskUse' => true]; //print_r($ops); $collection = new MongoCollection($db, $colname); echo "working on: {$colname} ... with"; $col2name = $colname . "_reg"; $col2 = new MongoCollection($db, $col2name); print_r($grouparr['_id']); makegrpIndex($db, $col2, $grouparr['_id']); //print_r($ops); try { $cursor = $collection->aggregateCursor($ops, $option); } catch (MongoException $e) { echo "error message: " . $e->getMessage() . "\n"; echo "error code: " . $e->getCode() . "\n"; exit(1); } //$results = $cursor['result']; foreach ($cursor as $result) { //print_r($result[_id]); $col2->insert($result['_id']); } }
/** * Write messages * * @return mixed|void */ public function write() { foreach ($this->messages as $message) { $message['log'] = $this->format($message); $this->collection->save($message); } }
/** * Write a message to the log. * * @param array $event Event data * @return void * @throws Zend\Log\Exception\RuntimeException */ protected function doWrite(array $event) { if (null === $this->mongoCollection) { throw new RuntimeException('MongoCollection must be defined'); } $this->mongoCollection->save($event, $this->saveOptions); }
/** * Zwraca liczbę elementów * * @return int */ public function count() { if ($this->iCount === null) { $this->iCount = $this->oColl->find($this->aQuery, $this->aFields)->count(true); } return $this->iCount; }
/** * Fetches the object pointed to by a reference * * @param mongodb $db - Database to use. * @param array $ref - Reference to fetch. * * @return array - Returns the document to which the reference refers * or NULL if the document does not exist (the reference is broken). */ public static function get(MongoDB $db, $ref) { $ref = (array) $ref; if (!isset($ref['$id']) || !isset($ref['$ref'])) { return; } $ns = $ref['$ref']; $id = $ref['$id']; $refdb = null; if (isset($ref['$db'])) { $refdb = $ref['$db']; } if (!is_string($ns)) { throw new MongoException('MongoDBRef::get: $ref field must be a string', 10); } if (isset($refdb)) { if (!is_string($refdb)) { throw new MongoException('MongoDBRef::get: $db field of $ref must be a string', 11); } if ($refdb != (string) $db) { $db = $db->_getClient()->{$refdb}; } } $collection = new MongoCollection($db, $ns); $query = ['_id' => $id]; return $collection->findOne($query); }
/** * Retrieve a log entry by his ID. * * @param string $id * * @return Log|null */ public function getLogById($id) { $log = $this->collection->findOne(array('_id' => array('$eq' => new \MongoId($id)))); if (!$log) { return null; } return $this->convertArrayToEntity($log); }
function dataBaseCall($collectionName) { $m = new MongoClient(); $db = $m->selectDB('SocialMedia'); $collection = new MongoCollection($db, $collectionName); $cursor = $collection->find(); return $cursor; }
/** * Retrieve the cached results of the api $request. * * @param RequestInterface $request A request for which the response may be cached. * * @return ResponseInterface|null */ public function get(RequestInterface $request) { $cached = $this->collection->findOne(['_id' => $request->getUrl()]); if ($cached === null) { return null; } return new Response($cached['httpCode'], $cached['headers'], $cached['body']); }
/** * {@inheritdoc} */ public function purge() { if ($this->collection instanceof \MongoDB\Collection) { $this->collection->deleteMany(['expiration' => ['$lte' => time()]]); } else { $this->collection->remove(['expiration' => ['$lte' => time()]], ['multiple' => true]); } return true; }
public function insertOrUpdate($product) { $result = $this->collection->findOne(['product_id' => $product['product_id']]); if ($result) { $this->update($product); } else { $this->insert($product); } }
public function testPermissionSave() { $perm_coll = new \MongoCollection($this->db, "auth_permission"); $p1 = Permission::create("test_1", "desc"); $this->adapter->permissionSave($p1); $this->assertNotEmpty($p1->permission_id); $fetched_perm = $perm_coll->findOne(array('_id' => $p1->permission_id)); $this->assertEquals($fetched_perm['_id']->{'$id'}, $p1->permission_id); }
/** */ public function add($tokenID) { $data = array(self::ADDRESS => $this->_encodeRemoteAddress(), self::TID => $tokenID); try { $this->_db->update($data, array('$set' => array_merge($data, array(self::TIMESTAMP => time()))), array('upsert' => true)); } catch (MongoException $e) { throw new Horde_Token_Exception($e); } }
/** * Connects to mongodb. * * @return \MongoCollection */ public function connect() { if (!$this->collection) { $client = new \MongoClient($this->server); $db = $client->selectDB($this->db); $this->collection = $db->selectCollection('mc_recharge_card_profiles'); $this->collection->ensureIndex(array('uniqueness' => 1), array('unique' => true)); } return $this->collection; }
/** * Connects to mongodb. * * @return \MongoCollection */ public function connect() { if (!$this->collection) { $client = new \MongoClient($this->server); $db = $client->selectDB($this->db); $this->collection = $db->selectCollection('mc_invitation_cards'); $this->collection->ensureIndex(array('code' => 1), array('unique' => true)); } return $this->collection; }
public function generateId(MongoCollection $collection) { $collection_name = $collection->getName(); $document = $this->collection->findAndModify(array('_id' => $collection_name), array('$inc' => array('_max_id' => 1)), null, array('new' => true)); if (!$document) { $this->collection->insert(array('_id' => $collection_name, '_max_id' => $this->autoIncrement)); return $this->generateId($collection); } return $document['_max_id']; }
/** * Connects to mongodb. * * @return \MongoCollection */ public function connect() { if (!$this->collection) { $client = new \MongoClient($this->server); $db = $client->selectDB($this->db); $this->collection = $db->selectCollection('mc_info_sms_subscription_low_balance_reminder_logs'); $this->collection->ensureIndex(array('mobile' => 1), array('unique' => true)); } return $this->collection; }
/** * Connects to mongodb. * * @return \MongoCollection */ public function connect() { if (!$this->collection) { $client = new \MongoClient($this->server); $db = $client->selectDB($this->db); $this->collection = $db->selectCollection('mc_info_sms_message_links'); $this->collection->ensureIndex(array('message' => 1), array('unique' => true)); } return $this->collection; }
public function gc($maxLifeTime) { $query = array('time' => array('$lt' => time() - $maxLifeTime)); try { $this->collection->remove($query); return true; } catch (MongoCursorException $ex) { return false; } }
/** * {@inheritdoc } */ public function set($key, $value, $ttl = null) { $tKey = $this->getKey($key); $tValue = $this->pack($value); if (!$ttl) { $ttl = $this->ttl; } $item = array('_id' => $tKey, 'value' => $tValue, 'ttl' => $this->getTtl($ttl)); $this->collection->update(array('_id' => $tKey), $item, array('upsert' => true)); }
public function testWrite() { $id = "somekey"; $value = "Foo"; $this->assertEquals(null, $this->collection->findOne()); $cache = SS_Cache::factory(self::$cache_name); /** @var ICacheFrontend$cache */ $cache->save($value, $id); $this->assertEquals(1, $this->collection->find()->count()); $this->assertEquals($value, $this->collection->find()->getNext()['content']); }
public function insertPost($title, $post, $tag, $date, $archieve) { $tagArray = explode(',', $tag); $doc = array("title" => $title, "post" => $post, "tag" => array($tagArray), "date" => new Date($date), "archieve" => $archieve); try { $col = new MongoCollection($this->con, COL_ARTICLES); $cur = $col->insert($doc); } catch (MongoCursorException $e) { return ADD_POST_FAILED; } }
/** * {@inheritdoc} */ public function read($sessionId) { $record = $this->collection->findOne(['_id' => $this->encodeSessionId($sessionId)]); if ($record == null) { return ''; } if ($record['expireat']->sec < time()) { return ''; } return serialize($record['data']); }
/** * Converts site.super[{user}] value from MongoId to string. * * @return void */ public function down() { $db = \DB::getMongoDB(); $sites = new MongoCollection($db, 'site'); $sitesCursor = $sites->find([], ['super' => true]); foreach ($sitesCursor as $site) { foreach ($site['super'] as $key => $supers) { $sites->update(['_id' => $site['_id']], ['$set' => ["super.{$key}.user" => (string) $supers['user']]], ['multiple' => true]); } } }
public function postUp(Schema $schema) { $helper = new UpgradeHelper($this->container); if ($helper->areProductsStoredInMongo()) { $database = $helper->getMongoInstance(); $versionCollection = new \MongoCollection($database, 'pim_versioning_version'); foreach ($this->movedEntities as $source => $target) { $result = $versionCollection->update(['resourceName' => $source], ['$set' => ['resourceName' => $target]], ['multiple' => true]); } } }