duanlin1931 2015-08-10 09:21
浏览 76
已采纳

将mongo条目解析为struct

I have mongo database with following schema

{ 
  "_id" : ObjectId("55c8526d8c16598efb5ee1e6"), 
  "guid" : "72811d52b48379e72c8fdd11aa09cb8b", 
  "blkid" : 1, 
  "vblkid" : 0, 
  "spltid" : 0, 
  "cmpr" : false, 
  "encr" : false,
  "chksum" : "",
  "dup" : false,
  "cid" : 1,
  "off" : 524508,
  "len" : 524408,
  "incr" : 0,
  "fBackupID" : 0,
  "vid" : 0,
  "plugInType" : 0, 
  "blkType" : 0, 
  "alen" : 0 
}

and I am trying to parse these into a struct with following structure:

type VhfsBlockMD struct {
    GUID       string `json:"guid"`
    BlkID      int    `bson:",minsize" json:"blkid"`
    VBlkID     int    `bson:",minsize" json:"vblkid"`
    SpltID     int    `bson:",minsize" json:"spltid"`
    Cmpr       bool   `json:"cmpr" `
    Encr       bool   `json:"encr"`
    Blksum     string `bson:"blksum,omitempty" json:"blksum,omitempty"`
    Chksum     string `json:"chksum"`
    Dup        bool   `json:"dup"`
    Cid        int    `bson:",minsize" json:"cid"`
    SplitLen   int    `bson:",minsize" json:"len"`
    Off        int64  `bson:",minsize" json:"off"`
    Incr       int    `bson:",minsize" json:"incr"`
    CDup       bool   `bson:"cdup,omitempty" json:"cdup,omitempty"`
    FBackupID  int    `bson:"fBackupID" json:"fBackupID"`
    Vid        int    `bson:"vid" json:"vid"`
    PlugInType int    `bson:"plugInType" json:"plugInType"`
    BlkType    int    `bson:"blkType" json:"blkType"`
    Alen       int    `bson:"alen" json:"alen"`
    IsValid    int    `bson:"-" json:"-"`
    Len        uint64 `bson:"-" json:"-"`
}

I am using mgo driver.

Now problem is that after parsing only attribute i am not able to parse correctly is "len" (SplitLen in go struct).

len is defined as

SplitLen int `bson:",minsize" json:"len"`

I believe it has something to do with tags. Also i would lke to mention that same struct was used to insert value into mongodb.

Any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • dongshie8450 2015-08-10 10:51
    关注

    Add the field name to the BSON tag as well:

    SplitLen int `bson:"len,minsize" json:"len"
    

    Otherwise it seems that it will conflict with the Len field that is ignored.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址