這可能是一個非常愚蠢的問題,但在網上搜索了兩個小時后,我在這里發布了這個問題。我嘗試學習 Go 并有一個非常簡單的“Hello World”應用程序,它使用 Mongo 作為數據源。我可以正常連接,我可以獲取數據,并且對象的數量還可以。問題是,我將數據映射到的對象具有空屬性,盡管 Mongo 中有數據。我在 mongo 中有一個非常簡單的集合,稱為具有 ~12k 記錄的站,如下所示:{ "_id" : ObjectId("563c8d56819c3c91076b7c13"), "nm" : "00000BE8" }{ "_id" : ObjectId("563c8d57819c3c91076b7c1a"), "nm" : "00000C01" }{ "_id" : ObjectId("563c8d58819c3c91076b7c1d"), "nm" : "00000C02" }{ "_id" : ObjectId("563c8d58819c3c91076b7c1f"), "nm" : "00000C31" }{ "_id" : ObjectId("563c8d5d819c3c91076b86c1"), "nm" : "000013E0" }{ "_id" : ObjectId("563c8d5d819c3c91076b86c5"), "nm" : "0000110B" }整個 Go 程序如下所示:package mainimport ( "log" "gopkg.in/mgo.v2")type StationConfig struct { id string `bson:"_id,omitempty"` name string `bson:"nm"`}func main() { session, err := mgo.Dial("192.168.1.41") if err != nil { panic(err) } defer session.Close() c := session.DB("metos").C("stations") var stationConfigs []StationConfig err = c.Find(nil).Limit(100).All(&stationConfigs) if err != nil { log.Fatal(err) } log.Printf("Found document: %+v\n", stationConfigs)}但是當我運行程序時,_id 和 nm 的值沒有分配給各自的結構屬性,我得到以下輸出:Found document: [{id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} ... and so on ... ]我錯過了什么?
1 回答
一只名叫tom的貓
TA貢獻1906條經驗 獲得超3個贊
我不熟悉 MongoDB Go API,但我認為您的結構字段應該是公開的,以便 MongoDB API 能夠填充它們。
嘗試公開您的字段,看看它是否有效:
type StationConfig struct {
ID string `bson:"_id,omitempty"`
Name string `bson:"nm"`
}
- 1 回答
- 0 關注
- 189 瀏覽
添加回答
舉報
0/150
提交
取消
