Mongo 相当于 SQL 的 SELECT DISTINCT?

发布于 2024-10-21 01:12:59 字数 305 浏览 8 评论 0原文

根据标题,PHP Mongo 相当于 SQL 中的类似内容:

SELECT DISTINCT(field) FROM table WHERE someCondition = 1

我读过 这张表,但我不知道如何将 db.users.distinct('last_name') 映射到 PHP 中。

As per the title, what would be the PHP Mongo equivalent of something like this in SQL:

SELECT DISTINCT(field) FROM table WHERE someCondition = 1

I've read looked at this table but I don't see how to map db.users.distinct('last_name') into PHP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反话 2024-10-28 01:12:59

只需发出 命令 并设置 distinct 键即可。

看一下文档中的以下示例:

查找某个键的所有不同值。

<?php

$people = $db->people;

$people->insert(array("name" => "Joe", "age" => 4));
$people->insert(array("name" => "Sally", "age" => 22));
$people->insert(array("name" => "Dave", "age" => 22));
$people->insert(array("name" => "Molly", "age" => 87));

$ages = $db->command(array("distinct" => "people", "key" => "age"));

foreach ($ages['values'] as $age) {
    echo "$age\n";
}

?>

上面的示例将输出类似以下内容的内容:

4
22
87

Just issue a command and set the distinct key.

Take a look at the following example from the docs:

Finding all of the distinct values for a key.

<?php

$people = $db->people;

$people->insert(array("name" => "Joe", "age" => 4));
$people->insert(array("name" => "Sally", "age" => 22));
$people->insert(array("name" => "Dave", "age" => 22));
$people->insert(array("name" => "Molly", "age" => 87));

$ages = $db->command(array("distinct" => "people", "key" => "age"));

foreach ($ages['values'] as $age) {
    echo "$age\n";
}

?>

The above example will output something similar to:

4
22
87
自演自醉 2024-10-28 01:12:59

如果需要添加 where 子句,请使用以下语法:

$ages = $db->command(array(
    "distinct" => "people", 
    "key" => "age",
    "query" => array("someField" => "someValue")));

If you need to add a where clause, use the following syntax:

$ages = $db->command(array(
    "distinct" => "people", 
    "key" => "age",
    "query" => array("someField" => "someValue")));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文