为什么mongoDB计数错误?

时间:2013-05-08 02:35:03

标签: mongodb aggregation-framework

我的数据如下:

 {
       "_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有一个错误。我尝试使用聚合函数解决假设的业务问题。 (编辑:我不是说我解决了mongo问题,或者我希望别人帮我创建一个算法,只是为了确认MongoDB的潜在错误)

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)

{
            "_id" : 1383,
            "avgPop" : 8.772857142857141,
            "docsByTag" : 28,
            "popSum" : 245.63999999999996
        },

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

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 } 

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

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

比集合函数少一个。

所以我认为聚合被破坏并重写为mapReduce

// 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());

}

结果再次被一个

关闭
{
    "_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.9,17.4,7.4,19.43, 8.46,17.21,9.24,9.89],“avg”:8.843846153846155}}

这里奇怪的是,计数是13,但元素的数量是10.这是因为

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对应

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位小数位。

此时真的很困惑。我确信这篇文章可能会对那些陷入困境的其他人有所帮助。

1 个答案:

答案 0 :(得分:1)

聚合框架和mapreduce都不太可能造成错误&#34;所以我会请你验证你如何将他们的结果与你的期望进行比较。

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

db.movies.find({"actors.name":"actor1383"},{title:1,popularity:1,_id: 0})

是否有证据证明你的&#34; actors.name&#34;和&#34; actors.id&#34;字段匹配100%?

当您进行浮点运算时,高于2位的精度是正常的,无需担心。它要求平均值为5和10并得到7.5没有什么不同,即使5和10都没有小数点后的数字。

还有另一个地方的差异&#34;可能来自。如果你有这样的文件:

{受欢迎程度:7.6,   演员:[     {id:1383,       ...       ...     },     {id:1383,       ...       ...     } }

现在你只有一个顶级文档,但是当你展开actors数组时,你现在有两个文件,由此产生的两个都有actor.id 1383.你能验证每个actor只出现一次每个顶级文件?如果不是这样会导致你看到的差异。