Deprecated: Optional parameter $keys declared before required parameter $cms_id is implicitly treated as a required parameter in /home/www/dev/work/class/blog/CmsKey.php on line 75

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\php::$title is deprecated in /home/www/dev/work/website/lvesu/class/controller/blog/php.php on line 28

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\php::$outlink is deprecated in /home/www/dev/work/website/lvesu/template/blog/cms/php.manual.tpl on line 2

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\php::$status is deprecated in /home/www/dev/work/website/lvesu/template/blog/index.head.php on line 2
PHP - Manual: MongoCollection::distinct - 互联网笔记

略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: MongoCollection::distinct

2025-05-18

MongoCollection::distinct

(PECL mongo >=1.2.11)

MongoCollection::distinct获取集合里指定键的不同值的列表。

说明

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

distinct 命令返回集合里给定键不同值的列表。

参数

key

要使用的键。

query

一个可选的查询参数

返回值

返回不同值的数组, 或者在失败时返回 FALSE

范例

Example #1 MongoCollection::distinct() 例子

<?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)
}

Example #2 内嵌文档的 MongoCollection::distinct() 例子

<?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.

官方地址:https://www.php.net/manual/en/mongocollection.distinct.php

冷却塔厂家 广告
中文GPT4.0无需注册 广告
北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3