2015-06-30 94 views
0

从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输出时,值为timeStampmainStory为空。我检查了这些文件,它说mgo把钥匙当作小写字母,所以当mongoDB中的钥匙包含大写字母时,这将是一个问题。

我该如何解决这个问题?

回答

1

字段标记中存在语法错误。取出之间的空间“:”和““”在外地标签

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

的语法场标签是无情

从这个问题分开,很可能需要添加

ID bson.ObjectId `bson:"_id"` 

给结构应用程序可以访问对象ID。