Golang中的MongoDB(mgo)无法插入文档。

huangapple go评论122阅读模式
英文:

golang mongodb (mgo) is not inserting docs

问题

我在使用mgo将一个golang结构体持久化到mongodb时遇到了问题。

  1. type AN_Track_Log struct {
  2. Id bson.ObjectId `bson:"_id,omitempty"`
  3. user_session_id_str string `bson:"user_session_id_str"`
  4. googleanaly_pixel_id_str string `bson:"googleanaly_pixel_id_str"`
  5. perfaud_pixel_id_str string `bson:"perfaud_pixel_id_str"`
  6. site_id_str string `bson:"site_id_str"`
  7. metric_str string `bson:"metric_str"`
  8. value_str string `bson:"value_str"`
  9. event_str string `bson:"event_str"`
  10. location_id_str string `bson:"location_id_str"`
  11. referer_str string `bson:"referer_str"`
  12. track_origin_str string `bson:"track_origin_str"`
  13. fingerprint_str string `bson:"fingerprint_str"`
  14. ...
  15. }
  16. p_track_log.Id = bson.NewObjectId()
  17. err := p_mongodb_coll.Insert(&p_track_log)

问题是,当Insert()调用完成后,只有一个空文档被持久化到数据库中。

  1. {"_id": ObjectId("561809d20037873154000003")}

我检查了结构体字段确实被设置了,而且不是空的。有什么想法为什么会发生这种情况。感谢您的提示 Golang中的MongoDB(mgo)无法插入文档。 谢谢

英文:

Im having issues with persisting a golang struct in mongodb using mgo.

  1. type AN_Track_Log struct {
  2. Id bson.ObjectId `bson:"_id,omitempty"`
  3. user_session_id_str string `bson:"user_session_id_str"`
  4. googleanaly_pixel_id_str string `bson:"googleanaly_pixel_id_str"`
  5. perfaud_pixel_id_str string `bson:"perfaud_pixel_id_str"`
  6. site_id_str string `bson:"site_id_str"`
  7. metric_str string `bson:"metric_str"`
  8. value_str string `bson:"value_str"`
  9. event_str string `bson:"event_str"`
  10. location_id_str string `bson:"location_id_str"`
  11. referer_str string `bson:"referer_str"`
  12. track_origin_str string `bson:"track_origin_str"`
  13. fingerprint_str string `bson:"fingerprint_str"`
  14. ...
  15. }
  16. p_track_log.Id = bson.NewObjectId()
  17. 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

  1. {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 Golang中的MongoDB(mgo)无法插入文档。 thank you

答案1

得分: 9

你需要通过将字段名称以大写字母开头来导出字段。

  1. type AN_Track_Log struct {
  2. Id bson.ObjectId `bson:"_id,omitempty"`
  3. User_session_id_str string `bson:"user_session_id_str"`
  4. Googleanaly_pixel_id_str string `bson:"googleanaly_pixel_id_str"`
  5. ...
  6. }
英文:

You need to export the fields by starting the field name with a capital letter.

  1. type AN_Track_Log struct {
  2. Id bson.ObjectId `bson:"_id,omitempty"`
  3. User_session_id_str string `bson:"user_session_id_str"`
  4. Googleanaly_pixel_id_str string `bson:"googleanaly_pixel_id_str"`
  5. ...
  6. }

huangapple
  • 本文由 发表于 2015年10月10日 03:01:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/33045404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定