douqian1517 2015-02-24 19:12
浏览 51
已采纳

如何防止mgo将int编组为float64

I have unknown json structure data stored in mongodb. They have the fields to present the unix time like this:

"date": 1424803567,

I am using mgo to load them to the bson.M.

var result bson.M
iter := c.Find(q).Iter()
for iter.Next(&result) {

Those unix time fields have turned to the fload64 instead of the int.

 "date": 1.424728798e+09,

So, how to prevent that happens in the case above? thanks!

  • 写回答

1条回答 默认 最新

  • dsadasd12132 2015-02-24 22:04
    关注

    Mgo does not unmarshal an integer to a float unless destination value is explicitly typed as a float by the application. Mgo is returning a float value here because the value stored in the database is a float.

    You can unmarshal the float value to an integer by specifying the type using a struct:

    var result struct {
      Date int64 `bson:"date"`
    }
    for iter.Next(&result) {
      ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?