來自 mongoDB 的一項數(shù)據(jù)是{ "_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."}在我的代碼中,結(jié)構(gòu)是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如果是大寫的,就出問題了。我該如何解決這個問題?
1 回答

冉冉說
TA貢獻1877條經(jīng)驗 獲得超1個贊
字段標記中存在語法錯誤。刪除字段標記中 ':' 和 '"' 之間的空格。
TimeStamp string `bson:"timeStamp" json:"timeStamp"`
字段標記的語法是無情的。
與此問題分開,您可能應該添加
ID bson.ObjectId `bson:"_id"`
應用程序可以訪問該結(jié)構(gòu)的對象 id。
- 1 回答
- 0 關(guān)注
- 199 瀏覽
添加回答
舉報
0/150
提交
取消