本文整理汇总了PHP中MongoCollection::distinct方法的典型用法代码示例。如果您正苦于以下问题:PHP MongoCollection::distinct方法的具体用法?PHP MongoCollection::distinct怎么用?PHP MongoCollection::distinct使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoCollection
的用法示例。
在下文中一共展示了MongoCollection::distinct方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getDistinct
/**
* 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);
}
示例2: getDistinct
/**
* 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);
}
示例3: listScopes
/**
*/
public function listScopes()
{
try {
return $this->_db->distinct(self::SCOPE);
} catch (MongoException $e) {
throw new Horde_Prefs_Exception($e);
}
}
示例4: getFresh
/**
* 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);
}
//.........这里部分代码省略.........
示例5: getFresh
/**
* 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.
//.........这里部分代码省略.........
示例6: Mongo
<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">
示例7: MongoClient
$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);
示例8: distinct
/**
* 根据指定字段
*
* @param string $key
* @param array $query
*/
public function distinct($key, $query = null)
{
$query = $this->appendQuery($query);
return parent::distinct($key, $query);
}