dousong5492 2018-01-14 21:35 采纳率: 100%
浏览 48
已采纳

从集合中选择嵌套字段

I am trying to grab a single field from a nested object in a Mongo collection.

I need to get all tags that match the criteria. I am able to query and get the whole object but having no luck with getting a list or array of tags.

// Content struct
type Content struct {
    ID                  bson.ObjectId           `json:"id" bson:"_id"`
    PrimaryMarket       string                  `json:"primary_market" bson:"primary_market"`
    Title               string                  `json:"title" bson:"title"`
    Description         string                  `json:"description" bson:"description"`
    Owner               User                    `json:"owner" bson:"owner"`
    IsActive            bool                    `json:"is_active" bson:"is_active"`
    File                string                  `json:"file" bson:"file"`
    FileName            string                  `json:"file_name" bson:"file_name"`
    FileType            string                  `json:"file_type" bson:"file_type"`
    FileSize            string                  `json:"file_size" bson:"file_size"`
    FileExt             string                  `json:"file_ext" bson:"file_ext"`
    OriginalHeight      int                     `json:"original_height" bson:"original_height"`
    OriginalWidth       int                     `json:"original_width" bson:"original_width"`
    Height              int                     `json:"height" bson:"height"`
    Width               int                     `json:"width" bson:"width"`
    Tags                []Tag                   `json:"tags" bson:"tags"`
    Flags               []Flagged               `json:"flags" bson:"flags"`
    CreatedDate         time.Time               `json:"created_date" bson:"created_date"`
}

// Tag struct
type Tag struct {
    ID                  bson.ObjectId           `json:"id" bson:"_id"`
    Tag                 string                  `json:"tag" bson:"tag"`
    Market              string                  `json:"market" bson:"market"`
    CreatedBy           User                    `json:"created_by" bson:"created_by"`
    CreatedDate         time.Time               `json:"created_date" bson:"created_date"`
    IsActive            bool                    `json:"is_active" bson:"is_active"`
}

My query

var result []struct {
    Tags []struct {
        Description string `bson:"description"`
    } `bson:"tags"`
}

find := app.Session.DB("mydb").C("content").Find(bson.M{"primary_market": "Photos", "tags.description": &bson.RegEx{Pattern: query, Options: "i"}}).All(&result)

I've tried using a Select statement using dot notation but that doesnt seem to work. Any help would be appreciated.

Thanks

  • 写回答

1条回答 默认 最新

  • dongming6201 2018-01-15 06:22
    关注

    MongoDB will result with an array of objects for the mentioned query, which will be something as follows:

    { "_id" : ObjectId("5a5c401ddbff3fdea15082ee"), "tags" : [ { "tag" : "query" }, { "tag" : "something" } ] }
    { "_id" : ObjectId("5a5c423ddbff3fdea15082f0"), "tags" : [ { "tag" : "something" }, { "tag" : "query" } ] }
    

    So to properly unmarshal this array of objects, theresult variable should be as follows:

    var result []struct {
        Tags []struct {
            Tag string `bson:"tag"`
        } `bson:"tags"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog