mongodb php获取字段唯一值

发布于 2024-10-05 01:02:50 字数 655 浏览 8 评论 0原文

我正在尝试从 mongodb 集合中的“type”字段获取唯一值的列表。下面的示例文档:

{
       "_id" : ...,
       "type" : "report",
       "tasks" : ...
}
{
       "_id" : ...,
       "type" : "research",
       "tasks" : ...
}
{
       "_id" : ...,
       "type" : "memo",
       "tasks" : ...
}
{
       "_id" : ...,
       "type" : "memo",
       "tasks" : ...
}
{
       "_id" : ...,
       "type" : "report",
       "tasks" : ...
}
{
       "_id" : ...,
       "type" : "report",
       "tasks" : ...
}

我正在寻找按频率排序的文档类型字段中的唯一类型,因此:

["report", "memo", "research"]

执行此操作的最佳方法是什么?希望我可以通过查询 mongo 来做到这一点,而不是下载整个集合......

I'm trying to get a list of unique values from the 'type' field from my mongodb collection. Example documents below:

{
       "_id" : ...,
       "type" : "report",
       "tasks" : ...
}
{
       "_id" : ...,
       "type" : "research",
       "tasks" : ...
}
{
       "_id" : ...,
       "type" : "memo",
       "tasks" : ...
}
{
       "_id" : ...,
       "type" : "memo",
       "tasks" : ...
}
{
       "_id" : ...,
       "type" : "report",
       "tasks" : ...
}
{
       "_id" : ...,
       "type" : "report",
       "tasks" : ...
}

I'm looking for, ordered by frequency, the unique types that are in the type field of the documents, so:

["report", "memo", "research"]

What's the best way to do this? Hopefully I can do this by querying with mongo and not downloading the entire collection...

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

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

发布评论

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

评论(2

深空失忆 2024-10-12 01:02:51

在标准 SQL DBMS 上,这将通过以下查询来完成:

SELECT type, count(*) as ct FROM table GROUP BY type ORDER BY ct;

在 mongodb 上,这将使用 group 函数来完成,尽管它稍微复杂一些:

db.collection.group(
           {key: { "type":true},
            reduce: function(obj,prev) { prev.count += 1; },
            initial: { count: 0 }
            });

这里我要求数据库返回键“type”的值(因此“true”),对于每个值,给定的reduce函数将用于聚合找到的记录。在这里,我只是更新每条记录出现的次数。如果您运行此查询,您将得到如下结果:

[
    {
        "type" : "report",
        "count" : 5
    },
    {
        "type" : "memo",
        "count" : 15
    }
    {
        "type" : "research",
        "count" : 3
    }

]

您会注意到这不是有序的;甚至 mongodb 文档也说最简单的订购方法是在客户端进行。

相关文档位于此处

On a standard SQL DBMS this would be done with the following query:

SELECT type, count(*) as ct FROM table GROUP BY type ORDER BY ct;

on mongodb this would be done using the group function, although it's slightly more complicated:

db.collection.group(
           {key: { "type":true},
            reduce: function(obj,prev) { prev.count += 1; },
            initial: { count: 0 }
            });

Here I'm asking the db to return values for the key "type" (hence the "true"), and for each value, the given reduce function will be used to aggregate the found records. Here I'm just updating a count of how many times each record appears. If you run this query you'll get something like this:

[
    {
        "type" : "report",
        "count" : 5
    },
    {
        "type" : "memo",
        "count" : 15
    }
    {
        "type" : "research",
        "count" : 3
    }

]

You'll notice this is not ordered; even the mongodb docs say that the easiest way to order it is to do it client-side.

Relevant documentation is here.

梦途 2024-10-12 01:02:51

您可以使用不同的: http://www.mongodb.org/display/DOCS/ Aggregation#Aggregation-Distinct

php 文档中有一个示例: http:// /php.net/manual/en/mongodb.command.php

$types = $db->command(array("distinct" => "yourCollection", "key" => "type"));

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

我不知道结果是否按频率排序。

You can use distinct : http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct

There is an example in the php doc : http://php.net/manual/en/mongodb.command.php

$types = $db->command(array("distinct" => "yourCollection", "key" => "type"));

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

I don't know if the results are ordered by frequency.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文