2017-01-08 29 views
1

我試圖更新視圖中的每個特定的博客被訪問

type Blog struct { 
    ID   bson.ObjectId `bson:"_id,omitempty"` 
    Topic  string 
    TimeCreated string 
    Views  int 
    Sections []Section 
} 
type Section struct { 
    Name string 
    Content string 
} 

和位指示

func Blogs(w http.ResponseWriter, r *http.Request) { 
    id := r.FormValue("id") 
    if id != "" { 
     blog := model.Blog{} 
     colQuerier := bson.M{"_id": bson.ObjectIdHex(id)} 

     e := mCollection.Find(colQuerier).One(&blog) 
     if e != nil { 
      console.PrintError(e) 
      return 
     } 
     views := blog.Views 
     fmt.Println(views) 
     change := bson.M{"$inc": bson.M{"Views": 1}} 

     e = mCollection.Update(colQuerier, change) 
     if e != nil { 
      console.PrintError(e) 
     } 

     jsonData, _ := json.Marshal(blog) 
     fmt.Fprintf(w, string(jsonData)) 
    } 
} 

//控制檯時間計算是一個內部封裝

代碼提取的內容,但不增加視圖

回答

1

我找到了答案, ,即使模型有'視圖'。在收藏中它是'意見',所以它不斷增加'意見',這從來沒有出現,因爲golang正在尋找'意見'。

所以工作代碼是

change := bson.M{"$inc": bson.M{"views": 1}}