Some of the value in mapReduce in mongodb are not reduce.

  mongodb, question

The use of mapReduce in mongodb has some value that have not been reduce, which is very confusing. Code first

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 are

"_id" : {
 "host" : "www.ddc.com",
 "os" : "android",
 "computed_on_date" : "2017-04-19",
 "uid" : "0ae71c7\ne-7da6-9051-ec6c-49dc671b4e3a"
 },
 "value" : {
 "data" : "android"
 }

It is very doubtful that if the key of the condition emit is less than 4, it will be displayed normally. If it is more than 3 or if the query condition is added, the reduce process will not be carried out.

There is another question, when mapreduce has query, is it to process query first or to select Reduce and then process query in the result?

Aggregation is recommended. Compared with MR, aggregation is the first class characteristic of MongoDB.

For reference.

Love MongoDB! Have fun!