在golang/mongodb聚合查询中,复合字面量中缺少类型。

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

missing type in composite literal in golang/mongodb aggregate query

问题

我可以帮你翻译Mongo查询和Go代码。以下是翻译的结果:

Mongo查询:

aggregate([
    {$match: {$and :
        [
            {"stream" : "CS"},
            {"semester" : "sem3"},
            {"section" : "A"}
        ]
    }},
    {$unwind:  '$atndnc'},
    { $group: { _id:{rollno: "$atndnc.rollno",attend:"$atndnc.attend"},count: { $sum: 1 }}},
    {  $project:
    {  _id: '$_id.rollno',
        'attend' : '$_id.attend',
        'count' : '$count'
    }}
])

Go代码:

cond := []bson.M{
    bson.M{"$match": bson.M{"$and": []interface{}{
        bson.M{"stream": srchobj.Stream},
        bson.M{"semester": srchobj.Semester},
        bson.M{"section": srchobj.Section},
        bson.M{"college_id": srchobj.College_id},
        bson.M{"date": bson.M{"$gt": srchobj.Startdate, "$lt": srchobj.Enddate}},
    }}},
    bson.M{"$unwind": "$atndnc"},
    bson.M{"$group": bson.M{"_id": bson.M{"rollno": "$atndnc.rollno", "attend": "$atndnc.attend"}, "count": bson.M{"$sum": 1}}},
    bson.M{"$project": bson.M{"_id": "$_id.rollno", "count": "$_id.count"}},
}

在这段代码中,你遇到了一个错误:"missing type in composite literal",出现在以下这行代码中:

bson.M{"$group":bson.M{"_id":bson.M{"rollno":bson.M{"$atndnc.rollno"},"attend":bson.M{"$atndnc.attend"}},"count":bson.M{"$sum":1}}},

这个错误是因为你在bson.M中使用了$atndnc.rollno$atndnc.attend作为键名,但是它们应该是字符串而不是bson.M类型。你需要将它们改为字符串,如下所示:

bson.M{"$group":bson.M{"_id":bson.M{"rollno":"$atndnc.rollno","attend":"$atndnc.attend"},"count":bson.M{"$sum":1}}},

这样修改后的代码应该就没有错误了。希望对你有帮助!

英文:

I want to write mongo query in golang. my mongo query is -

aggregate([
    {$match: {$and :
        [
            {"stream" : "CS"},
            {"semester" : "sem3"},
            {"section" : "A"}
        ]

    }},
    {$unwind:  '$atndnc'},
    { $group: { _id:{rollno: "$atndnc.rollno",attend:"$atndnc.attend"},count: { $sum: 1 }}},
    {  $project:
    {  _id: '$_id.rollno',
        'attend' : '$_id.attend',
        'count' : '$count'
    }}
])

And my Go code is -

cond:=[]bson.M{
	bson.M{"$match": bson.M{"$and ":[]interface{}{
		bson.M{"stream" : srchobj.Stream},
		bson.M{"semester" : srchobj.Semester},
		bson.M{"section" : srchobj.Section},
		bson.M{"college_id":srchobj.College_id},
		bson.M{"date":bson.M{"$gt":srchobj.Startdate,"$lt":srchobj.Enddate}}}}},
	bson.M{"$unwind":  "$atndnc"},
	bson.M{"$group":bson.M{"_id":{"rollno":bson.M{"$atndnc.rollno"},"attend":bson.M{"$atndnc.attend"}},"count":bson.M{"$sum":1}}},
	bson.M{"$project":bson.M{"_id":"$_id.rollno","count":"$_id.count"}}}

but it give the error "missing type in composite literal" in

	bson.M{"$group":bson.M{"_id":{"rollno":bson.M{"$atndnc.rollno"},"attend":bson.M{"$atndnc.attend"}},"count":bson.M{"$sum":1}}},

in this line.what should i do now?

答案1

得分: 2

你的$group查询中的一组大括号缺少类型声明:

{"rollno":bson.M{"$atndnc.rollno"},"attend":bson.M{"$atndnc.attend"}}

我认为应该是:

bson.M{"rollno":bson.M{"$atndnc.rollno"},"attend":bson.M{"$atndnc.attend":nil}}

还有一些其他的初始化问题,比如只用一个字符串键进行初始化(记住,bson.M只是map[string]interface{}的别名)。

英文:

You have a missing type declaration on a set of braces in your $group query:

I would assume should be:

there are also a few other initialization things like initializations with just a string key (remember, a bson.M is just an alias for map[string]interface{}

答案2

得分: 0

感谢您的支持。通过这样做,我得到了所需的输出。

cond := []bson.M{
	bson.M{"$match": bson.M{
		"stream":     srchobj.Stream,
		"semester":   srchobj.Semester,
		"section":    srchobj.Section,
		"college_id": srchobj.College_id,
		"date": bson.M{
			"$gt": srchobj.Startdate,
			"$lt": srchobj.Enddate},
	},
	},
	bson.M{"$unwind": "$atndnc"},
	bson.M{"$group": bson.M{
		"_id": bson.M{
			"rollno": "$atndnc.rollno",
			"attend": "$atndnc.attend",
			"name":   "$atndnc.name",
		},
		"count": bson.M{"$sum": 1},
	},
	},
	bson.M{"$project": bson.M{
		"rollno": "$_id.rollno",
		"name":   "$_id.name",
		"count":  "$count",
		"attend": "$_id.attend",
	},
	},
	bson.M{"$sort": bson.M{"rollno": 1}},
}
英文:

Thanks for your support.I got the desired output by doing this.

cond :=[]bson.M{
	bson.M{"$match": bson.M{
			"stream" : srchobj.Stream,
			"semester" : srchobj.Semester,
			"section" : srchobj.Section,
			"college_id":srchobj.College_id,
			"date":bson.M{
				"$gt":srchobj.Startdate,
				"$lt":srchobj.Enddate},
	 },
	},
	bson.M{"$unwind":  "$atndnc"},
	bson.M{"$group":bson.M{
		"_id":bson.M{
			"rollno":"$atndnc.rollno",
			"attend":"$atndnc.attend",
			"name":"$atndnc.name",
			},
		"count":bson.M{"$sum":1},
		},
	},
	bson.M{"$project":bson.M{
		"rollno":"$_id.rollno",
		"name":"$_id.name",
		"count":"$count",
		"attend":"$_id.attend",
		},
	},
	bson.M{"$sort":bson.M{"rollno":1}},
}

huangapple
  • 本文由 发表于 2016年4月13日 02:15:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/36581072.html
匿名

发表评论

匿名网友

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

确定