问题描述
这是一种愚蠢的语法错误,尝试了很多方法,只是无法实现,有人请帮助。
使用<$ c的MongoDB Go $ c> mgo ,我试图简化使用 $ ne
运算符,如下所示的代码,但不断收到编译语法错误:
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()
试图添加逗号,
在任何地方删除逗号,只是无法得到它的工作,总是得到像下面这样的编译语法错误:
mongodb / query.go:16:语法错误:意外{,期待逗号或}
mongodb / query.go:20:语法错误:意外},期望表达式
mongodb / query.go:21:语法错误:意外的},期望表达式
是一个映射类型,所以 bson.M {...}
是一个。如果键值对以多行写入,则每个键都必须以逗号结尾。有关详细信息,请参阅
另外,没有界面字面意思,请放下它。值 interface {}
类型可以保存/包装任何值,包括 bson.M
值。并且接口{}
值创建是自动的,您甚至不需要类型类型(这是一个切片),不以文字的结尾括号结束的行必须以逗号结尾,例如:
d:= bson.D {
{名称:fieldA,值:1},
{名称:fieldB,值:运行},
}
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
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 How to break a long line of code in Golang?
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"},
}
这篇关于MongoDB与Go一起使用mgo,运营商使用bson.M / bson.D总是出现语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!