首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么mongoDB计数不正确?

为什么mongoDB计数不正确?
EN

Stack Overflow用户
提问于 2013-05-08 10:35:03
回答 1查看 2K关注 0票数 1

我的数据如下所示:

代码语言:javascript
复制
 {
       "_id":ObjectId("516fbf68067323ce2ea5b4b8"),
       "title":"GVPKFlFIXdLUaLM",
       "release_year":1913,
       "country_of_origin":"sWdXLXUfun",
       "length_in_minutes":147,
       "plot_summary":"bmwYkyyiSymHJYoXEPauPNjdKoFANDgcDImVelDGPuPJmLhyWOuNXjurNyGp",
       "director":"rNDFhhxGIo",
       "language":"oYeWskT",
       "popularity":5.2,
       "genre":"jDwdaMhuT",
       "actors":[
          {
             "id":2740,
             "name":"actor2740",
             "dob":1989,
             "alt_name":"PBpXPqJwmftpfcR",
             "pob":"DFoxETDuhAdDGNE"
          },
          {
             "id":3143,
             "name":"actor3143",
             "dob":1953,
             "alt_name":"AHnVvTviSKuvNZO",
             "pob":"KBUdvbnvNkXmddk"
          }
       ]
    }

一开始我以为Mongo里有个bug。我尝试使用聚合函数来解决一个假设的业务问题。(编辑:我并不是说我解决了一个mongo问题,也不是说我希望人们帮助我创建一个算法,只是为了确认MongoDB的一个潜在bug )

代码语言:javascript
复制
db.movies.aggregate([{$match:{popularity:{$gte:7.3}}},
     {$project:{actors:1,popularity:1}},
     {$unwind:"$actors"},
     {$group:{_id:"$actors.id",avgPop:{$avg:"$popularity"},
              docsByTag : { $sum : 1 }, popSum:{$sum:"$popularity"}}},
    {$match:{avgPop:{$gte:7.5}}}]);

我关注的结果(编辑$sum: 1而不是0)

代码语言:javascript
复制
{
            "_id" : 1383,
            "avgPop" : 8.772857142857141,
            "docsByTag" : 28,
            "popSum" : 245.63999999999996
        },

但是当我用手验证结果时。

代码语言:javascript
复制
db.movies.find({"actors.name":"actor1383"},{title:1,popularity:1,_id: 0})

{ "title" : "kZFfBwtAfVNobEq", "popularity" : 8.54 }
{ "title" : "kyOeSorYUWyJmjK", "popularity" : 8.11 }
{ "title" : "rvSdJCgEkkpYgFB", "popularity" : 8.36 }
{ "title" : "SwcgHTgZqqcYJja", "popularity" : 8.68 }
{ "title" : "XmcidmdwtDlNoKw", "popularity" : 7.33 }
{ "title" : "gwThvrWifoKCvyG", "popularity" : 7.94 }
{ "title" : "RdUsAFIxTnntTZR", "popularity" : 6.91 }
{ "title" : "RwhJlORFdvtDtpO", "popularity" : 5.13 }
{ "title" : "TuDfcWhNkQFeycl", "popularity" : 9.93 }
{ "title" : "xTVkwnyvftKQraC", "popularity" : 7.27 }
{ "title" : "HYMjUFlSXgnWVTx", "popularity" : 6.94 }
{ "title" : "ZPPyAUdGMeVQhbK", "popularity" : 8.48 }
{ "title" : "kEITAiMMrWTECGM", "popularity" : 9.42 }
{ "title" : "asNsLYKjvHlihXZ", "popularity" : 9.86 }
{ "title" : "ctEmciXPhbMtspt", "popularity" : 8.85 }
{ "title" : "DHjFtctccwDHtlf", "popularity" : 5.5 }
{ "title" : "ElUqbLqkoKrJPVl", "popularity" : 8.26 }
{ "title" : "XdTCieKsWtTbfZa", "popularity" : 5.72 }
{ "title" : "EeNqOPSuKiHuWRs", "popularity" : 5.91 }
{ "title" : "YgysqxcesvPryMY", "popularity" : 6.05 }
{ "title" : "eARvpGydsWilquc", "popularity" : 7.34 }
{ "title" : "NDpdkhSUfePDYjH", "popularity" : 7.28 }
{ "title" : "wUGKLBwijftQKgU", "popularity" : 8.97 }
{ "title" : "UHVGUmAcjBgAPBp", "popularity" : 7.44 }
{ "title" : "NKTKEKfbxFrudVi", "popularity" : 9.4 }
{ "title" : "AeByTKwsEQuQBYG", "popularity" : 8.97 }
{ "title" : "nZskARfGbhYRxdY", "popularity" : 9.16 }
{ "title" : "nBenZrikXFFrrnq", "popularity" : 7.58 }
{ "title" : "GdEFwoKgqjhHvjM", "popularity" : 6.3 }
{ "title" : "grpKTHgnYcDNyXH", "popularity" : 7.16 }
{ "title" : "hXhOqknvjIYJIaT", "popularity" : 5.24 }
{ "title" : "rggTJENnVeuqQVI", "popularity" : 9.95 }
{ "title" : "ABvGVFHkgOumMPO", "popularity" : 9.56 }
{ "title" : "baVkepHniIURUFH", "popularity" : 9.28 }
{ "title" : "PUYXlhPwbanMDmT", "popularity" : 9.6 }
{ "title" : "IJbqonvsVeorDMv", "popularity" : 7.82 }
{ "title" : "iAhyATKYpCVjtMw", "popularity" : 5.88 }
{ "title" : "uDECLFQGTOVnyvC", "popularity" : 6.25 }
{ "title" : "rTwfCYLfLwgPcbH", "popularity" : 8.38 }
{ "title" : "GRyKjecBHQhvYJk", "popularity" : 9.11 }
{ "title" : "GyEaSHoprUvGmZM", "popularity" : 9.92 } 

它给出了大于或等于7.3的27个元素的子集

代码语言:javascript
复制
{ "title" : "kZFfBwtAfVNobEq", "popularity" : 8.54 }
{ "title" : "kyOeSorYUWyJmjK", "popularity" : 8.11 }
{ "title" : "rvSdJCgEkkpYgFB", "popularity" : 8.36 }
{ "title" : "SwcgHTgZqqcYJja", "popularity" : 8.68 }
{ "title" : "XmcidmdwtDlNoKw", "popularity" : 7.33 }
{ "title" : "gwThvrWifoKCvyG", "popularity" : 7.94 }
{ "title" : "TuDfcWhNkQFeycl", "popularity" : 9.93 }
{ "title" : "ZPPyAUdGMeVQhbK", "popularity" : 8.48 }
{ "title" : "kEITAiMMrWTECGM", "popularity" : 9.42 }
{ "title" : "asNsLYKjvHlihXZ", "popularity" : 9.86 }
{ "title" : "ctEmciXPhbMtspt", "popularity" : 8.85 }
{ "title" : "ElUqbLqkoKrJPVl", "popularity" : 8.26 }
{ "title" : "eARvpGydsWilquc", "popularity" : 7.34 }
{ "title" : "wUGKLBwijftQKgU", "popularity" : 8.97 }
{ "title" : "UHVGUmAcjBgAPBp", "popularity" : 7.44 }
{ "title" : "NKTKEKfbxFrudVi", "popularity" : 9.4 }
{ "title" : "AeByTKwsEQuQBYG", "popularity" : 8.97 }
{ "title" : "nZskARfGbhYRxdY", "popularity" : 9.16 }
{ "title" : "nBenZrikXFFrrnq", "popularity" : 7.58 }
{ "title" : "rggTJENnVeuqQVI", "popularity" : 9.95 }
{ "title" : "ABvGVFHkgOumMPO", "popularity" : 9.56 }
{ "title" : "baVkepHniIURUFH", "popularity" : 9.28 }
{ "title" : "PUYXlhPwbanMDmT", "popularity" : 9.6 }
{ "title" : "IJbqonvsVeorDMv", "popularity" : 7.82 }
{ "title" : "rTwfCYLfLwgPcbH", "popularity" : 8.38 }
{ "title" : "GRyKjecBHQhvYJk", "popularity" : 9.11 }
{ "title" : "GyEaSHoprUvGmZM", "popularity" : 9.92 }

比聚合函数小1。

所以我认为aggregate是被破坏的,并将其重写为mapReduce

代码语言:javascript
复制
// make sure we're using the right db; this is the same as "use aggdb;" in shell
db = db.getSiblingDB("recommendations"); //Put your MongoLab database name here.



var mapFunc2 = function() {
                       for (var idx = 0; idx < this.actors.length; idx++) {
                           var key = this.actors[idx].id;
                           var value = {
                                         count: 1,
                                         pop: this.popularity
                                       };
                           emit(key, value);
                       }
                    };

var reduceFunc2 = function(keyActor, countObjVals) {


                     reducedVal = { actor: keyActor, count: 0, pop: 0, pop_list : [] };

                     for (var idx = 0; idx < countObjVals.length; idx++) {
                         reducedVal.count += countObjVals[idx].count;
                         reducedVal.pop += countObjVals[idx].pop;
                         reducedVal.pop_list = reducedVal.pop_list.concat(countObjVals[idx].pop);
                     }

                     return reducedVal;
                  };

var finalizeFunc2 = function (key, reducedVal) {

                       reducedVal.avg = reducedVal.pop/reducedVal.count;

                       return reducedVal;

                    };


result = db.movies.mapReduce( mapFunc2,
                     reduceFunc2,
                     {
                       out: { merge: "mre" },
                       query: { popularity:
                                  { $gte: 7.3 }
                              },
                       finalize: finalizeFunc2
                     }
                   )
cursor = db.map_reduce_example.find()                  

while(cursor.hasNext()){
    printjson(cursor.next());

}

结果又差了一分。

代码语言:javascript
复制
{
    "_id" : 1383,
    "value" : {
        "actor" : 1383,
        "count" : 28,
        "pop" : 245.63999999999996,
        "avg" : 8.772857142857141
    }
}

所以我开始调试,当涉及到保存数组中每个单独电影的受欢迎程度时,我看到了一些奇怪的事情。

{ "_id“:1,"value”:{ "actor“:1,"count”:13,"pop“:114.97,"pop_list”:7.47,8.52,9.95,17.4,7.4,19.43,8.46,17.21,9.24,9.89,"avg“:8.843846153846155 }}

在这里,奇怪的是计数是13,而元素的数量是10。

代码语言:javascript
复制
7.4 7.4
7.47    7.47
8.07    1
8.14    2
8.46    8.46
8.52    8.52
9.14    1
9.24    9.24
9.26    2
9.57    3
9.86    3
9.89    9.89
9.95    9.95

其中1,2,3对应于

代码语言:javascript
复制
1   17.21=9.14+8.07
2   17.4=8.14+9.26
3   19.43=9.57+9.86

{ "_id“:2,"value”:{ "actor“:2,"count”:14,"pop“:120.91999999999999,"pop_list”:35.239999999999995,7.58,35.56,9.35,25.839999999999996,7.35,"avg“:8.637142857142857 }}然而,上面的公式完全不清楚,因为我所有的平均值都只有2位小数精度。

在这一点上真的很困惑。我相信这篇文章会对其他被同样类型的计数问题困扰的人有所帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-08 12:35:43

聚合框架和mapreduce都犯了“错误”的可能性很小,所以我想请您验证一下,您是如何将它们的结果与您的期望进行比较的。

在聚合中,您可以对"actors.id"字段进行分组。但是您手动验证的查询是:

代码语言:javascript
复制
db.movies.find({"actors.name":"actor1383"},{title:1,popularity:1,_id: 0})

有证据表明你的"actors.name“和"actors.id”字段100%匹配吗?

当你做浮点运算时,高于2位的精度是正常的,不用担心。这与要求5和10的平均值并得到7.5没有什么不同,即使5和10在小数点后都没有数字。

还有另一个“差异”可能来自的地方。如果您有这样的文档:

{人气: 7.6,演员:[{ id: 1383,... },{ id: 1383,... ... }

您现在只有一个顶层文档,但当您展开actors数组时,您现在有两个文档,这两个文档都具有actor.id 1383。您能验证每个参与者在每个顶级文档中只出现一次吗?如果不是,这将导致您看到的差异。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16431768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档