How do you select all records from a mongodb collection in golang using mgo

瘦欲@ 提交于 2019-12-03 22:23:34

Found a solution:

    var results []client

    err := db.C("client").Find(nil).All(&results)
    if err != nil {
        // TODO: Do something about the error
    } else {
        fmt.Println("Results All: ", results) 
    }
func (uc UserController) GetUsersList(w http.ResponseWriter,r *http.Request,p httprouter.Params){

var u []models.User
// Fetch user
if err := uc.session.DB("mydb").C("users").Find(nil).All(&u); err != nil {

    w.WriteHeader(404)
    fmt.Println("Results All: ", u) 
    return
}
uj, _ := json.Marshal(u)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
fmt.Fprintf(w, "%s", uj)

}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!