golang mongodb(mgo)没有插入文档

时间:2015-10-09 19:01:07

标签: mongodb go mgo

我有使用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()调用完成时,DB中唯一存在的是空文档

{u'_id': ObjectId('561809d20037873154000003')}

我检查结构字段确实设置了,而不是空的。 关于为什么会发生这种情况的任何想法。 感谢提示:)谢谢

1 个答案:

答案 0 :(得分:5)

您需要通过以大写字母开头的字段名称来export字段。

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"`
  ...
}