I have a mongodb collection like this
{"assigneeId" => 1000, "status" => 3, "starttime" => "2014 Feb 25", "numofdays => 6} {"assigneeId" => 1000, "status" => 2, "starttime" => "2014 Jan 10", "numofdays => 6} {"assigneeId" => 1000, "status" => 3, "starttime" => "2014 Jan 1", "numofdays => 20}
I wrote a MongoDB query to group the above collection with assigneeId whose status is 3 and add the value of numofdays like this
db.events.group ( { key: {assigneeId:1}, cond: {status: {$gte: 3}}, reduce: function (curr, result) {result.total++; result.numofdays += curr.numofdays;}, initial: {total:0, numofdays:0} })
This gives the output as expected from the Mongodb cli.
When I write the ruby code for executing the same query, I could not make it work. Am getting lot of different hits for similar problem but none of the format seems to work. I could not go past the syntax error in ruby for grouping function. Please note I am not using mongoid. If I could get this work with Aggregation framework, that is also fine with me.
This is the ruby query I wrote and the error for the same.
@events_h.group (
:cond => {:status => { '$in' => ['3']}},
:key => 'assigneeId',
:initial => {count:0},
:reduce => "function(x, y) {y.count += x.count;}"
)
Error I am getting
analyze.rb:43: syntax error, unexpected ',', expecting ')' out = @events_h.group (["assigneeId"], { }, { }, "function() { }") ^ analyze.rb:43: syntax error, unexpected ')', expecting keyword_end analyze.rb:83: syntax error, unexpected end-of-input, expecting keyword_end
Any help is much appreciated.
Thanks