When using mapReduce in mongodb, some values are not processed by reduce, which is very confusing. First code
db.test.mapReduce(
function () {
emit({
host: this.host,
os: this.os,
computed_on_date: this.computed_on_date,
uid: this.uid
}, {data: this.os});
},
function (key, value) {
return {result: 1};
},
{
out: "a"
}
)
The data that should be output is
"_id" : {
"host" : "www.ddc.com",
"os" : "android",
"computed_on_date" : "2017-04-19",
"uid" : "0ae71c7\ne-7da6-9051-ec6c-49dc671b4e3a"
},
"value" : {
"result" : 1
}
The actual output data is
"_id" : {
"host" : "www.ddc.com",
"os" : "android",
"computed_on_date" : "2017-04-19",
"uid" : "0ae71c7\ne-7da6-9051-ec6c-49dc671b4e3a"
},
"value" : {
"data" : "android"
}
I am very confused. When the key of condition emit is less than 4, it will be displayed normally. If it is more than 3 or query condition is added, reduce processing will not be performed.
Another question is, when mapReduce has a query, should it process the query first or process reduce and then process the query in the result
It is recommended to use aggregation. Compared with MR, aggregation is the first class feature of MongoDB.
For reference.
Love MongoDB! Have fun!