英文:
conditional aggregate query in golang
问题
我正在使用Golang和MongoDB。
我的attendance
集合如下所示:
{
"_id" : ObjectId("5708156b51230e8edcb01fd1"),
"college_id" : "tisl",
"stream" : "CS",
"semester" : "sem3",
"section" : "A",
"subject" : "PH301",
"date" : ISODate("2016-04-08T20:32:42.547Z"),
"teacher" : "Chandra Kanta Bhattacharya",
"atndnc" : [
{
"rollno" : "13000112115",
"name" : "Md Hossain Ahamed",
"attend" : true
},
{
"rollno" : "13000112116",
"name" : "Md Sajid Tagala",
"attend" : true
},
{
"rollno" : "13000112117",
"name" : "Nabarun Roy",
"attend" : false
},
{
"rollno" : "13000112118",
"name" : "Nikunj Mundra",
"attend" : true
}
]
}
我想要以百分比的形式为每个学生生成报告,作为对象数组,如下所示:
[{"rollno" : "13000112115",
"name" : "Md Hossain Ahamed",
"prcntg" : 80},
{
"rollno" : "13000112116",
"name" : "Md Sajid Tagala",
"prcntg" : 60
},
{
"rollno" : "13000112117",
"name" : "Nabarun Roy",
"prcntg" : 90
},
{
"rollno" : "13000112118",
"name" : "Nikunj Mundra",
"prcntg" : 65
}]
我的条件将是以下内容:
college_id、stream、semester、section、subject、startingdate和enddate
英文:
i am using golang and mongodb.
my attendance
collection looks like this -
{
"_id" : ObjectId("5708156b51230e8edcb01fd1"),
"college_id" : "tisl",
"stream" : "CS",
"semester" : "sem3",
"section" : "A",
"subject" : "PH301",
"date" : ISODate("2016-04-08T20:32:42.547Z"),
"teacher" : "Chandra Kanta Bhattacharya",
"atndnc" : [
{
"rollno" : "13000112115",
"name" : "Md Hossain Ahamed",
"attend" : true
},
{
"rollno" : "13000112116",
"name" : "Md Sajid Tagala",
"attend" : true
},
{
"rollno" : "13000112117",
"name" : "Nabarun Roy",
"attend" : false
},
{
"rollno" : "13000112118",
"name" : "Nikunj Mundra",
"attend" : true
}
]
}
I want to get report for each student in percentage as an array of object like:
[{"rollno" : "13000112115",
"name" : "Md Hossain Ahamed",
"prcntg" : 80},
{
"rollno" : "13000112116",
"name" : "Md Sajid Tagala",
"prcntg" : 60
},
{
"rollno" : "13000112117",
"name" : "Nabarun Roy",
"prcntg" : 90
},
{
"rollno" : "13000112118",
"name" : "Nikunj Mundra",
"prcntg" : 65
}]
and my conditions will be the following
college_id,stream,semester,section,subject,startingdate and enddate
答案1
得分: 1
这是由于错误的bson.M用法导致的。如果是字符串(单个值),则不需要创建bson映射。所以你可以将其更新为:
bson.M{"$group":
bson.M{"_id": bson.M{"rollno":"$atndnc.rollno"}}
}
你的Go聚合管道的等效代码如下:
pipeline := []bson.M{
bson.M{"$match": bson.M{"stream": "CS", "semester":"sem3", "section":"A"}},
bson.M{"$unwind": "$atndnc"},
bson.M{"$group": bson.M{
"_id": bson.M{"rollno":"$atndnc.rollno", "name":"$atndnc.name"},
"count": bson.M{"$sum":1},
}},
bson.M{"$project": bson.M{"_id":"$_id.rollno", "name":"$_id.name", "count":"$count"}},
}
我建议查看MongoDB mgo驱动程序页面上的示例和参考资料。
英文:
> bson.M{"$group":bson.M{"_id":{"rollno":bson.M{"$atndnc.rollno"}}}} in this line i am getting that error
This is due to the incorrect bson.M usage. You don't need to create a bson map if its a string (single value). So you could update that to:
<!-- language: lang-js -->
bson.M{"$group":
bson.M{"_id": bson.M{"rollno":"$atndnc.rollno"}}
}
The equivalent of your aggregation pipeline in Go is below :
<!-- language: lang-js -->
pipeline := []bson.M{
bson.M{"$match":
bson.M{"stream": "CS", "semester":"sem3", "section":"A"}},
bson.M{"$unwind": "$atndnc"},
bson.M{"$group":
bson.M{ "_id": bson.M{"rollno":"$atndnc.rollno", "name":"$atndnc.name"},
"count":bson.M{"$sum":1},
},
},
bson.M{"$project":
bson.M{"_id":"$_id.rollno", "name":"$_id.name", "count":"$count"}},
}
I would recommend to checkout MongoDB mgo driver page for examples and references.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论