我的数据如下:
library(tidyverse)
DF %>% separate_rows(V1, sep = ",") %>%
mutate(V1 = gsub("(Option)\\s+(\\d+)","\\1\\2", V1))
ID1 V1
1 Resp1 Option1
2 Resp1 Option1
3 Resp3 Option1
4 Resp3 Option2
5 Resp4 Option2
6 Resp5 Option2
7 Resp6 Option1
8 Resp6 Option2
我想要实现的目标是:: 查找汇总失败的员工(term1 + term2 + term3)
我正在做的事情最终得到的是:
> db.PQRCorp.find().pretty()
{
"_id" : 0,
"name" : "Ancy",
"results" : [
{
"evaluation" : "term1",
"score" : 1.463179736705023
},
{
"evaluation" : "term2",
"score" : 11.78273309957772
},
{
"evaluation" : "term3",
"score" : 6.676176060654615
}
]
}
{
"_id" : 1,
"name" : "Mark",
"results" : [
{
"evaluation" : "term1",
"score" : 5.89772766299929
},
{
"evaluation" : "term2",
"score" : 12.7726680028769
},
{
"evaluation" : "term3",
"score" : 2.78092882672992
}
]
}
{
"_id" : 2,
"name" : "Jeff",
"results" : [
{
"evaluation" : "term1",
"score" : 36.78917882992872
},
{
"evaluation" : "term2",
"score" : 2.883687879200287
},
{
"evaluation" : "term3",
"score" : 9.882668212003763
}
]
}
输出: {" _id" :null," totalTermScore" :90.92894831067625} 简单地说,我得到的是所有分数的总和。 我想要的是,分别为不同的员工加上第1,2和3项。
请有人帮助我。我是MongoDB的新手(虽然非常明显)。
答案 0 :(得分:1)
您无需在此使用$unwind
和$group
...简单的$project
查询可以$sum
您的整个分数...
db.PQRCorp.aggregate([
{ "$project": {
"name": 1,
"totalTermScore": {
"$sum": "$results.score"
}
}}
])