2017-10-14 44 views

回答

3

如果你看一下mgo包的文件,你會看到的結構有標註有`bson:"fieldName``json:"fieldName"`。您可以看到一個示例here

原因是mongo使用bson序列化格式而不是json通過線路發送結構。 bsonjson非常相似,但它是一種二進制格式,並且已針對數據庫等存儲系統進行了優化。

所以更新您的結構看起來像這樣:

type Event struct { 
    Id   string  `bson:"id"` 
    CreationDate time.Time `bson:"creationTime"` 
    CreatorId string  `bson:"creatorId"` 
    Place  string  `bson:"place"` 
    ActivityId string  `bson:"activityId"` 
    Time   time.Time `bson:"time"` 
    Lang   string  `bson:"lang"` 
} 
0

實際上,你可以同時使用這兩種jsonbson標籤。

type Event struct { 
    Id   string `json:"id" bson:"id"` 
    CreationDate time.Time `json:"creationTime" bson:"creationTime"` 
    CreatorId string `json:"creatorId" bson:"creatorId"` 
    Place  string `json:"place" bson:"place"` 
    ActivityId string `json:"activityId" bson:"activityId"` 
    Time   time.Time `json:"time" bson:"time"` 
    Lang   string `json:"lang" bson:"lang"` 
}