distinct() public method

Retrieve a list of distinct values for the given key across a collection
public distinct ( string $key, array $query = [] ) : array | boolean
$key string The key to use.
$query array An optional query parameters
return array | boolean Returns an array of distinct values, or FALSE on failure
Esempio n. 1
0
 /**
  * Retrieve a list of distinct values for the given key across a collection.
  *
  * @param string $selector field selector
  * @param array|callable|\Sokil\Mongo\Expression $expression expression to search documents
  * @return array distinct values
  */
 public function getDistinct($selector, $expression = null)
 {
     if ($expression) {
         return $this->_mongoCollection->distinct($selector, Expression::convertToArray($expression));
     }
     return $this->_mongoCollection->distinct($selector);
 }
 /**
  * Retrieve a list of distinct values for the given key across a collection.
  *
  * @param string $selector field selector
  * @param array|callable|\Sokil\Mongo\Expression $expression expression to search documents
  * @return array distinct values
  */
 public function getDistinct($selector, $expression = null)
 {
     if ($expression) {
         return $this->_mongoCollection->distinct($selector, self::mixedToArray($expression, '\\Sokil\\Mongo\\Expression'));
     }
     return $this->_mongoCollection->distinct($selector);
 }
Esempio n. 3
0
 /**
  */
 public function listScopes()
 {
     try {
         return $this->_db->distinct(self::SCOPE);
     } catch (MongoException $e) {
         throw new Horde_Prefs_Exception($e);
     }
 }
Esempio n. 4
0
 /**
  * Execute the query as a fresh "select" statement.
  *
  * @param  array  $columns
  * @return array|static[]
  */
 public function getFresh($columns = array())
 {
     // If no columns have been specified for the select statement, we will set them
     // here to either the passed columns, or the standard default of retrieving
     // all of the columns on the table using the "wildcard" column character.
     if (is_null($this->columns)) {
         $this->columns = $columns;
     }
     // Drop all columns if * is present, MongoDB does not work this way.
     if (in_array('*', $this->columns)) {
         $this->columns = array();
     }
     // Compile wheres
     $wheres = $this->compileWheres();
     // Use MongoDB's aggregation framework when using grouping or aggregation functions.
     if ($this->groups or $this->aggregate) {
         $group = array();
         // Add grouping columns to the $group part of the aggregation pipeline.
         if ($this->groups) {
             foreach ($this->groups as $column) {
                 $group['_id'][$column] = '$' . $column;
                 // When grouping, also add the $last operator to each grouped field,
                 // this mimics MySQL's behaviour a bit.
                 $group[$column] = array('$last' => '$' . $column);
             }
         } else {
             // If we don't use grouping, set the _id to null to prepare the pipeline for
             // other aggregation functions.
             $group['_id'] = null;
         }
         // Add aggregation functions to the $group part of the aggregation pipeline,
         // these may override previous aggregations.
         if ($this->aggregate) {
             $function = $this->aggregate['function'];
             foreach ($this->aggregate['columns'] as $column) {
                 // Translate count into sum.
                 if ($function == 'count') {
                     $group['aggregate'] = array('$sum' => 1);
                 } else {
                     $group['aggregate'] = array('$' . $function => '$' . $column);
                 }
             }
         } else {
             foreach ($this->columns as $column) {
                 $key = str_replace('.', '_', $column);
                 $group[$key] = array('$last' => '$' . $column);
             }
         }
         // Build the aggregation pipeline.
         $pipeline = array();
         if ($wheres) {
             $pipeline[] = array('$match' => $wheres);
         }
         $pipeline[] = array('$group' => $group);
         // Apply order and limit
         if ($this->orders) {
             $pipeline[] = array('$sort' => $this->orders);
         }
         if ($this->offset) {
             $pipeline[] = array('$skip' => $this->offset);
         }
         if ($this->limit) {
             $pipeline[] = array('$limit' => $this->limit);
         }
         if ($this->projections) {
             $pipeline[] = array('$project' => $this->projections);
         }
         // Execute aggregation
         $results = $this->collection->aggregate($pipeline);
         // Return results
         return $results['result'];
     } else {
         if ($this->distinct) {
             // Return distinct results directly
             $column = isset($this->columns[0]) ? $this->columns[0] : '_id';
             // Execute distinct
             $result = $this->collection->distinct($column, $wheres);
             return $result;
         } else {
             $columns = array();
             // Convert select columns to simple projections.
             foreach ($this->columns as $column) {
                 $columns[$column] = true;
             }
             // Add custom projections.
             if ($this->projections) {
                 $columns = array_merge($columns, $this->projections);
             }
             // Execute query and get MongoCursor
             $cursor = $this->collection->find($wheres, $columns);
             // Apply order, offset and limit
             if ($this->timeout) {
                 $cursor->timeout($this->timeout);
             }
             if ($this->orders) {
                 $cursor->sort($this->orders);
             }
             if ($this->offset) {
                 $cursor->skip($this->offset);
             }
             if ($this->limit) {
                 $cursor->limit($this->limit);
             }
             // Return results as an array with numeric keys
             return iterator_to_array($cursor, false);
         }
     }
 }
Esempio n. 5
0
 /**
  * Execute the query as a fresh "select" statement.
  *
  * @param  array  $columns
  * @return array|static[]
  */
 public function getFresh($columns = [])
 {
     // If no columns have been specified for the select statement, we will set them
     // here to either the passed columns, or the standard default of retrieving
     // all of the columns on the table using the "wildcard" column character.
     if (is_null($this->columns)) {
         $this->columns = $columns;
     }
     // Drop all columns if * is present, MongoDB does not work this way.
     if (in_array('*', $this->columns)) {
         $this->columns = [];
     }
     // Compile wheres
     $wheres = $this->compileWheres();
     // Use MongoDB's aggregation framework when using grouping or aggregation functions.
     if ($this->groups or $this->aggregate or $this->paginating) {
         $group = [];
         // Add grouping columns to the $group part of the aggregation pipeline.
         if ($this->groups) {
             foreach ($this->groups as $column) {
                 $group['_id'][$column] = '$' . $column;
                 // When grouping, also add the $last operator to each grouped field,
                 // this mimics MySQL's behaviour a bit.
                 $group[$column] = ['$last' => '$' . $column];
             }
             // Do the same for other columns that are selected.
             foreach ($this->columns as $column) {
                 $key = str_replace('.', '_', $column);
                 $group[$key] = ['$last' => '$' . $column];
             }
         }
         // Add aggregation functions to the $group part of the aggregation pipeline,
         // these may override previous aggregations.
         if ($this->aggregate) {
             $function = $this->aggregate['function'];
             foreach ($this->aggregate['columns'] as $column) {
                 // Translate count into sum.
                 if ($function == 'count') {
                     $group['aggregate'] = ['$sum' => 1];
                 } else {
                     $group['aggregate'] = ['$' . $function => '$' . $column];
                 }
             }
         }
         // When using pagination, we limit the number of returned columns
         // by adding a projection.
         if ($this->paginating) {
             foreach ($this->columns as $column) {
                 $this->projections[$column] = 1;
             }
         }
         // The _id field is mandatory when using grouping.
         if ($group and empty($group['_id'])) {
             $group['_id'] = null;
         }
         // Build the aggregation pipeline.
         $pipeline = [];
         if ($wheres) {
             $pipeline[] = ['$match' => $wheres];
         }
         if ($group) {
             $pipeline[] = ['$group' => $group];
         }
         // Apply order and limit
         if ($this->orders) {
             $pipeline[] = ['$sort' => $this->orders];
         }
         if ($this->offset) {
             $pipeline[] = ['$skip' => $this->offset];
         }
         if ($this->limit) {
             $pipeline[] = ['$limit' => $this->limit];
         }
         if ($this->projections) {
             $pipeline[] = ['$project' => $this->projections];
         }
         $options = ['typeMap' => ['root' => 'array', 'document' => 'array']];
         // Execute aggregation
         $results = iterator_to_array($this->collection->aggregate($pipeline, $options));
         // Return results
         return $results;
     } elseif ($this->distinct) {
         // Return distinct results directly
         $column = isset($this->columns[0]) ? $this->columns[0] : '_id';
         // Execute distinct
         if ($wheres) {
             $result = $this->collection->distinct($column, $wheres);
         } else {
             $result = $this->collection->distinct($column);
         }
         return $result;
     } else {
         $columns = [];
         // Convert select columns to simple projections.
         foreach ($this->columns as $column) {
             $columns[$column] = true;
         }
         // Add custom projections.
         if ($this->projections) {
             $columns = array_merge($columns, $this->projections);
         }
         $options = [];
         // Apply order, offset, limit and projection
         if ($this->timeout) {
             $options['maxTimeMS'] = $this->timeout;
         }
         if ($this->orders) {
             $options['sort'] = $this->orders;
         }
         if ($this->offset) {
             $options['skip'] = $this->offset;
         }
         if ($this->limit) {
             $options['limit'] = $this->limit;
         }
         if ($columns) {
             $options['projection'] = $columns;
         }
         // if ($this->hint)    $cursor->hint($this->hint);
         // Fix for legacy support, converts the results to arrays instead of objects.
         $options['typeMap'] = ['root' => 'array', 'document' => 'array'];
         // Execute query and get MongoCursor
         $cursor = $this->collection->find($wheres, $options);
         // Return results as an array with numeric keys
         return iterator_to_array($cursor, false);
     }
 }
Esempio n. 6
0
<body>
  <nav class="navbar navbar-default navbar-fixed-top text-center">
    <div class="container" style="float: center">
      <img src="af_logotype.png" style="float: left; width:68px;height:68px;">
      <h1 id="header-text" style="float: center">Event Calendar</h1>
    </div>
  </nav>

  <div class="container-fluid text-center"> 
    <?php 
// select a database and collection
$m = new Mongo();
$db = $m->selectDB('eventDB');
$events = new MongoCollection($db, 'events');
// list all unique locations in DB
$list_locations = $events->distinct("location");
$list_tags = $events->distinct("tags");
$list_types = $events->distinct("type");
sort($list_tags);
sort($list_locations);
sort($list_types);
?>

    <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">
Esempio n. 7
0
$m = new MongoClient();
$db = $m->selectDB("biglegcarry");
$car_col = new MongoCollection($db, "car");
$makers = array();
array_push($makers, "Any");
$models = array();
array_push($models, "Any");
$colors = array();
array_push($colors, "Any");
$cars = array();
// Get maker and model from url
$maker = $_GET["maker"];
$model = $_GET["model"];
$color = $_GET["color"];
// Find makers from mongodb
$cursor = $car_col->distinct("maker");
foreach ($cursor as $obj) {
    array_push($makers, $obj);
}
if ($maker != "Any") {
    // Find models from mongodb
    $cursor = $car_col->distinct("model", array("maker" => $maker));
    foreach ($cursor as $obj) {
        array_push($models, $obj);
    }
}
if ($maker != "Any") {
    if ($model != "Any") {
        $cursor = $car_col->distinct("color", array("maker" => $maker, "model" => $model));
        foreach ($cursor as $obj) {
            array_push($colors, $obj);
Esempio n. 8
0
 /**
  * 根据指定字段
  *
  * @param string $key            
  * @param array $query            
  */
 public function distinct($key, $query = null)
 {
     $query = $this->appendQuery($query);
     return parent::distinct($key, $query);
 }