MongoDB 计数不同的值? [英] MongoDB count distinct value?

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

问题描述

下面显示我的代码.我必须计算不同值重复的次数.在这里,我在结果"中存储了不同的值.我使用 collection.count() 来计算但它不起作用.请任何人告诉我我必须错误的地方.非常感谢.

Here below show my code. I have to calculate the how many times distinct value repeated. Here i have store distinct value in "results".I used collection.count() to calculate but it's not work. please any one tell me where i have to mistake. Thank you very much .

var DistinctIntoSingleDB = function(Collection,arr,opt,distVal,callback){
 Collection.find({}).distinct(distVal, function(err, results) {
      if(!err && results){
            console.log("Distinct Row Length :", results.length);
            var a,arr1 = [];
            for(var j=0; j<results.length; j++){
                collection.count({'V6': results[j]}, function(err, count) {
                      console.log(count)
                });

                arr1.push(results[j]+ " : " +a);
            }
            callback(results,arr1);
      }else{
           console.log(err, results);
           callback(results);
      }
 });

推荐答案

获取字段 'field1' 在集合 'col1' 上出现的不同值并写入单独的集合 'distinctCount'.还允许在集合很大的情况下使用磁盘空间.

To get occurrences of distinct values of a field 'field1' on a collection 'col1' and write to a separate collection 'distinctCount'. Also allow to use disk space in case the collection is huge.

db.col1.aggregate(
          [{$group: {
              _id: "$field1",
              count: { $sum : 1 }
            }}, {
            $group: {
              _id: "$_id",
              count: { $sum : "$count" }
            }},{
              $out: "distinctCount"
            }],
         {allowDiskUse:true}
)

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

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