golang mongodb(mgo)不插入文档 [英] golang mongodb (mgo) is not inserting docs
本文介绍了golang mongodb(mgo)不插入文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有使用mgo在mongodb中持久化一个golang结构的问题。
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)
问题是,当Insert()调用完成时,数据库中唯一持久的是一个空文档
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')}
我检查struct字段的确设置了,而不是空的。
关于为什么会发生这种情况的任何想法。
提示非常感谢:)谢谢
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
推荐答案
您需要导出这些字段,并以大写字母开始字段名称。
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"`
...
}
这篇关于golang mongodb(mgo)不插入文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文