douyi4205 2015-10-09 19:01
浏览 51
已采纳

golang mongodb(mgo)未插入文档

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条回答 默认 最新

  • doujingdai5521 2015-10-09 19:19
    关注

    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"`
      ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题