【发布时间】: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