Ruby driver: How can we convert #aggregate from 1.8 to 2.1?

8 views
Skip to first unread message

cloudstrife

unread,
Sep 11, 2015, 6:59:22 AM9/11/15
to mongodb-user
When using the ruby driver v1.8.2 I had the following query:

r = col.aggregate([
  { "$match" => { :d => {'$gte' => key_day(from), '$lte' => key_day(to)} } },
  { "$group" => {
     _id: { "sid" => "$sid" },
     v: {"$sum" => "$v"},
     c: {"$sum" => "$c"}}}
]).to_a


In the process of converting the use for driver 2.1.0: I've kept the query intact and it raised no errors, but seems to return the wrong results.

My question is, how should we convert this query properly? Should maybe instead I do:

r = col.find({ :=> {'$gte' => key_day(from), '$lte' => key_day(to)} }).aggregate([
  { "$group" => {
     _id
: { "sid" => "$sid" },
     v
: {"$sum" => "$v"},
     c
: {"$sum" => "$c"}}}
]).to_a


Thanks!

Emily S

unread,
Sep 11, 2015, 1:54:17 PM9/11/15
to mongodb-user
Hi

You can keep the pipeline exactly the same and pass it to the Collection#aggregate method when upgrading to 2.1.0 from 1.8.2. Can you tell me what the difference in the result set is with an example so I can investigate?

Thanks
Emily
Reply all
Reply to author
Forward
0 new messages