Mongo-Map减少查询在单个文档中正常工作,但不适用于所有记录

时间:2016-02-15 20:32:02

标签: mongodb grails mapreduce

我正在使用map reduce框架来确定这样的用户统计信息。试图找出每个用户的lastOrderDate和totalOrders。

db.order.mapReduce(function() { 
emit (this.customer,{count:1,orderDate:this.orderDate.interval_start}) },
function(key,values){  
    var sum =0 ; var lastOrderDate; 
values.forEach(function(value) {
  lastOrderDate=value['orderDate']; sum+=value['count'];  }); 
        return 
        {totalOrder:sum,lastOrderDate:lastOrderDate}; 
    },
  { 
query:{
status:"DELIVERED","customer":ObjectId("545f64e7e4b07a0a501276db")
},
  out:"order_total"}).find()

这给了我绝对正确的结果

{ "_id" : ObjectId("545f64e7e4b07a0a501276db"), 
"value" : { "totalOrder" : 21, "lastOrderDate" : ISODate("2015-10-02T18:30:00Z") } }

但是当我对所有用户使用相同的查询时,我只是从查询

中删除了客户过滤器
                  "customer":ObjectId("545f64e7e4b07a0a501276db")}

现在,当我再次运行查询时,它显示错误和奇怪的输出,如此

{ "_id" : ObjectId("5443765ae4b05294c8944d5b"), "value" : { "count" : 1, "orderDate" : ISODate("2014-10-18T18:30:00Z") } }
{ "_id" : ObjectId("54561911e4b07a0a501276af"), "value" : { "totalOrder" : 2, "lastOrderDate" : ISODate("2015-03-14T18:30:00Z") } }
{ "_id" : ObjectId("54561b9ce4b07a0a501276b1"), "value" : { "totalOrder" : NaN, "lastOrderDate" : null } }
{ "_id" : ObjectId("5458712ee4b07a0a501276c2"), "value" : { "totalOrder" : 2, "lastOrderDate" : ISODate("2014-11-03T18:30:00Z") } }
  // check this user
  { "_id" : ObjectId("545f64e7e4b07a0a501276db"), "value" : { "totalOrder" : NaN, "lastOrderDate" : null } }
  //
{ "_id" : ObjectId("54690771e4b0070527c657ed"), "value" : { "totalOrder" : NaN, "lastOrderDate" : ISODate("2015-11-02T18:30:00Z") } }
{ "_id" : ObjectId("54696c64e4b07f3c07010b4a"), "value" : { "totalOrder" : 2, "lastOrderDate" : ISODate("2015-11-15T18:30:00Z") } }
{ "_id" : ObjectId("546980d1e4b07f3c07010b4d"), "value" : { "totalOrder" : 4, "lastOrderDate" : ISODate("2015-03-24T18:30:00Z") } }
{ "_id" : ObjectId("54699ac4e4b07f3c07010b51"), "value" : { "totalOrder" : NaN, "lastOrderDate" : null } }
{ "_id" : ObjectId("54699d0be4b07f3c07010b55"), "value" : { "totalOrder" : NaN, "lastOrderDate" : null } }

正如您现在所看到的,它显示了同一用户的不同输出,即

{ "_id" : ObjectId("545f64e7e4b07a0a501276db"), 
"value" : { "totalOrder" : NaN, "lastOrderDate" : null } }   

我是新来的mongo地图reduce,帮助!

0 个答案:

没有答案