1

I have this collection

> db.test.find()
{ "_id" : ObjectId("5398ddf40371cdb3aebca3a2"), "name" : "ahmed", "qte" : 30 }
{ "_id" : ObjectId("5398de040371cdb3aebca3a3"), "name" : "demha", "qte" : 35 }
{ "_id" : ObjectId("5398de140371cdb3aebca3a4"), "name" : "ahmed", "qte" : 50 }
{ "_id" : ObjectId("5398de210371cdb3aebca3a5"), "name" : "ahmed", "qte" : 60 }

i would like to sum "qte" where "name"= "ahmed" and print the sum with php i know how to do with SQL but i have no idea how it is in mongodb.

Thanks :)

Ahmed
  • 169
  • 1
  • 4
  • 13
  • See this S.O question: http://stackoverflow.com/questions/18969916/mongodb-sum-query – Zander Rootman Jun 12 '14 at 09:38
  • Duplicate of a two year old answer that is not accepted and the answers given are either inefficient or incorrect. What gives? Timestamps on the answers have been altered as well so very interesting indeed. – Neil Lunn Jun 12 '14 at 10:34
  • @NeilLunn timestamps? They look fine to me – Sammaye Jun 12 '14 at 11:54
  • @NeilLunn Also the first answer there does seem to answer this question by example. Judging by the previous questions of this user it would seem that this question would also go unaccepted, his questions seem to be of personal low quality, i.e.: http://stackoverflow.com/questions/23965996/count-mongodb-in-php in this case it is normally better to direct to somewhere that will give a reference than code convert on-demand for him and encourage further questions like this. – Sammaye Jun 12 '14 at 11:58
  • @Sammaye Cannot aggree with you. There was no other answer here when I posted. My point is I don't see a duplicate when the question it is duplicating does not have answers of sufficient quality. Something I have brought up before with others. So those question/answers should be closed. The OP's handling of question acceptance is not the best, but also not really relevant to shutting down a question when better answers can be given. Not interested in chatting. The comment is here for re-open votes. – Neil Lunn Jun 12 '14 at 12:04
  • @NeilLunn I am doing merely what I have read in the meta, if you have a problem please raise it there. – Sammaye Jun 12 '14 at 12:56

2 Answers2

3

Use the aggregation framework.

Assuming you have an the current collection as $collection

result = $collection->aggregate(array(
    array(
        '$match' => array(
            'name' => 'ahmed'
        )
    ),
    array(
        '$group' => array(
            '_id' => NULL,
            'total' => array(
                '$sum' => '$qte'
            )
        )
    )
));

The two parts are the $match to meet the criteria, and the $group to arrive at the "total" using $sum

See other Aggregation Framework Operators and the Aggregation to SQL Mapping chart for more examples.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
0

This is done with an aggregate statement:

db.test.aggregate([
    {
        $match: {
            name: "ahmed"
        } 
    },
    {
        $group: {
            _id:"$name",
            total: {
                $sum: "$qte" 
            }
        }
    }
])
Red
  • 6,599
  • 9
  • 43
  • 85
colburton
  • 4,685
  • 2
  • 26
  • 39