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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了