首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >按字段分组在MongoDB中不工作

按字段分组在MongoDB中不工作
EN

Stack Overflow用户
提问于 2015-04-28 13:45:20
回答 1查看 131关注 0票数 4

我正在计算我数据库中所有记录的总和,我需要避免重复。我编写了这段代码来对记录进行分组,但它对我不起作用。

代码语言:javascript
运行
复制
$pipeline = [
    ['$match' =>
            $criteria->getCondition()],
    ['$group' => 
            ['_id' => '$order_id', 'total' => ['$sum' => '$'.$column]]]
];

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

测试请求:

代码语言:javascript
运行
复制
db.getCollection('ticket_cache').aggregate(
{
"$match":
    {"event_id":64}
},
{
    "$group" : 
        {"_id":"$order_id", "total": {"$sum":"$payment_amount"}}
})

结果:

代码语言:javascript
运行
复制
/* 1 */
{
    "result" : [ 
        {
            "_id" : NumberLong(7002),
            "total" : 9000.0000000000000000
        }
    ],
    "ok" : 1.0000000000000000
}

数据库中的数据:

代码语言:javascript
运行
复制
/* 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
}

我哪里出错了?

EN

Stack Overflow用户

回答已采纳

发布于 2015-04-28 14:51:11

您能尝试使用以下查询吗?它假定付款金额总是相同的。看看addToSet http://docs.mongodb.org/manual/reference/operator/update/addToSet/

代码语言:javascript
运行
复制
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"}}}
)
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29921321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档