Support use Group to sum array of object.

94 views
Skip to first unread message

Tom Vo

unread,
Sep 12, 2012, 6:19:37 AM9/12/12
to mongod...@googlegroups.com
Dear all
My collections which have key is a array object.
I need sum total as SQL statement:
                   Select Line_Items.Name , Sum(Line_Items.Quantity) from Orders group by Line_Items.Name
Could I use group to sum data instead of map/reduce?
Please help me for this case ?

Thanks

My data samples

db.Orders.find()
{
        "_id" : ObjectId("505057cbe415556238661561"),
        "BuyerName" : "Lukas",
        "Line_Items" : [
                {
                        "Name" : "2012 Accord SeDan",
                        "Pricing" : 20,
                        "Quantity" : 1
                },
                {
                        "ProductID" : ObjectId("50504b36e41555623866155c"),
                        "Name" : "2012 Accord Coupe",
                        "Pricing" : 15,
                        "Quantity" : 2
                }
        ],
        "IssueDate" : ISODate("2012-03-08T00:00:00Z"),
         "Total" : 50
}

,

{
        "_id" : ObjectId("505057cbe415556238661560"),
        "BuyerName" : "Bobby",
        "Line_Items" : [
                {
                        "Name" : "2012 Accord SeDan",
                        "Pricing" : 15,
                        "Quantity" : 1
                }
        ],
        "IssueDate" : ISODate("2012-02-08T00:00:00Z"),
        "Total" : 15
}

Thanks and Regards.

Tom Vo

Tom Vo

unread,
Sep 12, 2012, 10:05:58 PM9/12/12
to mongod...@googlegroups.com
Dear all
Have Mongodb support sum/avg key in array of Object by use Aggregation Group/Map Reduce yet?

Best regards.

Stephen Steneker

unread,
Sep 18, 2012, 12:48:14 AM9/18/12
to mongod...@googlegroups.com
My collections which have key is a array object.
I need sum total as SQL statement:
                   Select Line_Items.Name , Sum(Line_Items.Quantity) from Orders group by Line_Items.Name
Could I use group to sum data instead of map/reduce?

Hi Tom,

You could use the Aggregation Framework in MongoDB 2.2 to sum the data by name:

db.Orders.aggregate(
 { $unwind: "$Line_Items" },
 { $group : {
_id: "$Line_Items.Name",
Quantity: { $sum: "$Line_Items.Quantity"}
 }}
)

There are a few limitations to be aware of, in particular that the output from the Aggregation Framework is limited to the maximum BSON document size (16Mb): http://docs.mongodb.org/manual/applications/aggregation/#limitations.

You may also want to start your aggregation with an indexed $match to limit the range of documents to aggregate.

> Have Mongodb support sum/avg key in array of Object by use Aggregation Group/Map Reduce yet?

The new aggregation framework includes a number of mathematical functions such as $sum, $avg, $min, and $max:
 http://docs.mongodb.org/manual/reference/aggregation/#arithmetic-operators

Cheers,
Stephen

Tom Vo

unread,
Sep 18, 2012, 1:34:25 AM9/18/12
to mongod...@googlegroups.com
Hi Stephen
Thanks for your suggestion .


--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb



--

Thanks and Best Regards
-------------------------------------------------------------------------------------------------------------
Vo Tan Hau (TOM)
Senior Database Administrator
NEXCEL SOLUTIONS LTD
SMS Tower,Lot 40, Quang Trung Software City, District 12, HCMC, Vietnam.
Tel:+84-8-37154278 - Fax:+84-8-37154279 www.nexcel.vn
Messenger
(Skype+Yahoo+Live): Vohau2002


Reply all
Reply to author
Forward
0 new messages