retrieve a nested document with mgo

限于喜欢 提交于 2019-12-11 06:13:58

问题


I need to retrieve a nested document in mongoDB with mgo.
Here is my document in db:

{
    "_id" : "packing_type_0000",
    "name" : "packing",
    "category" : "logistics",
    "en" : {
            "translatedName" : "Packing and Order Prep",
    },
}

This is my golang structure:

type jobTypeWording struct {
     translatedName     string `json:"translatedName" bson:"translatedName"`
}

type jobType struct {
    ID               string         `json:"_id" bson:"_id"`
    Name             string         `json:"name" bson:"name"`
    Category         string         `json:"category" bson:"category"`
    en               jobTypeWording `json:"en" bson:"en"`
}

And my code:

result := jobType{}
sessionCopy := session.Copy()
defer sessionCopy.Close()
c := sessionCopy.DB(os.Getenv("DB_DATABASE")).C("jobTypes")
err := c.Find(bson.M{"_id": Id}).One(&result)  
fmt.Println(result.en)

My program program output:

{  }

How do I retrieve en.translatedName?

In the same program I get other nested bson from mongo and it's working with same way. I don't understand my mistake.


回答1:


I found solution, it's because my field en began by a lower case. If i change en by En and translatedName by TranslatedName, it's work.
Here a more details answer



来源:https://stackoverflow.com/questions/43677530/retrieve-a-nested-document-with-mgo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!