Ivan Lukasevych
Ivan Lukasevych

Reputation: 183

Group by field doesn't work in MongoDB

I'm trying to calculate a sum of all records in my db, and I need to avoid duplicates. I wrote this code to group the records, but it doesn't work for me.

$pipeline = [
    ['$match' =>
            $criteria->getCondition()],
    ['$group' => 
            ['_id' => '$order_id', 'total' => ['$sum' => '$'.$column]]]
];

$this->getDbConnection()->aggregate('ticket_cache', $pipeline);

Test request:

db.getCollection('ticket_cache').aggregate(
{
"$match":
    {"event_id":64}
},
{
    "$group" : 
        {"_id":"$order_id", "total": {"$sum":"$payment_amount"}}
})

Result:

/* 1 */
{
    "result" : [ 
        {
            "_id" : NumberLong(7002),
            "total" : 9000.0000000000000000
        }
    ],
    "ok" : 1.0000000000000000
}

Data in the db:

/* 1 */
{
    "result" : [ 
        {
            "_id" : ObjectId("553f8b4fbfabe2772f8b4f51"),
            "event_id" : NumberLong(64),
            "ticket_id" : NumberLong(8563),
            "ticket_code" : NumberLong(22062299),
            "ticket_type_id" : NumberLong(391),
            "ticket_created" : NumberLong(1430227620),
            "ticket_deleted" : NumberLong(0),
            "ticket_user_id" : NumberLong(2),
            "ticket_used" : NumberLong(0),
            "order_id" : NumberLong(7002),
            "order_code" : NumberLong(517005),
            "order_created" : NumberLong(1430227620),
            "order_deleted" : NumberLong(0),
            "order_sales_pipeline" : NumberLong(18),
            "order_invoice_id" : NumberLong(4202),
            "order_invoice_amount" : 3000.0000000000000000,
            "order_invoice_created" : NumberLong(1430227641),
            "order_invoice_deleted" : NumberLong(0),
            "order_invoice_code" : NumberLong(420155),
            "payment_id" : NumberLong(4365),
            "payment_amount" : 3000.0000000000000000,
            "payment_currency" : NumberLong(4),
            "payment_author_id" : NumberLong(1),
            "payment_type_id" : NumberLong(27),
            "payment_created" : NumberLong(1430227641),
            "payment_deleted" : NumberLong(0),
            "create_time" : ISODate("2015-04-28T13:29:51.328Z")
        }, 
        {
            "_id" : ObjectId("553f8b4fbfabe2772f8b4f4f"),
            "event_id" : NumberLong(64),
            "ticket_id" : NumberLong(8561),
            "ticket_code" : NumberLong(49287433),
            "ticket_type_id" : NumberLong(391),
            "ticket_created" : NumberLong(1430227620),
            "ticket_deleted" : NumberLong(0),
            "ticket_user_id" : NumberLong(2),
            "ticket_used" : NumberLong(0),
            "order_id" : NumberLong(7002),
            "order_code" : NumberLong(517005),
            "order_created" : NumberLong(1430227620),
            "order_deleted" : NumberLong(0),
            "order_sales_pipeline" : NumberLong(18),
            "order_invoice_id" : NumberLong(4202),
            "order_invoice_amount" : 3000.0000000000000000,
            "order_invoice_created" : NumberLong(1430227641),
            "order_invoice_deleted" : NumberLong(0),
            "order_invoice_code" : NumberLong(420155),
            "payment_id" : NumberLong(4365),
            "payment_amount" : 3000.0000000000000000,
            "payment_currency" : NumberLong(4),
            "payment_author_id" : NumberLong(1),
            "payment_type_id" : NumberLong(27),
            "payment_created" : NumberLong(1430227641),
            "payment_deleted" : NumberLong(0),
            "create_time" : ISODate("2015-04-28T13:29:51.316Z")
        }, 
        {
            "_id" : ObjectId("553f8b4fbfabe2772f8b4f50"),
            "event_id" : NumberLong(64),
            "ticket_id" : NumberLong(8562),
            "ticket_code" : NumberLong(24016753),
            "ticket_type_id" : NumberLong(391),
            "ticket_created" : NumberLong(1430227620),
            "ticket_deleted" : NumberLong(0),
            "ticket_user_id" : NumberLong(2),
            "ticket_used" : NumberLong(0),
            "order_id" : NumberLong(7002),
            "order_code" : NumberLong(517005),
            "order_created" : NumberLong(1430227620),
            "order_deleted" : NumberLong(0),
            "order_sales_pipeline" : NumberLong(18),
            "order_invoice_id" : NumberLong(4202),
            "order_invoice_amount" : 3000.0000000000000000,
            "order_invoice_created" : NumberLong(1430227641),
            "order_invoice_deleted" : NumberLong(0),
            "order_invoice_code" : NumberLong(420155),
            "payment_id" : NumberLong(4365),
            "payment_amount" : 3000.0000000000000000,
            "payment_currency" : NumberLong(4),
            "payment_author_id" : NumberLong(1),
            "payment_type_id" : NumberLong(27),
            "payment_created" : NumberLong(1430227641),
            "payment_deleted" : NumberLong(0),
            "create_time" : ISODate("2015-04-28T13:29:51.326Z")
        }
    ],
    "ok" : 1.0000000000000000
}

Where did I go wrong?

Upvotes: 4

Views: 133

Answers (1)

Lalit Agarwal
Lalit Agarwal

Reputation: 2354

Can you try with the below query. It assumes that the payment amount will always be same. Have a look at addToSet http://docs.mongodb.org/manual/reference/operator/update/addToSet/.

db.getCollection('ticket_cache').aggregate( 
{ "$match": {"event_id":64} }, 
{ "$group" :
     {"_id":"$order_id", "total": {"$addToSet":"$payment_amount"}}
},     
{"$unwind": "$total"}, 
{"$group": {"_id": "null", "totalOdr": {"$sum": "$total"}}}
)

Upvotes: 1

Related Questions