使用mgo检索嵌套文档 [英] retrieve a nested document with mgo

查看:230
本文介绍了使用mgo检索嵌套文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用mgo在mongoDB中检索嵌套文档.
这是我在db中的文档:

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",
    },
}

这是我的golang结构:

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

我的代码:

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)

我的程序程序输出:

{  }

如何检索en.translatedName?

在同一程序中,我从mongo获得了其他嵌套的bson,并且它以相同的方式工作.我不明白我的错误.

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

推荐答案

我找到了解决方案,这是因为我的字段en以小写字母开头.如果我将en更改为En,将translatedName更改为TranslatedName,则可以使用.
这里有更多详细信息 answer

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

这篇关于使用mgo检索嵌套文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆