MongoDB MapReduce - Emit one key/one value doesnt call reduce

后端 未结 5 1815
情深已故
情深已故 2021-02-01 05:56

So i\'m new with mongodb and mapreduce in general and came across this "quirk" (or atleast in my mind a quirk)

Say I have objects in my collection like so:

5条回答
  •  庸人自扰
    2021-02-01 06:25

    Solution:

    • added new field in map: single: 0
    • in reduce change this field to: single: 1
    • in finalize make checking for this field and make required actions

      $map = new MongoCode("function() {
          var value = {
              time: this.time,
              email_id: this.email_id,
              single: 0
          };
      
          emit(this.email, value);
      }");
      
      $reduce = new MongoCode("function(k, vals) {
      
          // make some need actions here
          return {
              time: vals[0].time,
              email_id: vals[0].email_id,
              single: 1
          };
      }");
      
      $finalize = new MongoCode("function(key, reducedVal) {
          if (reducedVal.single == 0) {
              reducedVal.time = 11111;
          }
          return reducedVal;
      };");
      

提交回复
热议问题