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

查看:63
本文介绍了最常见的不同值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'
}

在这里我想找到键名称"的模式,即最重复的唯一名称".

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

用于按重复次数(计数)对结果进行排序.
如果您只希望最大重复一次记录,则可以在其中添加过滤器:

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}

您可以使用该框架做更多的事情.请参阅有关运算符

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

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

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