Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-3352

Map-Reduce JS increment operators (++, +=1) do not work

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 1.8.2
    • Component/s: JavaScript, MapReduce
    • Labels:
    • Environment:
      osx snow leopard, ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]
       
    • OS X

      In my reduce function, I can only increment a counter with

      result.counter += value.counter

      These alternatives do not work:

      result.counter++
      result.counter += 1
      result.counter = result.counter + 1

      Full code:

      def total (query = nil)
      map_fn = "function() {
      values = {};
      values.displays = 1;
      values.attempts = this.disposition != '16' ? 1 : 0;
      values.contacts = this.disposition == '1' ? 1 : 0;
      emit(1,values);
      }"

      reduce_fn = "function(key, values) {
      var total =

      {displays: 0, attempts: 0, contacts: 0}

      ;
      values.forEach( function(value)

      { // total.displays++; // FAIL // total.displays += 1; // FAIL // total.displays = total.displays+1; // FAIL total.displays += value.displays; // SUCCESS total.attempts += value.attempts; total.contacts += value.contacts; }

      );
      return total;
      }"

      results_collection = Contact.collection.map_reduce(map_fn, reduce_fn, {:out => {:inline => 1}, :raw => true, :query => query})['results']
      results_collection.present? ? results_collection.first['value'] : []

      end

            Assignee:
            antoine Antoine Girbal
            Reporter:
            dogbert2521 Pete Campbell
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: