猿问

当键包含大写时,无法使用 mgo 检索值

来自 mongoDB 的一项数据是


{

    "_id" : ObjectId("5536def4e4b0644323e219a8"),

    "title" : "The Title",

    "description" : "The Description",

    "timeStamp" : "21/04/2015",

    "category" : "news",

    "url" : "http://www.example.com",

    "source" : "Evening Times",

    "mainStory" : "This is the main story."

}

在我的代码中,结构是


type NewsData struct {

    Title       string `bson: "title" json: "title"`

    TimeStamp   string `bson: "timeStamp" json: "timeStamp"`

    Description string `bson: "description" json: "description"`

    MainStory   string `bson: "mainStory" json:"mainStory"`

}

然后我用下面的代码来提取信息


err = conn.Find(nil).Select(bson.M{"title": 1, "timeStamp": 1, "description": 1, "mainStory": 1}).All(&result)

但是,当我打印result出来时,timeStampand 的mainStory值为空。我查了文档,说mgo是把key取小写的,所以mongoDB的key如果是大写的,就出问题了。


我该如何解决这个问题?


潇湘沐
浏览 186回答 1
1回答

冉冉说

字段标记中存在语法错误。删除字段标记中 ':' 和 '"' 之间的空格。TimeStamp   string `bson:"timeStamp" json:"timeStamp"`字段标记的语法是无情的。与此问题分开,您可能应该添加ID bson.ObjectId `bson:"_id"`应用程序可以访问该结构的对象 id。
随时随地看视频慕课网APP

相关分类

Go
我要回答