【发布时间】:2018-05-16 09:16:13
【问题描述】:
我想计算正确、错误和未尝试的问题计数。我得到零值。
查询 -
db.studentreports.aggregate([
{ $match: { 'groupId': 314 } },
{ $unwind: '$questions' },
{ $group:
{
_id: {
dateTimeStamp: '$dateTimeStamp',
customerId: '$customerId'
},
questions : { $push: '$questions' },
unttempted : { $sum : { $eq: ['$questions.status',0]}},
correct : { $sum : { $eq: ['$questions.status',1]}},
incorrect : { $sum : { $eq: ['$questions.status',2]}},
Total: { $sum: 1 }
}
}
])
架构结构 -
{
"_id" : ObjectId("59fb46ed560e1a2fd5b6fbf4"),
"customerId" : 2863318,
"groupId" : 309,
"questions" : [
{
"questionId" : 567,
"status" : 0,
"_id" : ObjectId("59fb46ee560e1a2fd5b700a4"),
},
{
"questionId" : 711,
"status" : 0,
"_id" : ObjectId("59fb46ee560e1a2fd5b700a3")
},
....
未尝试、正确和不正确的值都会出错 -
“未曾尝试”:0, “正确”:0, “不正确”:0, “总计”:7558.0
需要根据 datetime 和 customerId 分组。 有人可以正确查询吗? 谢谢。
【问题讨论】:
标签: mongodb mongoose mongodb-query mongoose-schema