Пример #1
1
 /**
  * {@inheritdoc}
  */
 public function find(IdentifierInterface $id, $class, $table)
 {
     $query = new Query(array('uuid' => (string) $id), array('sort' => array('version' => -1), 'limit' => 1));
     $cursor = $this->conn->executeQuery($this->getNamespace($table), $query);
     $cursor->setTypeMap(array('document' => 'array'));
     $results = $cursor->toArray();
     if (count($results)) {
         $results = current($results);
         return $this->deserialize($class, (array) $results);
     }
     return null;
 }
Пример #2
0
 public function analyzeTableDecisions($table_id, $variant_id)
 {
     $table = $this->read($table_id);
     $mongo = new Manager(sprintf("mongodb://%s:%d", env('DB_HOST'), env('DB_PORT')));
     $query = new Query(['table._id' => new ObjectID($table_id), 'table.variant._id' => new ObjectId($variant_id), 'applications' => ApplicationableHelper::getApplicationId(), 'created_at' => ['$gte' => new UTCDatetime($table->updated_at->timestamp * 1000)]], ['projection' => ['rules' => 1]]);
     $decisions = $mongo->executeQuery(env('DB_DATABASE') . '.' . (new Decision())->getTable(), $query)->toArray();
     $map = [];
     if (($decisionsAmount = count($decisions)) > 0) {
         foreach ($decisions as $decision) {
             $rules = $decision->rules;
             foreach ($rules as $rule) {
                 if (!isset($rule->_id)) {
                     # ignore old decisions without Rule._id
                     continue;
                 }
                 $ruleIndex = strval($rule->_id);
                 foreach ($rule->conditions as $condition) {
                     $index = "{$ruleIndex}@" . strval($condition->_id);
                     if (!isset($map[$index])) {
                         $map[$index] = ['matched' => 0, 'requests' => 0];
                     }
                     if ($condition->matched === true) {
                         $map[$index]['matched']++;
                     }
                     $map[$index]['requests']++;
                 }
                 if (!isset($map[$ruleIndex])) {
                     $map[$ruleIndex] = ['matched' => 0, 'requests' => 0];
                 }
                 $map[$ruleIndex]['requests']++;
                 if ($rule->than === $rule->decision) {
                     $map[$ruleIndex]['matched']++;
                 }
             }
         }
     }
     $variant = $table->getVariantForCheck($variant_id);
     foreach ($variant->rules as $rule) {
         $ruleIndex = $rule->_id;
         foreach ($rule->conditions as $condition) {
             $index = "{$ruleIndex}@" . strval($condition->_id);
             if (array_key_exists($index, $map)) {
                 $condition->probability = round($map[$index]['matched'] / $map[$index]['requests'], 5);
             } else {
                 $condition->probability = null;
             }
             $condition->requests = array_key_exists($index, $map) ? $map[$index]['requests'] : 0;
             $rule->conditions()->associate($condition);
         }
         $ruleHasRequests = array_key_exists($ruleIndex, $map);
         $rule->probability = $ruleHasRequests ? round($map[$ruleIndex]['matched'] / $map[$ruleIndex]['requests'], 5) : 0;
         $rule->requests = $ruleHasRequests ? $map[$ruleIndex]['requests'] : 0;
         $variant->rules()->associate($rule);
     }
     $clonedTable = clone $table;
     $clonedTable->variants = [];
     $clonedTable->variants()->associate($variant);
     return $clonedTable;
 }
 /**
  * Performs a query.
  *
  * @param array $query The query to perform
  * @param array $options Optional array of query options
  * @return \MongoDB\Driver\Cursor The results cursor
  * @throws Swift_IoException
  */
 protected function find(array $query, array $options = array())
 {
     try {
         $q = new \MongoDB\Driver\Query($query, $options);
         return $this->manager->executeQuery($this->collection, $q, $this->rp);
     } catch (\Exception $e) {
         throw new Swift_IoException("Could not query for emails", 0, $e);
     }
 }
Пример #4
0
 /**
  * Execute mongodb query.
  *
  * @param Query  $query
  * @param string $namespace
  *
  * @return DomainEventStream|null
  */
 private function query(Query $query, $namespace)
 {
     $cursor = $this->conn->executeQuery($namespace, $query);
     $cursor->setTypeMap(array('document' => 'array'));
     $results = $cursor->toArray();
     $events = array();
     if (count($results)) {
         foreach ($results as $record) {
             $events[] = $this->deserialize((array) $record);
         }
         return new DomainEventStream($events);
     }
     return null;
 }
Пример #5
0
<?php

use MongoDB\Driver\Manager;
use MongoDB\Driver\Query;
use MongoDB\Driver\Command;
$dsn = "mongodb://localhost:27017/users";
$options = ['username' => 'admin', 'password' => 'public'];
$manager = new Manager($dsn, $options);
$query = new Query(['city' => '北京市'], ['limit' => 5]);
$count = ['count' => 'usergeo', 'query' => ['city' => '北京市']];
$aggregate = ['aggregate' => 'usergeo', 'pipeline' => [['$match' => ['city' => '北京市']], ['$group' => ['_id' => null, 'avg_time' => ['$avg' => '$created_at']]]]];
try {
    /* Specify the full namespace as the first argument, followed by the query
     * object and an optional read preference. MongoDB\Driver\Cursor is returned
     * success; otherwise, an exception is thrown. */
    $cursor = $manager->executeQuery("users.usergeo", $query);
    // Iterate over all matched documents
    $array = iterator_to_array($cursor);
    foreach ($array as $key => $value) {
        $value = (array) $value;
        var_dump($value['city']);
        var_dump($value['province']);
    }
    // count command
    $command = new Command($count);
    $result = $manager->executeCommand("users", $command);
    $response = $result->toArray();
    var_dump($response);
    // aggregate
    $command = new Command($aggregate);
    $result = $manager->executeCommand("users", $command);