php[world] 2019 — 25 years of PHP

MongoCollection::distinct

(PECL mongo >=1.2.11)

MongoCollection::distinctRetrieve a list of distinct values for the given key across a collection

説明

public MongoCollection::distinct ( string $key [, array $query ] ) : array

The distinct command returns a list of distinct values for the given key across a collection.

パラメータ

key

The key to use.

query

An optional query parameters

返り値

Returns an array of distinct values, 失敗した場合に FALSE を返します

例1 MongoCollection::distinct() example

<?php
$m 
= new Mongo;
$db $m->selectDB("test");
$db->dropCollection("distinct");
$c $db->distinct;

$c->insert(array("stuff" => "bar""zip-code" => 10010));
$c->insert(array("stuff" => "foo""zip-code" => 10010));
$c->insert(array("stuff" => "bar""zip-code" => 99701), array("w" => 1));

$retval $c->distinct("zip-code");
var_dump($retval);

$retval $c->distinct("zip-code", array("stuff" => "foo"));
var_dump($retval);

$retval $c->distinct("zip-code", array("stuff" => "bar"));
var_dump($retval);

?>

上の例の出力は以下となります。

array(2) {
  [0]=>
  int(10010)
  [1]=>
  int(99701)
}
array(1) {
  [0]=>
  int(10010)
}
array(2) {
  [0]=>
  int(10010)
  [1]=>
  int(99701)
}

例2 MongoCollection::distinct() example on a embedded document

<?php
$c
->insert(array("user" => array("points" => 25)));
$c->insert(array("user" => array("points" => 31)));
$c->insert(array("user" => array("points" => 25)));

$retval $c->distinct("user.points");
var_dump($retval);

$retval $c->distinct("user.nonexisting");
var_dump($retval);
?>

上の例の出力は以下となります。

array(2) {
  [0]=>
  int(25)
  [1]=>
  int(31)
}
array(0) {
}
add a note add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top