将Mongo条目解析为结构体

huangapple go评论88阅读模式
英文:

Parsing mongo entries into struct

问题

我有一个具有以下模式的Mongo数据库:

{ 
  "_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 
}

我试图将其解析为具有以下结构的结构体:

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:"-"`
}

我正在使用mgo驱动程序。

现在的问题是,解析后,我无法正确解析的唯一属性是"len"(在Go结构体中为SplitLen)。

len被定义为:

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

我认为这与标签有关。另外,我想提到的是,相同的结构体被用于将值插入到MongoDB中。

任何帮助将不胜感激。

英文:

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.

答案1

得分: 2

如果在其他表示形式(例如JSON文本或数据库)中,数据元素的名称与结构字段名称不同,您必须告诉它们在字段标签中匹配的名称。

您告诉json包将JSON值"len"设置到名为SplitLen的字段中,通过在标签中包含json:"len"来指定不同的名称。

但是,您还没有告诉Mongo驱动程序也使用这个字段,该字段在您的MongoDB中可能被命名为"Len"(或"len")。您明确排除了可以通过名称进行“自动匹配”的字段:

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

正如Ainar-G建议的那样,您可以通过将"len"添加到bson标签值中来指定该字段,这将强制mgo驱动程序也使用SplitLen字段:

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

现在我看不到Len字段的任何用途,您应该将其删除以避免混淆,或者使用Len名称而不是SplitLen

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

If a data element appears under a different name in the other representation (e.g. json text or database) than what the struct field name is, you have to tell what name to match to the struct field in the field tag.

You told the json package to get/set the json value "len" into the field SplitLen which is named differently by including this in its tag: json:"len".

But you haven't told the mongo driver to also use this field which is most likely named "Len" (or "len") in your mongodb. You explicitly excluded the field that could be "auto-matched" by name:

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

As Ainar-G suggested, you can designate the field by adding the "len" to the bson tag value which will force the mgo driver to also use the SplitLen field:

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

And now I don't see any purpose for the Len field, you should remove it to avoid confusion, or use the Len name instead of SplitLen:

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

答案2

得分: 1

将字段名称也添加到BSON标签中:

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

否则,似乎会与被忽略的Len字段发生冲突。

英文:

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.

huangapple
  • 本文由 发表于 2015年8月10日 17:21:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/31915888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定