使用golang的mgo库,如何检索嵌套对象,例如列表?

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

Using golang's mgo library, how do you retrieve nested objects such as lists

问题

我正在尝试检索填充此类的对象:

type Room struct {
    Name   string
    People []Person
    Chat   []ChatMessage
    Me     Person
}

数据字段"People"显示为空切片[]。我正在使用简单的查找来获取数据。

result := Room{}
err = c.Find(bson.M{"name": "dev"}).One(&result)

我做错了什么?

找到答案在这里:
https://groups.google.com/d/msg/mgo-users/KirqfCSlKFc/t2l3l4yxFRwJ

基本上,只需要在People []Person行的末尾添加 'bson: "<mongodb_class_name>"'

英文:

I am trying to retrieve an object that fills this class:

type Room struct {
    Name   string
    People []Person
    Chat   []ChatMessage
    Me     Person
}

The data field "People" comes up as an empty slice []. I am using a simple find to get the data.

result := Room{}
err = c.Find(bson.M{&quot;name&quot;: &quot;dev&quot;}).One(&amp;result)

What am I doing wrong?

Figured it out....

The answer can be found here:
https://groups.google.com/d/msg/mgo-users/KirqfCSlKFc/t2l3l4yxFRwJ

Basically, just need to add 'bson: "<mongodb_class_name>"' at the end of the People []Person line

答案1

得分: 6

解决了....

答案可以在这里找到:https://groups.google.com/d/msg/mgo-users/KirqfCSlKFc/t2l3l4yxFRwJ

基本上,只需要在People []Person行的末尾添加'bson: ""'

英文:

Figured it out....

The answer can be found here: https://groups.google.com/d/msg/mgo-users/KirqfCSlKFc/t2l3l4yxFRwJ

Basically, just need to add 'bson: ""' at the end of the People []Person line

huangapple
  • 本文由 发表于 2013年1月30日 02:38:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/14589869.html
匿名

发表评论

匿名网友

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

确定