如何从学生合约中获取特定学生的论文失败次数?

时间:2017-05-09 13:51:12

标签: mongodb mongodb-query aggregation-framework

我有以下收藏

{
"name" : "arjun",
"Roll NO" : 123,
"timestamp" : {
    "full" : "Wed Mar 23 10:38:12 UTC 2016"
}
"failed_in" : "Mathematics",

}

{
"name" : "Ram",
"Roll NO" : 103,
"timestamp" : {
    "full" : "Wed Mar 23 10:38:12 UTC 2016"
}
"failed_in" : "English",

}

{
"name" : "Ram",
"Roll NO" : 103,
"timestamp" : {
    "full" : "Wed Mar 23 10:38:12 UTC 2016"
}
"failed_in" : "Science",

}

{
"name" : "arjun",
"Roll NO" : 123,
"timestamp" : {
    "full" : "Thur April 18 10:38:12 UTC 2016"
}
"failed_in" : "English",

}

{
"name" : "arjun",
"Roll NO" : 123,
"timestamp" : {
    "full" : "Wed Mar 23 10:38:12 UTC 2016"
}
"failed_in" : "Mathematics",

}

我想要输出如下排序依据日期。

{
'name':'arjun',
failed_in:[{'mathematics':2},{'english':1}]

}

{
'name':'ram',
failed_in:[{'science':1},{'english':1}]

} 你能不能给它查询?我以一些可怕的方式对此进行了尝试,但是没有它们有效。

1 个答案:

答案 0 :(得分:0)

以下是你如何做到这一点。我已经对它进行了测试并且工作得很完美。

db.getCollection('tests')。aggregate([{$ match:{“Roll NO”:103}}, {$ group:{             _id:“$ Roll NO”,             failed_in:{$ push:“$ failed_in”}         }}, ])