【问题标题】:Cannot save structure into mongodb with golang (only empty records created)无法使用 golang 将结构保存到 mongodb(仅创建空记录)
【发布时间】:2015-03-15 04:17:44
【问题描述】:

我有以下结构

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

我想保存到 mongodb 中:

我创造它

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

并测试是否创建正确:

fmt.Println(r) 

这给了我期望的结果:

{b8da3f19d1318af6879976c1eea66c78c48e1144 1421417252 65072917F19D7F4C4B54C9C66A3EB31F77012981 127.0.0.1:65290}

然后我将它保存到mongo中:

h.c.Insert(r)

但在 mongo 我只看到空记录:

db.data.find()

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

上面代码中的h定义为

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

h.c = session.DB(DATABASE).C(COLLECTION)

【问题讨论】:

    标签: mongodb go database nosql


    【解决方案1】:

    您的记录文件需要公开以供其他包(如 MongoDB 包装器)查看。像这样重命名字段:

    type Result struct {
        Nid         string
        Timestamp   int64
        Hexhash     string
        Addr        string
    }
    

    【讨论】:

    • 非常感谢!我已经为此苦苦挣扎了好几个小时!
    • 7 年后,我只想说声谢谢!
    猜你喜欢
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 2020-07-17
    • 2018-10-13
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多