英文:
mgo convert bson.objectId to string(hex) in html template
问题
我知道这个问题可能与这个问题重复了。但直到现在还没有得到满意的答案。我真的希望引起一些关注,尽快找到解决方案。所以我请求你不要关闭这个问题,除非你有解决方案并在之前的问题中回答了它
我将描述一下这个问题:
我有一个数据结构存储在mongodb中,众所周知,mongodb的_id
是一个bson.ObjectId
类型,我可以通过类似以下的方式检索它:
type Data struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Content string `bson:"content"`
}
然后,我通过特定的查询条件找到了一个Data
的切片,并在http/template
中使用该切片进行前端视图的渲染。为了操作切片中的每个项,我想使用Id
字段,但是当使用以下代码时:
{{range $key, $value := .DataSlice}}
<td>{{$value.Id}}</td>
{{end}}
它只会给出一个类似于ObjectIdHex("550146d1b51bc1c208d1924d")
的字符串,而不是550146d1b51bc1c208d1924d
,后者更好且更容易使用。
在重复的问题中,提问者说他找到了一种解决方法,即"在原始数据结构中添加一个Id_String
"。但我真的不知道该怎么做?这是指在检索后分配它吗?由于我使用一个切片来存储检索到的数据,而且切片中的数据不容易更改,所以这样做会比在前端使用jquery更复杂。但是这样做让我对我心爱的Go感到沮丧 :-(。
所以有没有更好的方法来解决这个问题?
英文:
I know this problem maybe duplicate to this one. But it hasn't get a satisfied answer till now. And I really want to draw some attention to get a solution as soon as possible. So I beg you not to close this issue unless you have the solution and answered it in the previous one
I will describe the issue for convince:
I have a data structure that was stored in mongodb, as known, the _id
of mongodb is a bson.ObjectId
type, I could retrieve that with sort of like this:
type Data struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Content string `bson:"content"`
}
Then I got a slice of Data
by finding with specific query condition, and use that slice in http/template
to render for front-end view. In order to manipulate every item in the slice, I want to use the Id
field, but When using that with:
{{range $key, $value := .DataSlice}}
<td>{{$value.Id}}</td>
{{end}}
That Only gives a sort of string like ObjectIdHex("550146d1b51bc1c208d1924d")
instead of 550146d1b51bc1c208d1924d
which is nice and easy to use.
In the duplicate issue. The op have said that He found a way to resolve this by "adding a Id_String
" to the original data structure. But I really don't know how to do this? Is that mean assign it after retrieval ? Since I use a slice to store the data retrieved, And data in a slice couldn't be changed easily. It will be more complex to do that than do the job in the front-end using jquery. But doing that just depress me with my beloved Go :-(.
So is There a better way to do that?
答案1
得分: 17
bson.ObjectId 类型提供了 Hex 方法,它将返回你所需要的十六进制表示。而 template 包允许你在手头上拥有的值上调用任意方法,因此没有必要将该值作为字符串存储在其他地方。
例如,可以这样使用:
<td>{{$value.Id.Hex}}</td>
英文:
The bson.ObjectId type offers a Hex method that will return the hex representation you are looking for, and the template package allows one to call arbitrary methods on values you have at hand, so there's no need to store that value in duplicity anywhere else as a string.
This would work, for example:
<td>{{$value.Id.Hex}}</td>
答案2
得分: 0
你也可以按照以下方式定义结构体:
type Data struct {
Id bson.ObjectId `json:"id" bson:"_id"`
Content string `json:"content" bson:"content"`
}
然后在模板化 HTML 时可以使用 <td>{{$value.Id}}</td>
。
英文:
you also can define struct as follows
type Data struct {
Id bson.ObjectId `json:"id" bson:"_id,"`
Content string `json:"content" bson:"content"`
}
Then you can use <td>{{$value.Id}}</td>
when templating html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论