2012-07-08 82 views
1

我有這樣的一個蒙戈集合是這樣的:MongoDB中的MapReduce返回空值

{ 
     "_id" : ObjectId("4fd930ecd729f5b31ea5ad03"), 
     "O_ORDERKEY" : NumberLong(359), 
     "O_CUSTKEY" : { 
       "$ref" : "CUSTOMER", 
       "$id" : ObjectId("4f973ff37d6517e9723c4d63") 
     }, 
     "O_ORDERSTATUS" : "F", 
     "O_TOTALPRICE" : 239998.53, 
     "O_ORDERDATE" : ISODate("1994-12-19T02:00:00Z"), 
     "O_ORDERPRIORITY" : "3-MEDIUM", 
     "O_CLERK" : "Clerk#000000934", 
     "O_SHIPPRIORITY" : 0, 
     "O_COMMENT" : "furiously final foxes are. regular," 
} 

,我想總結所有的o_totalprice當O_SHIPPRIORITY = 0,那麼我使用這個命令:

db.runCommand({ 
    mapreduce: "ORDERS7", 
    query: { 
     O_SHIPPRIORITY: 0 
    }, 
    map : function Map() { 
     emit("sum",this.O_TOTALPRICE); 
    }, 
    reduce : function Reduce(key, values) { 
     var sum = 0; 
     for (var i = 0; i < values.length; i++) { 
      sum += values[i]; 
     } 
     return sum; 
    }, 
    out: 'query' 
}); 

db.query.find(); 

但我結果是這樣的:

{ "_id" : "sum", "value" : null } 

爲什麼值爲什麼值爲空?

回答

2

以下內容適用於我,說明您的map-reduce命令如上所述是正確的。 請完全按照書面方式嘗試以下操作,看看是否得到相同的結果。 如果你這樣做,那麼你的問題在別處,而不是你的問題中描述的任何東西。 希望這有助於。

orders7.js

db.ORDERS7.drop(); 
db.query.drop(); 
doc0 = { 
     "O_ORDERKEY" : NumberLong(359), 
     "O_CUSTKEY" : { 
       "$ref" : "CUSTOMER", 
       "$id" : ObjectId("4f973ff37d6517e9723c4d63") 
     }, 
     "O_ORDERSTATUS" : "F", 
     "O_TOTALPRICE" : 239998.53, 
     "O_ORDERDATE" : ISODate("1994-12-19T02:00:00Z"), 
     "O_ORDERPRIORITY" : "3-MEDIUM", 
     "O_CLERK" : "Clerk#000000934", 
     "O_SHIPPRIORITY" : 0, 
     "O_COMMENT" : "furiously final foxes are. regular," 
}; 
doc1 = { 
     "O_ORDERKEY" : NumberLong(359), 
     "O_CUSTKEY" : { 
       "$ref" : "CUSTOMER", 
       "$id" : ObjectId("4f973ff37d6517e9723c4d63") 
     }, 
     "O_ORDERSTATUS" : "F", 
     "O_TOTALPRICE" : 239998.53, 
     "O_ORDERDATE" : ISODate("1994-12-19T02:00:00Z"), 
     "O_ORDERPRIORITY" : "3-MEDIUM", 
     "O_CLERK" : "Clerk#000000934", 
     "O_SHIPPRIORITY" : 0, 
     "O_COMMENT" : "furiously final foxes are. regular," 
}; 
db.ORDERS7.save(doc0); 
db.ORDERS7.save(doc1); 
printjson(db.ORDERS7.find().toArray()); 

printjson(db.runCommand({ 
    mapreduce: "ORDERS7", 
    query: { 
     O_SHIPPRIORITY: 0 
    }, 
    map : function Map() { 
     emit("sum",this.O_TOTALPRICE); 
    }, 
    reduce : function Reduce(key, values) { 
     var sum = 0; 
     for (var i = 0; i < values.length; i++) { 
      sum += values[i]; 
     } 
     return sum; 
    }, 
    out: 'query' 
})); 
printjson(db.query.find().toArray()); 

$蒙戈XYZZY orders7.js

MongoDB shell version: 2.1.2 
connecting to: xyzzy 
[ 
    { 
     "_id" : ObjectId("4ffa368b9ee7cfbc46a3990a"), 
     "O_ORDERKEY" : NumberLong(359), 
     "O_CUSTKEY" : DBRef("CUSTOMER", ObjectId("4f973ff37d6517e9723c4d63")), 
     "O_ORDERSTATUS" : "F", 
     "O_TOTALPRICE" : 239998.53, 
     "O_ORDERDATE" : ISODate("1994-12-19T02:00:00Z"), 
     "O_ORDERPRIORITY" : "3-MEDIUM", 
     "O_CLERK" : "Clerk#000000934", 
     "O_SHIPPRIORITY" : 0, 
     "O_COMMENT" : "furiously final foxes are. regular," 
    }, 
    { 
     "_id" : ObjectId("4ffa368b9ee7cfbc46a3990b"), 
     "O_ORDERKEY" : NumberLong(359), 
     "O_CUSTKEY" : DBRef("CUSTOMER", ObjectId("4f973ff37d6517e9723c4d63")), 
     "O_ORDERSTATUS" : "F", 
     "O_TOTALPRICE" : 239998.53, 
     "O_ORDERDATE" : ISODate("1994-12-19T02:00:00Z"), 
     "O_ORDERPRIORITY" : "3-MEDIUM", 
     "O_CLERK" : "Clerk#000000934", 
     "O_SHIPPRIORITY" : 0, 
     "O_COMMENT" : "furiously final foxes are. regular," 
    } 
] 
{ 
    "result" : "query", 
    "timeMillis" : 22, 
    "counts" : { 
     "input" : 2, 
     "emit" : 2, 
     "reduce" : 1, 
     "output" : 1 
    }, 
    "ok" : 1 
} 
[ { "_id" : "sum", "value" : 479997.06 } ]