2014-07-10 15:53
浏览 193


In MongoDB doing something like db.mycollection.find() returns all documents in a collection.

When working in GoLang using the package and I do for example:

query := db.C("client").Find();

It complains that it requires input in the form of an interface. All I need to do is retrieve all documents and iterate through them and display each one for now. How do I achieve this effect? All examples I have seen seem to have filters in place.

图片转代码服务由CSDN问答提供 功能建议

在MongoDB中,执行类似 db.mycollection.find()之类的操作的所有文档

在GoLang中使用软件包 进行操作时,例如,

  query:= db.C(“ client”)。Find(); 

它抱怨它需要以接口形式输入。 我需要做的就是检索所有文档并遍历它们,并立即显示每个文档。 如何达到此效果? 我见过的所有示例似乎都已安装了过滤器。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongpao1926 2014-07-10 16:02

    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) 
    点赞 打赏 评论
  • dougu5847 2018-10-17 05:58
    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 {
        fmt.Println("Results All: ", u) 
    uj, _ := json.Marshal(u)
    w.Header().Set("Content-Type", "application/json")
    fmt.Fprintf(w, "%s", uj)
    点赞 打赏 评论

相关推荐 更多相似问题