2

I have this kind of documents in my mongo collection -

{
    "_id" : "3wPEpWwECbrTrnSbh",
    "brandId" : 45,
    "title" : "brandtitle",
    "logoImg" : "brandtitle.png",
    "category" : {
        "category 1" : [ 
            {
                "cat" : "A1 Plus Champ"
            }, 
            {
                "cat" : "A108"
            }, 
            {
                "cat" : "A6"
            },            
        ],
        "category 2" : [ 
            {
                "cat" : "something"
            }, 
            {
                "cat" : "soemthing else"
            }, 
            {
                "cat" : "something else"
            },            
        ],
    },
    "isActive" : true,
    "isOnboarded" : false,
    "serviceNumber" : 18605001492.0
}

So there are several brands. I can get everything, except the category in this.

My Models code data type for this is -

type Brand struct {
    Id string           `bson:"_id" json:"_id"`
    Brandid int         `bson:"brandId" json:"brandId"`
    Title string        `json:"title"`
    Logoimg string      `bson:"logoImg" json:"logoImg"`
    Category []string   `bson:"category" json:"category"`
    Isactive bool       `bson:"isActive" json:"isActive"`
    Isonboarded bool    `bson:"isOnboarded" json:"isOnboarded"`
    Servicenumber int   `bson:"serviceNumber" json:"serviceNumber"`
}

I'm taking Category to be a string array right now but of course that's wrong.

The output looks like this -

  {
    "_id": "3wPEpWwECbrTrnSbh",
    "brandId": 45,
    "title": "brandtitle",
    "logoImg": "brandtitle.png",
    "category": null,
    "isActive": true,
    "isOnboarded": false,
    "serviceNumber": 18605001492
  }

How should I construct this struct to be able to display the kind of data I'm getting out from the database?

Gaurav Ojha
  • 1,147
  • 1
  • 15
  • 37

1 Answers1

1
type Brand struct {
    Id string           `bson:"_id" json:"_id"`
    Brandid int         `bson:"brandId" json:"brandId"`
    Title string        `json:"title"`
    Logoimg string      `bson:"logoImg" json:"logoImg"`
    Category      map[string][]map[string]string   `bson:"category" json:"category"`
    Isactive bool       `bson:"isActive" json:"isActive"`
    Isonboarded bool    `bson:"isOnboarded" json:"isOnboarded"`
    Servicenumber int   `bson:"serviceNumber" json:"serviceNumber"`
}
CrazyCrow
  • 4,125
  • 1
  • 28
  • 39
  • Can you explain a little bit as to what [string] means vs just string? – Gaurav Ojha Aug 04 '16 at 10:02
  • @GauravOjha sorry, but I'm not sure if I can explain this in terms of pure Go without any other programming language background. In short you've tried to fill `[]string` typed field with more complex type that's why you got `nil`. To work with nested JSON (and not only) in Go you should learn more about the [`map`](https://blog.golang.org/go-maps-in-action). – CrazyCrow Aug 04 '16 at 10:21
  • Thank you once again for the information @roman-r I'm learning Go and highly appreciate your kind help! – Gaurav Ojha Aug 04 '16 at 10:40