如何使用mgo从golang的mongodb集合中选择所有记录 [英] How do you select all records from a mongodb collection in golang using mgo
本文介绍了如何使用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)
}
这篇关于如何使用mgo从golang的mongodb集合中选择所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文