如何在MongoDB聚合查询中对单个数组元素求和?


How do we sum individual array elements in MongoDB aggregation query?

{"版本":"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","名称":{"en - us":"ps3"},"描述":{"en - us":"ps3"}}},"时间戳":"2016 - 10 - 25 t11:21:25.917z","背景":{"扩展":{" http://localhost/eventinfo ": {"sessionId":"1477393533327","starttm":"1477394351210","eventtm":"1477394485917","课程":"cji"}}," contextActivities ": {"父":({"objectType":"活动","id":"http://localhost/id=cji"}]}},"结果":{"持续时间":"PT2M14.71S","分数":{"生":6,"马克斯":21}},"权威":{"objectType":"代理","name":"New Client",:"mbox mailto: hello@lhd.net"},"存储":"2016 - 10 - 25 t11:20:29.666700 + 00:00","id":"c7039783 - 371 f - 4 - f59 a665 - 65 a9d09a2b7f"}我们有这个PHP + MongoDB聚合查询:$condition = array(阵列('$match' => array('client_id' => $CFG->mongo_clientid,' statement.actor.account.name ' =>阵列("$"=>数组(' 67 ',' 192 ',' 213 ')),"statement.verb。Id ' => 'http://adlnet.gov/expapi/verbs/completed',"statement.object。Id ' => 'http://localhost/action?id = cji")),阵列('$group' => array('_id' => '$statement.actor.account.name',//' totalpoints ' =>阵列("美元金额"=>数组("最后美元"=>"statement.result.score.raw美元"))'laststatement' => array('$last' => '$statement.result.score.raw'),//' sumtest ' =>阵列('添加美元' => [' laststatement美元']))));$cursor = $collection->aggregate($condition);回声"<>之前";print_r($光标);回声"之前";它返回如下结果:数组([result] =>数组([0] =>数组([_id] => 192[laststatement] => MongoInt64对象([value] => 4))[1] =>数组([_id] => 67[laststatement] => MongoInt64对象([value] => 6)))[ok] => 1)之前

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

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

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

看起来你想要的只是一个组。因此分组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 }

Update更改为包含每个组中最后一条语句的和。第一个分组是根据参与者名称进行的,并返回每个组中的最后一条语句。第二组对最后一条语句进行求和。

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"
        },
    }
}])