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 :)

6
  • See this S.O question: stackoverflow.com/questions/18969916/mongodb-sum-query Commented Jun 12, 2014 at 9: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. Commented Jun 12, 2014 at 10:34
  • @NeilLunn timestamps? They look fine to me Commented Jun 12, 2014 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.: 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. Commented Jun 12, 2014 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. Commented Jun 12, 2014 at 12:04

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

This is done with an aggregate statement:

db.test.aggregate([
    {
        $match: {
            name: "ahmed"
        } 
    },
    {
        $group: {
            _id:"$name",
            total: {
                $sum: "$qte" 
            }
        }
    }
])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.