This code works fine in mongodb shell:
db.orders.aggregate({$unwind: "$picklist"}, {$group : {_id: "$picklist.sku", qty: {$sum : "$picklist.quantity"}}},{ $sort : { qty : 1}})
I thought conversion to mongoid syntax would look like:
Order.collection.aggregate(
{'$unwind'=> '$picklist'},
{'$group'=> {'_id'=> '$picklist.sku',
'qty'=> {'$sum'=> '@picklist.quantity'}}},
{ '$sort'=> {'qty'=> -1}})
Then I discovered that $sum and $sort were converted not in a mongoid way.
Interesting moment is that I didn't get any errors, it was just giving me zeros for qty.
As for $sum I have to use (as shown here)
.sum(:field_name)
Can't figure out how
'qty'=> {'$sum'=> '@picklist.quantity'}}},
{ '$sort'=> {'qty'=> -1}})
should be expressed in a mongoid way.
Posted also on StackOverflow http://stackoverflow.com/q/18139632/1745902
qty field for each document resulted from the aggregation is 0. But when I perform aggregation from within mongodb shell it's not zero and has some value depending on the document...--
---
You received this message because you are subscribed to the Google Groups "Mongoid" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoid+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
'@picklist.quantity' --> '$picklist.quantity'