title: Getting data from MongoDB inside a struct quite complex
body: <p>For a project I have to query a Mongo database with a RESTful API in Go.</p>
  
  <p>There is my problem, I get this structure from Mongo.</p>
  
  <pre><code>{
      "_id" : "ObjectID",
      "id" : 1234,
      "project" : {
          "id" : 1,
          "name" : "A project"
      },
      "tracker" : {
          "id" : 2,
          "name" : "A tracker"
      },
      "status" : {
          "id" : 3,
          "name" : "A status"
      },
      "priority" : {
          "id" : 4,
          "name" : "A priority"
      },
      "author" : {
          "id" : 5,
          "name" : "An author"
      },
      "assigned_to" : {
          "id" : 6,
          "name" : "Assigned to"
      },
      "category" : {
          "id" : 7,
          "name" : "A category"
      },
      "subject" : "A subject",
      "description" : "A description",
      "done_ratio" : 0,
      "spent_hours" : 0.0,
      "total_spent_hours" : 0.0,
      "custom_fields" : [ 
          {
              "id" : 1,
              "name" : "",
              "value" : ""
          }, 
          {
              "id" : 2,
              "name" : "",
              "multiple" : true,
              "value" : [ 
                  ""
              ]
          }
  
      ],
      "created_on" : "2018-12-21T15:22:57.000Z",
      "updated_on" : "2018-12-26T10:58:35.000Z",
      "journals" : [ 
          {
              "id" : 1,
              "user" : {
                  "id" : 1,
                  "name" : ""
              },
              "notes" : "",
              "created_on" : "2018-12-21T15:32:51Z",
              "details" : [ 
                  {
                      "property" : "",
                      "name" : "",
                      "new_value" : ""
                  }
              ]
          }, 
          {
              "id" : 2,
              "user" : {
                  "id" : 1,
                  "name" : ""
              },
              "notes" : "",
              "created_on" : "2018-12-21T15:33:54Z",
              "details" : []
          }
      ]
  }
  </code></pre>
  
  <p>As you can see, there is two elements called custom_fields and journals which are list of object. But sometimes it can be only a unique object not a list.</p>
  
  <p>In my Go API, I created the following structure: </p>
  
  <pre><code>type Custom struct {
      Id int      `json:"id" bson:"id"`
      Name string `json:"name" bson:"name"`
      Multiple bool `json:"multiple" bson:"multiple"`
      Value []string `json:"value" bson:"value"`
  }
  
  type Journal struct {
      ID   int `json:"id" bson:"id"`
      User struct {
          ID   int    `json:"id" bson:"id"`
          Name string `json:"name" bson:"name"`
      } `json:"user" bson:"user"`
      Note       string `json:"notes" bson:"notes"`
      Created_on string `json:"created_on" bson:"created_on"`
      Details    struct {
          Property  string `json:"property" bson:"property"`
          Name      string `json:"name" bson:"name"`
          Old_Value string `json:"old_value" bson:"old_value"`
          New_Value string `json:"new_value" bson:"new_value"`
      } `json:"details" bson:"details"`
  }
  
  type MainStruct struct {
      ID      int `json:"id" bson:"id"`
      Project struct {
          ID   int    `json:"id" bson:"id"`
          Name string `json:"name" bson:"name"`
      } `json:"project" bson:"project"`
      Tracker struct {
          ID   int    `json:"id" bson:"id"`
          Name string `json:"name" bson:"name"`
      } `json:"tracker" bson:"tracker"`
      Status struct {
          ID   int    `json:"id" bson:"id"`
          Name string `json:"name" bson:"name"`
      } `json:"status" bson:"status"`
      Priority struct {
          ID   int    `json:"id" bson:"id"`
          Name string `json:"name" bson:"name"`
      } `json:"priority" bson:"priority"`
      Author struct {
          ID   int    `json:"id" bson:"id"`
          Name string `json:"name" bson:"name"`
      } `json:"author" bson:"author"`
      Assigned_to struct {
          ID   int    `json:"id" bson:"id"`
          Name string `json:"name" bson:"name"`
      } `json:"assigned_to" bson:"assigned_to"`
      Subject      string    `json:"subject" bson:"subject"`
      Description  string    `json:"description" bson:"description"`
      Due_date     time.Time `json:"due_date" bson:"due_date"`
      Done_ratio   int       `json:"done_ratio" bson:"done_ratio"`
      CustomFields []Custom  `json:"custom_fields" bson:"custom_fields"`
      Created_on   time.Time `json:"created_on" bson:"created_on"`
      Updated_on   time.Time `json:"updated_on" bson:"updated_on"`
      Journals     []Journal `json:"journals" bson:"journals"`
  }
  </code></pre>
  
  <p>But when I query MongoDb and get a document which as one custom_field or one journal, I get the following errors:</p>
  
  <pre><code>cannot decode string into a slice
  </code></pre>
  
  <p>I don't know  how to do to make the one element into a slice of 1.</p>