Search code examples
gobsonmgo

Select nested field from collection


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


Solution

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