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条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源