我无法解释如何解释,但这就是发生在我身上的事情:
当我运行以下命令时:
db.rating_predictions.mapReduce(
function(){
if (!this.target_infos){return;}
for (i=0;i<this.target_infos.length;i++){
var obj = {
channel_id:this.channel_id,
target_audience:this.target_infos[i].name,
day:parseInt(this.time_slot.getDay())
}
emit(obj,this.target_infos[i]);
}},
function(ids,target_infos){
var res={};
res.thousands = 0.0;
res.rating = 0.0;
//print(target_infos.length);
for (var i=0;i<target_infos.length;i++){
var thousands = parseFloat(target_infos[i].thousand_viewer);
var rating = parseFloat( target_infos[i].trp);
if (isNaN(thousands)){
thousands=0;
}
if (isNaN(rating)){
rating=0;
}
res.thousands+=thousands;
res.rating+=rating;
//print(i);
}
return res;
}
,
{
out:{inline:true}
}
)
响应的第一条记录是:
{
"_id" : {
"channel_id" : ObjectId("53219847091d6601dd000018"),
"target_audience" : "13-19jaar",
"day" : 0
},
"value" : {
"thousands" : 1,
"rating" : 0.10793
}
}
现在我知道你一定认为你需要看到数据,但是,打开你的头脑,看看这个。当我在映射中没有channel属性的情况下运行相同的查询时。含义:
db.rating_predictions.mapReduce(
function(){
if (!this.target_infos){return;}
for (i=0;i<this.target_infos.length;i++){
var obj = {
//channel_id:this.channel_id, **this is what i removed**
target_audience:this.target_infos[i].name,
day:parseInt(this.time_slot.getDay())
}
emit(obj,this.target_infos[i]);
}},
function(ids,target_infos){
var res={};
res.thousands = 0.0;
res.rating = 0.0;
//print(target_infos.length);
for (var i=0;i<target_infos.length;i++){
var thousands = parseFloat(target_infos[i].thousand_viewer);
var rating = parseFloat( target_infos[i].trp);
if (isNaN(thousands)){
thousands=0;
}
if (isNaN(rating)){
rating=0;
}
res.thousands+=thousands;
res.rating+=rating;
//print(i);
}
return res;
}
,
{
out:{inline:true}
}
)
我得到以下信息:
{
"_id" : {
"target_audience" : "13-19jaar",
"day" : 0
},
"value" : {
"thousands" : 0,
"rating" : 0
}
}
现在我很困惑,怎么可能对于特定的channel_id,我在第0天的评分>0,而对于所有频道,我的评分都为0
我检查了是否得到负值
db.rating_predictions.count({"target_infos.trp":{"$lt":0}})
结果为0。
我真的很感激你的帮助,谢谢。
我想明白了res对象必须具有与reduce函数的第二个参数相同的字段,因此更改的名称
var res={};
res.thousand_viewer = 0.0;
res.trp = 0.0;