Hi Jenna,
Thanks for your reply and the modification on my Reduce function where
indeed lies some bugs when i wrote this post.
The demand is briefly that get the user name and the platform with the
latest information.
Here i give a tested example:
The data:
{
"_id" : ObjectId("4fac78d8a1681d11dc93b498"),
"name" : "eason",
"date" : 20120511,
"platform" : "ubuntu"
}
{
"_id" : ObjectId("4fac78f8a1681d11dc93b49a"),
"name" : "wang",
"date" : 20120511,
"platform" : "xp"
}
{
"_id" : ObjectId("4fac78eba1681d11dc93b499"),
"name" : "eason",
"date" : 20120512,
"platform" : "redhat"
}
Map function:
function Map() {
emit(
this.name, {"platform":this.platform,"date":this.date});
}
Reduce function1:
function Reduce(key, number) {
var date=0;
var platform;
for(var i in number){
if(date<number[i].date){
date=number[i].date; platform=number[i].platform;
}
}
return {"date":date,"platform":platform};
}
After this MapReduce, the result is as expected:
{
"_id" : "eason",
"value" : {
"date" : 20120512.0,
"platform" : "redhat"
}
}
{
"_id" : "wang",
"value" : {
"platform" : "xp",
"date" : 20120511.0
}
}
/********************************************/
Reduce function2:
function Reduce(key, number) {
var date=0;
var platform;
for(var i in number){
if(date<number[i].date){
date=number[i].date; platform=number[i].platform;
}
}
return platform;
}
In this case, i want to modify the "value" structure of MapReduce
result since the "date" is not what i concern. Strangely the result is
that:
{
"_id" : "eason",
"value" : "redhat"
}
{
"_id" : "wang",
"value" : {
"platform" : "xp",
"date" : 20120511.0
}
}
It seems that the "eason" documents can reach the demand as i desired,
while the "wang" document (unique key) cannot. So must the quoation
"the value that the reduce function returns must match the structure
of the map function's emitted value" be followed? How to explain the
unbalanced output with Reduce function2 (For unique key doc, it
doesn't work, otherwise, it seems work). My current assumption is that
in the MapReduce mechanism, if the unique-key document is detected
after Map, it will bypass the Reduce process. Is that reasonable?
Thanks!
Regards,
Eason Wang