如何访问以下 mongoDB map reduce 代码的输出?我假设 map reduce 函数正在生成一个名为“session_stat”的集合,其中包含字段:“dayOfWeek”和“count”,我可以使用 find() 和 printValue 函数进行打印。但是当我使用“mongo code.js”执行代码时,我只是得到“未定义”。提前致谢。
db = db.getSiblingDB("mrex");
// simulate saving records that log the lengths of user sessions in seconds
db.session.save({userid:"a", time: ISODate('2011-11-03 14:17:00'), length: 95});
db.session.save({userid:"b", time: ISODate('2011-11-03 14:23:00'), length: 110});
db.session.save({userid:"c", time: ISODate('2011-11-03 15:02:00'), length: 120});
db.session.save({userid:"d", time: ISODate('2011-11-03 16:45:00'), length: 45});
function mapf()
{
emit(this.time.getDay(),
{count:1});
}
function reducef(key, values)
{
var r = {dayOfWeek:key, count:0};
values.forEach(function(v)
{
r.count += v.count;
});
return r;
}
var mrcom1 = db.runCommand( { mapreduce:"session",
map:mapf,
reduce:reducef,
query: {time: {$gt:ISODate('2011-11-03 00:00:00')}},
out: { reduce: "session_stat" }
});
function printValue (r) {
print(r.count)
};
db.session_stat.find().forEach(printValue)