dongzhi9192
dongzhi9192
2015-01-16 14:22

无法使用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 dongzaobei0942 6年前

    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
    }
    
    点赞 评论 复制链接分享

相关推荐