title: How to decode mongodb data to struct
body: <p>Go Model:</p>
  
  <pre><code>package models
  
  import (
      "time"
      "go.mongodb.org/mongo-driver/bson/primitive"
  )
  
  // News : News Model
  type News struct {
      ID         primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
      Host       string             `json:"host,omitempty" bson:"host,omitempty"`
      Category   string             `json:"category,omitempty" bson:"category,omitempty"`
      Headline   string             `json:"headline,omitempty" bson:"headline,omitempty"`
      Image      string             `json:"image,omitempty" bson:"image,omitempty"`
      URL        string             `json:"url,omitempty" bson:"url,omitempty"`
      Date       string             `json:"date,omitempty" bson:"date,omitempty"`
      ClickCount int64              `json:"clickCount,omitempty" bson:"clickCount,omitempty"`
      Archived   bool               `json:"archived,omitempty" bson:"archived,omitempty"`
      CreatedAt  time.Time          `json:"createdAt,omitempty" bson:"createdAt,omitempty"`
  }
  </code></pre>
  
  <p>MongoDB Data that I have:</p>
  
  <pre><code>{
      "_id" : ObjectId("5e1d58f6fad87c735bbca592"),
      "createdAt" : ISODate("2020-01-14T11:30:22.481Z"),
      "clickCount" : 0,
      "archived" : false,
      "host" : "timesofindia",
      "category" : "sports",
      "headline" : "Caroline Wozniacki pulls out of Kooyong Classic",
      "url" : "https://timesofindia.indiatimes.com/sports/tennis/top-stories/caroline-wozniacki-pulls-out-of-kooyong-classic/articleshow/73238147.cms",
      "image" : "https://timesofindia.indiatimes.com/thumb/msid-73238147,width-400,resizemode-4/73238147.jpg",
      "date" : "14 Jan 2020, 0958 hrs IST"
  }
  </code></pre>
  
  <p>My API endpoint code:</p>
  
  <pre><code>func AllNews(w http.ResponseWriter, r *http.Request) {
      w.Header().Set("content-type", "application/json")
  
      collection := config.Client.Database("newspaper").Collection("news")
      ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
  
      var allNews []models.News
      var finalResponse models.FinalResponse
  
      cursor, e := collection.Find(ctx, bson.M{})
      if e != nil {
          w.WriteHeader(http.StatusInternalServerError)
          w.Write([]byte(`{ "message": "` + e.Error() + `" }`))
          return
      }
      defer cursor.Close(ctx)
  
      for cursor.Next(ctx) {
          var news models.News
          cursor.Decode(&amp;news)
          allNews = append(allNews, news)
      }
  
      finalResponse.Status = "success"
      finalResponse.Body = allNews
  
      json.NewEncoder(w).Encode(finalResponse)
  }
  </code></pre>
  
  <p>Challenge that I am facing right now is in output, I can not see "clickCount" and "archived". </p>
  
  <p>Output:</p>
  
  <pre><code>{
      "status": "success",
      "body": [
          {
              "_id": "5e1d58f6fad87c735bbca588",
              "host": "timesofindia",
              "category": "business",
              "headline": "Bandhan Bank all set to announce its Q3 results today",
              "image": "https://timesofindia.indiatimes.com/thumb/msid-73239715,width-400,resizemode-4/73239715.jpg",
              "url": "https://timesofindia.indiatimes.com/business/india-business/bandhan-bank-all-set-to-announce-its-q3-results-today/articleshow/73239715.cms",
              "date": "14 Jan 2020, 1111 hrs IST",
              "createdAt": "2020-01-14T11:30:22.442Z"
          }
      ]
  }
  </code></pre>
  
  <p>I tried changing data types to int32 and string on both fields it still didn't work.
  If I change the data type of these two field then in "body" of output I only see "id" and "createdAt"
  Let me know if more data is needed.</p>