dongzhi9192 2015-01-16 14:22
浏览 167
已采纳

无法使用golang将结构保存到mongodb中(仅创建了空记录)

I have the following structure

type Result struct {
    nid         string
    timestamp   int64
    hexhash     string
    addr        string
}

which I want to save into mongodb:

I create it

r := Result{hex_id, int64(msg.timestamp.Unix()), hexhash, msg.addr.String()}

And test if it is created correctly:

fmt.Println(r) 

Which gives me result I'm expecting:

{b8da3f19d1318af6879976c1eea66c78c48e1144 1421417252 65072917F19D7F4C4B54C9C66A3EB31F77012981 127.0.0.1:65290}

Then I save it into mongo:

h.c.Insert(r)

But in mongo i see only empty records:

db.data.find()

{ "_id" : ObjectId("54b91a268da6c829a412cd4d") }

The h in the code above defined as

type Handler struct {
    storage     map[string]Message
    new_msg     chan Message
    new_inp     chan Input
    c           *mgo.Collection
}

and

h.c = session.DB(DATABASE).C(COLLECTION)
  • 写回答

1条回答 默认 最新

  • dongzaobei0942 2015-01-16 14:37
    关注

    The fileds of your record need to be public for other packages (like the MongoDB wrapper) to see them. Rename the fields like this:

    type Result struct {
        Nid         string
        Timestamp   int64
        Hexhash     string
        Addr        string
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误