在Golang中的条件聚合查询

huangapple go评论76阅读模式
英文:

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{&quot;$group&quot;:
    bson.M{&quot;_id&quot;: bson.M{&quot;rollno&quot;:&quot;$atndnc.rollno&quot;}}
} 

The equivalent of your aggregation pipeline in Go is below :

<!-- language: lang-js -->

pipeline := []bson.M{ 
        bson.M{&quot;$match&quot;: 
            bson.M{&quot;stream&quot;: &quot;CS&quot;, &quot;semester&quot;:&quot;sem3&quot;, &quot;section&quot;:&quot;A&quot;}},
        bson.M{&quot;$unwind&quot;: &quot;$atndnc&quot;},
 		bson.M{&quot;$group&quot;: 
            bson.M{ &quot;_id&quot;: bson.M{&quot;rollno&quot;:&quot;$atndnc.rollno&quot;, &quot;name&quot;:&quot;$atndnc.name&quot;},     
                    &quot;count&quot;:bson.M{&quot;$sum&quot;:1},
                  },
        }, 
        bson.M{&quot;$project&quot;: 
            bson.M{&quot;_id&quot;:&quot;$_id.rollno&quot;, &quot;name&quot;:&quot;$_id.name&quot;, &quot;count&quot;:&quot;$count&quot;}},
        }

I would recommend to checkout MongoDB mgo driver page for examples and references.

huangapple
  • 本文由 发表于 2016年4月9日 18:43:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/36515708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定