如何使用mgo从golang的mongodb集合中选择所有记录

如何使用mgo从golang的mongodb集合中选择所有记录

问题描述:

在MongoDB中,执行类似db.mycollection.find()的操作会返回集合中的所有文档.

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

使用 labix.org/v2/mgo 软件包在GoLang中工作时,例如,我这样做:

When working in GoLang using the package labix.org/v2/mgo 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.

找到了解决方案:

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