![](/img/trans.png)
[英]How would I go about inserting a subdocument in Mongodb in Golang / mgo?
[英]golang mongodb (mgo) is not inserting docs
我在使用mgo將mongodb中保留golang結構時遇到問題。
type AN_Track_Log struct {
Id bson.ObjectId `bson:"_id,omitempty"`
user_session_id_str string `bson:"user_session_id_str"`
googleanaly_pixel_id_str string `bson:"googleanaly_pixel_id_str"`
perfaud_pixel_id_str string `bson:"perfaud_pixel_id_str"`
site_id_str string `bson:"site_id_str"`
metric_str string `bson:"metric_str"`
value_str string `bson:"value_str"`
event_str string `bson:"event_str"`
location_id_str string `bson:"location_id_str"`
referer_str string `bson:"referer_str"`
track_origin_str string `bson:"track_origin_str"`
fingerprint_str string `bson:"fingerprint_str"`
...
}
p_track_log.Id = bson.NewObjectId()
err := p_mongodb_coll.Insert(&p_track_log)
問題是,在完成Insert()調用后,唯一保留在數據庫中的是空文檔
{u'_id': ObjectId('561809d20037873154000003')}
我檢查struct字段是否確實已設置,並且不為空。 關於為什么發生這種情況的任何想法。 提示表示贊賞:)謝謝
您需要通過以大寫字母開頭的字段名稱來導出字段。
type AN_Track_Log struct {
Id bson.ObjectId `bson:"_id,omitempty"`
User_session_id_str string `bson:"user_session_id_str"`
Googleanaly_pixel_id_str string `bson:"googleanaly_pixel_id_str"`
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.