Parsing mongo records in a structure

I have a mongo database with the 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 them into a structure with the 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 the problem is that after parsing only the attribute, I cannot parse it correctly, it is "len" (SplitLen in go struct).

len is defined as

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

      

I believe it has to do with tags. Also I would like to mention that the same structure was used to insert the value into mongodb.

Any help would be appreciated.

+3


source to share


2 answers


Add the field name to the BSON tag:

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

      



Otherwise, it appears that it will conflict with the field Len

being ignored.

+1


source


If the data item appears with a different name in a different view (like json text or database) than what the struct field name is, you must specify which name should match the struct field in the field tag.

You told the package json

to get / set the json value "len"

on a field SplitLen

that is named differently by including it in your: tag json:"len"

.

But you didn't tell the mango driver to also use this field, which is most likely called "len"

(or "len"

) in your mongodb. You have explicitly excluded a field that can be "automatically matched" by name:

Len uint64 `bson:"-" json:"-"`

      



As Ainar-G suggested, you can specify a field by adding a tag value "len"

to the tag bson

, which will force the mgo driver to use the field as well SplitLen

:

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

      

And now I don't see any purpose for the field Len

, you should remove it to avoid confusion, or use the name Len

instead SplitLen

:

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

      

+2


source







All Articles