[SERVER-3352] Map-Reduce JS increment operators (++, +=1) do not work Created: 30/Jun/11  Updated: 12/Jul/16  Resolved: 12/Oct/11

Status: Closed
Project: Core Server
Component/s: JavaScript, MapReduce
Affects Version/s: 1.8.2
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Pete Campbell Assignee: Antoine Girbal
Resolution: Done Votes: 0
Labels: query
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

osx snow leopard, ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]


Operating System: OS X
Participants:

 Description   

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



 Comments   
Comment by Azat Khuzhin [ 23/Mar/12 ]

Here it is #5379

Comment by Azat Khuzhin [ 23/Mar/12 ]

I have the same issue, must I create a new ticket, or you reopen this, and I post some usefull information?

Comment by Antoine Girbal [ 12/Oct/11 ]

This has been verified to work with mongo 2.0 and master branch.
The following code properly increments values
foo.foo / M/R: { "mapreduce" : "foo" , "map" : "function(){ emit(this.n,

{count:1}

); }" , "reduce" : "function(key, values) { total =

{count: 0}

; for (var i = 0; i < values.length; ++i)

{ total.count++; }

; return total; }" , "out" : { "replace" : "mrtest"}}

Pete, marking as resolved, if you have further clues on the issue please reopen.

Comment by Antoine Girbal [ 05/Jul/11 ]

what do you mean by fails?
do you get a different result that you expect, or is it stuck at 0?
are you using the standard 1.8.2 (with spidermonkey js engine)?
The operator is working for my tests.

Note that in any case it is usually not going to be consistent to do "+ constant" in a reduce.
The reduce may be called many times for the same key, and must be idempotent.
So here the value you get may be higher than you expect.

Generated at Thu Feb 08 03:02:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.