Php 字段分组在MongoDB中不起作用

Php 字段分组在MongoDB中不起作用,php,mongodb,Php,Mongodb,我试图计算数据库中所有记录的总和,我需要避免重复。我写这段代码是为了对记录进行分组,但它不适合我 $pipeline = [ ['$match' => $criteria->getCondition()], ['$group' => ['_id' => '$order_id', 'total' => ['$sum' => '$'.$column]]] ]; $this->getDbCon

我试图计算数据库中所有记录的总和,我需要避免重复。我写这段代码是为了对记录进行分组,但它不适合我

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

$this->getDbConnection()->aggregate('ticket_cache', $pipeline);
测试请求:

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

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

/* 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
}

我哪里出错了?

你能试试下面的查询吗。它假设付款金额始终相同。看看addToSet


你说的避免重复是什么意思?你期望结果是什么样子?@JohnnyHK,正如你在上面看到的,我有3条相同顺序id的记录。我需要计算所有具有唯一顺序id的记录的总和。在这种情况下,我期望得到“3000”,因为正如我之前所说,我有相同顺序id的记录,所以在分组后,它必须只找到一条记录,并且它应该计算总和。@Kossgreim-你不能很好地计算一个数字的总和,你可以;只是电话号码。您要求Mongo做的是将具有相同订单id的所有内容放入一个组中,然后对于每个组,返回该组中每个付款金额的总和。@xathien,例如在SQL中,它将给出我需要的结果:从票证缓存中选择SUMpayment作为总计,其中事件\u id=64按订单\u id分组返回3000@Kossgreim-嗯,不会。对数据的SQL查询也会返回9000,因为它会将所有3条记录放入order_id=7002的组中,然后将所有3条记录的payment_amount字段相加。您想要的是获得每个组的付款金额,然后求和。结果是:{result:[{{id:NumberLong7002,totalOdr:3000.0000000000000000},{id:NumberLong7003,totalOdr:5000.0000000000000000}],ok:1.0000000000000000000000}有没有办法做到这一点{result:[{u id:NumberLong7002,totalOdr:8000.0000000000000000},],ok:1.0000000000000000}`要将所有结果相加为一,在上面的情况下,它返回两个:3000和5000
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"}}}
)