mgo $inc 更新不工作

mgo $inc update not working

我正在尝试在每次访问特定博客时更新查看计数

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))
     }
}

//console是内部包

代码获取内容但不增加视图

我找到了答案, 所以即使模型有 'Views'。在集合中它是 'views' 所以它不断递增 'Views',它从未出现,因为 golang 正在寻找 'views'.

所以工作代码是

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