dongzhi9192
2015-01-16 14:22
浏览 164

无法使用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)

图片转代码服务由CSDN问答提供 功能建议

我具有以下结构</ p>

  type结果struct {\  n nid字符串
时间戳int64 
十六进制字符串
 addr字符串
} 
 </ code> </ pre> 
 
 

我要保存到mongodb中的文件:</ p> \ n

我创建它</ p>

  r:=结果{hex_id,int64(msg.timestamp.Unix()),hexhash,msg.addr.String()} \  n </ code> </ pre> 
 
 

并测试其创建是否正确:</ p>

  fmt.Println(r)
 </ code>  </ pre> 
 
 

这给了我期望的结果:</ p>

{b8da3f19d1318af6879976c1eea66c78c48e1144 1421417252 65072917F19D7F4C4B54C9C66A3EB31F77012652 p> </ blockquote>

然后我将其保存到mongo:</ p>

  hcInsert(r)
 </ code> </ pre  > 
 
 

但是在mongo中,我只看到空记录:</ p>

db.data.find()</ p>

{“ _id”:ObjectId(“ 54b91a268da6c829a412cd4d”)} </ p> </ blockquote>

上面代码中的h定义为</ p> < pre> type处理程序结构{ storage map [string] Message new_msg chan Message new_inp chan Input c * mgo.Collection } </ code> </ pre>

和</ p>

  hc = session.DB(DATABASE).C(COLLECTION)
 </ code> </ pre> 
 </ div>

1条回答 默认 最新

相关推荐 更多相似问题