【问题标题】:How do we sum individual array elements in MongoDB aggregation query?我们如何对 MongoDB 聚合查询中的单个数组元素求和?
【发布时间】:2017-03-07 04:16:24
【问题描述】:
文档:
  {
    “版本”:“1.0.0”,
    “演员”:{
        "objectType": "代理",
        "name": "测试用户",
        “帐户”: {
            "主页": "http://testing.com/",
            “名称”:“67”
        }
    },
    “动词”:{
        "id": "http://adlnet.gov/expapi/verbs/completed",
        “展示”: {
            “en-US”:“已完成”
        }
    },
    “目的”: {
        "objectType": "活动",
        "id": "http://localhost/action?id=cji",
        “定义”: {
            “类型”:“http://adlnet.gov/expapi/activities/lesson”,
            “姓名”: {
                "zh-CN": "ps3"
            },
            “描述”: {
                "zh-CN": "ps3"
            }
        }
    },
    “时间戳”:“2016-10-25T11:21:25.917Z”,
    “语境”: {
        “扩展”:{
            “http://localhost/eventinfo”:{
                "sessionId": "1477393533327",
                "starttm": "1477394351210",
                "eventtm": "1477394485917",
                “课程”:“cji”
            }
        },
        “上下文活动”:{
            “父母”:[
                {
                    "objectType": "活动",
                    "id": "http://localhost/id=cji"
                }
            ]
        }
    },
    “结果”: {
        “持续时间”:“PT2M14.71S”,
        “分数”: {
            “原始”:6,
            “最大”:21
        }
    },
    “权威”: {
        "objectType": "代理",
        "name": "新客户",
        "mbox": "mailto:hello@lhd.net"
    },
    “存储”:“2016-10-25T11:20:29.666700+00:00”,
    “id”:“c7039783-371f-4f59-a665-65a9d09a2b7f”
}

我们有这个 PHP + MongoDB 聚合查询:

    $条件 = 数组(
                 大批(
                '$match' => 数组(
                    'client_id' => $CFG->mongo_clientid,
                    'statement.actor.account.name' => array('$in'=> array('67','192','213')),
                    'statement.verb.id' => 'http://adlnet.gov/expapi/verbs/completed',
                    'statement.object.id' => 'http://localhost/action?id=cji'
                )),
                 大批(
                '$group' => 数组(
                    '_id' => '$statement.actor.account.name' ,
                    //'totalpoints' =>array('$sum' => array('$last' => '$statement.result.score.raw'))
                    'laststatement' => array('$last' => '$statement.result.score.raw'),
                    //'sumtest' => array('$add' => ['$laststatement'])
                     )
                  )
            );
             $cursor = $collection->aggregate($condition);
             回声“
”;
             print_r($cursor);
             回声“
”; 返回此结果: 大批 ( [结果] => 数组 ( [0] => 数组 ( [_id] => 192 [laststatement] => MongoInt64 对象 ( [值] => 4 ) ) [1] => 数组 ( [_id] => 67 [laststatement] => MongoInt64 对象 ( [值] => 6 ) ) ) [好的] => 1 )

我们如何在 MongoDB 聚合查询中对这些单个数组元素的 [laststatement].[value] 求和?

[laststatement] => MongoInt64 Object
                        (
                            [value] => values goes here
                        )

另外,我们如何在MongoDB聚合查询中同时使用$last$sum? 在我的结果中,2 个不同的 id (192,67) 有 2 个原始分数(最后一条语句)。我想对所有多个 id 的分数求和,例如 4 + 6 = 10,但只需要最后一个语句的最后一个分数。我无法在线使用 $last 和 $sum。请检查

【问题讨论】:

  • 您想求和并产生一个组吗?如果可以,请添加数据结构。
  • 向问题添加了文档。请检查

标签: php mongodb mongodb-query aggregation-framework


【解决方案1】:

看起来你想要的只是一个组。所以分组 id 应该为空。如果您关心最后一条记录应该是什么,您可能需要添加一个排序。未测试。

array(
      '$group' => array(
      '_id' =>  null ,
      'totalpoints' => array( '$sum' => '$statement.result.score.raw')                
      'laststatement' => array('$last' => '$statement.result.score.raw')
     )
)

这里是 mongo shell 版本。

aggregate([
    {
       $match :{
             "actor.account.name":{$in:["67","192","213"]},
             "verb.id":{$eq:"http://adlnet.gov/expapi/verbs/completed"},
             "object.id":{$eq:"http://localhost/action?id=cji"}
       }
    },
    {
      $group: {
          "_id": null,
          "totalpoints" : {$sum:"$result.score.raw"},                
          "laststatement" :{$last:"$result.score.raw"}
      }
    }
])

输出:

{ "_id" : null, "totalpoints" : 10, "laststatement" : 4 }

更新 更改为包括每个组中最后一条语句的总和。第一个分组是按参与者名称并返回每个组的最后一条语句。第二个分组总结了所有最后一条语句。

aggregate([{
    $match: {
        "actor.account.name": {
            $in: ["67", "192", "213"]
        },
        "verb.id": {
            $eq: "http://adlnet.gov/expapi/verbs/completed"
        },
        "object.id": {
            $eq: "http://localhost/action?id=cji"
        }
    }
}, {
    $group: {
        "_id": "$actor.account.name",
        "laststatement": {
            $last: "$result.score.raw"
        }
    }
}, {
    $group: {
        "_id": null,
        "totalpoints": {
            $sum: "$laststatement"
        },
    }
}])

【讨论】:

  • 这只会为用户 67 生成一个输出,并给出 67 生成的所有语句的总数,它将为 60。我们希望最后一条语句的总数为 67,192,而总数不是 10
  • 根据之前的评论更新了答案。请验证。
  • 谢谢@Veeram,它解决了我的问题,现在我被困在另一个查询中。用新问题更新了问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-27
  • 2021-11-17
  • 2019-02-02
  • 1970-01-01
  • 2021-05-27
  • 2017-03-12
相关资源
最近更新 更多