Golang/mgo:无法从MongoDB文档中检索int字段的值

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

Golang/mgo: Cannot retrieve value of int field from MongoDB document

问题

我正在查询一个包含整数值的集合,并将结果文档加载到这个结构中:

type Subscription struct {
    Id bson.ObjectId "_id,omitempty"
    Listen string
    Job string
    TimeoutSeconds int
    Data string
}


var subscription Subscription

subscriptions := subscriptionsCol.Find(bson.M{"listen": "example_channel"}).Iter()
for subscriptions.Next(&subscription) {
    log("Pending job: %s?%s (timeout: %d)\n",
            subscription.Job, 
            subscription.Data, 
            subscription.TimeoutSeconds)
}

这是phpMoAdmin显示给我的内容:

[_id] => MongoId Object (
    [$id] => 502ed8d84eaead30a1351ea7
)
[job] => partus_test_job_a
[TimeoutSeconds] => 30
[listen] => partus.test
[data] => a=1&b=9

令我困惑的是,subscription.TimeoutSeconds 总是包含0,而我确定我在集合中插入的文档中有30。

其他所有值都能正确检索。

int类型有什么问题?

英文:

I am querying a collection that includes an integer value among it's values, and loading resulting documents into this struct:

type Subscription struct {
    Id bson.ObjectId "_id,omitempty"
    Listen string
    Job string
    TimeoutSeconds int
    Data string
}


var subscription Subscription

subscriptions := subscriptionsCol.Find(bson.M{"listen": "example_channel"}).Iter()
for subscriptions.Next(&subscription) {
    log("Pending job: %s?%s (timeout: %d)\n",
            subscription.Job, 
            subscription.Data, 
            subscription.TimeoutSeconds)
}

This is what phpMoAdmin shows me:

[_id] => MongoId Object (
    [$id] => 502ed8d84eaead30a1351ea7
)
[job] => partus_test_job_a
[TimeoutSeconds] => 30
[listen] => partus.test
[data] => a=1&b=9

It puzzles me that subscription.TimeoutSeconds contains always 0, when I'm positive I have 30 in the document I inserted in the collection.

All other values are retrieved OK.

What's wrong with the int type?

答案1

得分: 4

你尝试设置该字段的“key”值了吗?

Unmarshal

> 小写的字段名被用作每个导出字段的键,但是可以使用相应的字段标签来更改此行为。

type Subscription struct {
    Id              bson.ObjectId    "_id,omitempty"
    Listen          string
    Job             string
    TimeoutSeconds  int     "TimeoutSeconds"
    Data            string
}

其他字段正常工作是因为它们的小写值与集合中的Mongo字段匹配,而TimeoutSeconds使用了TitleCase。发生的情况是int字段被保留为其零值,因为Unmarshal无法将字段映射到它。

英文:

Have you tried setting the "key" value for that field?

Unmarshal

> The lowercased field name is used as the key for each exported field,
> but this behavior may be changed using the respective field tag.

type Subscription struct {
    Id              bson.ObjectId    "_id,omitempty"
    Listen          string
    Job             string
    TimeoutSeconds  int     "TimeoutSeconds"
    Data            string
}

The other fields are working fine because their lowercase value matches your Mongo fields in the collection, whereas TimeoutSeconds is using the TitleCase. What is happening is the int field is being left at its zero value, since the Unmarshal can't map a field to it.

答案2

得分: 0

当进行数据解组时,支持多个键。以下是一些示例:

type T struct {
    A bool
    B int    "myb"
    C string "myc,omitempty"
    D string `bson:",omitempty" json:"jsonkey"`
    E int64  ",minsize"
    F int64  "myf,omitempty,minsize"
}

在编组期间,一个键值对的一般规范是:

"[<key>][,<flag1>[,<flag2>]]"

`(...) bson:"[<key>][,<flag1>[,<flag2>]]" (...)`

GO提供了对特定关键字的支持,如bson(用于mongo键)和json(用于设置响应中的json键)。

请查看Marshal GO Reference获取更多信息。

类似地,还有一些框架提供了进一步的选项来定义解析之前的键。例如,在sql jinzhu github库中提供了设置默认值、映射列ID等功能。

任何人都可以使用此功能并提供定制支持。

英文:

When UnMarshalling data, there are multiple keys that are supported.
Below are some examples:

type T struct {
    A bool
    B int    &quot;myb&quot;
    C string &quot;myc,omitempty&quot;
    D string `bson:&quot;,omitempty&quot; json:&quot;jsonkey&quot;`
    E int64  &quot;,minsize&quot;
    F int64  &quot;myf,omitempty,minsize&quot;
}

The general spec for 1 key-value pair during marshal is :

&quot;[&lt;key&gt;][,&lt;flag1&gt;[,&lt;flag2&gt;]]&quot;

`(...) bson:&quot;[&lt;key&gt;][,&lt;flag1&gt;[,&lt;flag2&gt;]]&quot; (...)` 

GO provides support for particular keywords like bson (for mongo keys) and json for setting the json key in a resposne.

Check the Marshal GO Reference for more information.

Similarly there are some frameworks which provide further options to define the keys befor parsing. For example, in sql jinzhu github library gives support for setting default values, column ids to map, etc.

Anyone can use this feature and provide customized support.

huangapple
  • 本文由 发表于 2012年8月18日 08:01:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/12014597.html
匿名

发表评论

匿名网友

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

确定