英文:
golang mongodb (mgo) is not inserting docs
问题
我在使用mgo将一个golang结构体持久化到mongodb时遇到了问题。
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()调用完成后,只有一个空文档被持久化到数据库中。
{"_id": ObjectId("561809d20037873154000003")}
我检查了结构体字段确实被设置了,而且不是空的。有什么想法为什么会发生这种情况。感谢您的提示 谢谢
英文:
Im having issues with persisting a golang struct in mongodb using mgo.
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)
the problem is that when the Insert() call completes, the only thing thats persisted in the DB is an empty doc
{u'_id': ObjectId('561809d20037873154000003')}
I check that the struct fields are indeed set, and not empty.
Any ideas as to why this is happening.
Hints are appreciated thank you
答案1
得分: 9
你需要通过将字段名称以大写字母开头来导出字段。
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"`
...
}
英文:
You need to export the fields by starting the field name with a capital letter.
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"`
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论