最常见的不同值 mongodb [英] Most common distinct values mongodb

查看:19
本文介绍了最常见的不同值 mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在查询结果中获取一个键的最常见的不同值.

How to get the most common distinct values of a key in query results.

考虑一个集合collectionSample"

Consider a collection 'collectionSample'

{
    name : 'a',
    value: 10,
    word : 'baz'
},
{
    name : 'a',
    value: 65,
    word : 'bar'
},
{
    name : 'a',
    value: 3,
    word : 'foo'
},
{
    name : 'b',
    value: 110,
    word : 'bar'
},
{
    name : 'b',
    value: 256,
    word : 'baz'
}

这里我想找到key 'name' 的模式,即最重复的不同'name'.

Here I want to find the mode of key 'name', that is the most repeated distinct 'name'.

我希望得到的结果就像

 {'most_common_distinct_val':a}  //since a is count 3 and b is count 2

如何在NodeJs mongo客户端查询?

How to query it in NodeJs mongo client?

推荐答案

2017-08-01更新

随着 MongoDB 3.4 的发布,可以使用 $ 简化以下代码sortByCount,本质上等于 $group + $sort.代码片段:

2017-08-01 Update

As release of MongoDB 3.4, the following code can be simplified by using $sortByCount, which essentially equals to $group + $sort. Code snippet:

col.aggregate([{
    "$sortByCount": "$name"
}], ...);

<小时>

mongodb 聚合框架 可以完成这项工作.代码示例:


The mongodb aggregation framework would do the job. Code sample:

var MongoClient = require("mongodb").MongoClient;
MongoClient.connect("mongodb://localhost/YourDB", function(err, db) {
    var col = db.collection("YourCol");
    col.aggregate([{
        "$group": {_id: "$name", count: { "$sum": 1}}
    }, {
        "$sort": {count: -1}
    }], function(err, docs) {
        var keys = []
        docs.forEach(function(doc) {
            console.log(JSON.stringify(doc)); // do what you want here.
        });
    });
});

聚合框架使用不同的过滤器"来过滤结果集.正如您在示例中看到的,有一个包含所有这些过滤器的数组.
这里我有 2 个过滤器,第一个:

The aggregation framework uses different "filters" to filter out the result set. As you can see in the sample, there's an array of all these filters.
Here I have 2 filters, the first one:

{"$group": {_id: "$name", count: { "$sum": 1}}}

是按名称对数据进行分组并计算重复次数.
第二个:

is to group your data by name and count the repeated times.
The 2nd one:

{"$sort": {count: -1}}

是按照重复次数(count)对结果进行排序.
如果你只想要最多重复的一条记录,你可以在那里添加一个过滤器:

is to sort the result by repeated times (count).
if you want only the max repeated one record, you can add a filter there:

{"$limit": 1}

您可以使用该框架做更多事情.请参阅有关 operators

You can do a lot more things with the framework. refer to the doc about operators

这篇关于最常见的不同值 mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆