本文介绍了Mongo是否等同于SQL的SELECT DISTINCT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照标题,相当于SQL的PHP​​ Mongo是这样的:

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

我已经看过此表,但我看不到如何将db.users.distinct('last_name')映射到PHP.

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

推荐答案

只需发出命令并设置distinct键.

看看以下来自文档的示例:

Take a look at the following example from the docs:

查找键的所有不同值.

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

这篇关于Mongo是否等同于SQL的SELECT DISTINCT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 20:05