Tuesday, 13 August 2013

Mongo query find top sellers from array

Mongo query find top sellers from array

I'm pretty new to Mongo, I'm having trouble figure out how to write this
query. I have an order collection that looks like this.
{
"_id" : ObjectId("51fade5b8c825bb19d9ef228"),
"o_id" : 1,
...
"order_line" : [
{
"ol_id" : 1,
"ol_o_id" : 1,
"ol_i_id" : 531,
"ol_qty" : 280
},
{
"ol_id" : 2,
"ol_o_id" : 1,
"ol_i_id" : 90,
"ol_qty" : 295
},
{
"ol_id" : 3,
"ol_o_id" : 1,
"ol_i_id" : 963,
"ol_qty" : 184
}
]}
I need to find the top selling related items for each item.
The query needs to find the top 5 items/ol_i_id's that are sold WITH the
ol_i_id that is currently being queried. In order to do so the query would
need to find all orders with the "ol_i_id" in question, say "ol_i_id" :
531, then sum the "ol_qty" of each item sold with "ol_i_id" : 531 over the
entire collection. Then report back with with the top 5 "ol_i_id" that are
sold with "ol_i_id":531.
I tried to make that as understandable as possible.

No comments:

Post a Comment