使用mgo在Go语言中操作MongoDB时,使用bson.M / bson.D操作符总是出现语法错误。

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

MongoDB in Go with mgo, operators with bson.M / bson.D always got syntax error

问题

这是一个愚蠢的语法错误,尝试了很多方法,但就是无法让它工作,有人可以帮忙吗?

在Go中使用mgo操作MongoDB,我试图简化使用$ne操作符的方法,代码如下,但一直得到编译时的语法错误:

第15行:convIter := Session.Copy().DB("").C("convs").Find(bson.M {
第16行:  "conversationStatus": interface{} {
第17行:    bson.M {
第18行:      "$ne": "DESTROYED"
第19行:    },
第20行:  },
第21行:}).Iter()

尝试添加逗号,或者移除逗号,但就是无法让它工作,总是得到以下编译时的语法错误:

mongodb/query.go:16: 语法错误:意外的{,期望逗号或}
mongodb/query.go:20: 语法错误:意外的},期望表达式
mongodb/query.go:21: 语法错误:意外的},期望表达式
英文:

It is kind of stupid syntax error, tried tons of ways, just couldn't get it work, someone please help.

MongoDB in Go with mgo, I just tried to simplify use the $ne operator, code like below, but kept getting compile syntax error:

line 15: convIter := Session.Copy().DB("").C("convs").Find(bson.M {
line 16:   "conversationStatus": interface{} {
line 17:     bson.M {
line 18:       "$ne": "DESTROYED"
line 19:     },
line 20:   },
line 21: }).Iter()

Tried to add comma , remove comma everywhere, just couldn't get it work, always got such compile syntax error like below:

mongodb/query.go:16: syntax error: unexpected {, expecting comma or }
mongodb/query.go:20: syntax error: unexpected }, expecting expression
mongodb/query.go:21: syntax error: unexpected }, expecting expression

答案1

得分: 9

bson.M 是一个映射类型,所以 bson.M{ ... } 是一个映射字面量。如果键值对写在多行中,每行都必须以逗号结尾。详细信息请参见 https://stackoverflow.com/questions/34846848/how-to-break-a-long-line-of-code-in-golang/34848928#34848928

另外,没有"interface"字面量,可以省略它。interface{}类型的值可以包含/封装任何值,包括bson.M值。而且interface{}值的创建是自动的,甚至不需要类型转换

正确的语法:

convIter := Session.Copy().DB("").C("convs").Find(bson.M{
	"conversationStatus": bson.M{
		"$ne": "DESTROYED",
	},
}).Iter()

类似地,如果使用bson.D类型(它是一个切片),没有以字面量的闭合括号结尾的行必须以逗号结尾,例如:

d := bson.D{
	{Name: "fieldA", Value: 1},
	{Name: "fieldB", Value: "running"},
}
英文:

bson.M is a map type, so the bson.M{ ... } is a map literal. If key-value pairs are written in multiple rows, each has to end with a comma. For details, see https://stackoverflow.com/questions/34846848/how-to-break-a-long-line-of-code-in-golang/34848928#34848928

Also there is no "interface" literal, drop that. A value of interface{} type can hold / wrap any value, including a bson.M value. And the interface{} value creation is automatic, you don't even need a type conversion.

Correct syntax:

convIter := Session.Copy().DB("").C("convs").Find(bson.M{
	"conversationStatus": bson.M{
		"$ne": "DESTROYED",
	},
}).Iter()

Similarly, if you use the bson.D type (which is a slice), lines not ending with the closing bracket of the literal has to end with a comma, e.g.:

d := bson.D{
	{Name: "fieldA", Value: 1},
	{Name: "fieldB", Value: "running"},
}

huangapple
  • 本文由 发表于 2017年3月2日 19:51:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/42554759.html
匿名

发表评论

匿名网友

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

确定