我在这里查看了答案,但这使用了旧的和未维护的 mgo。如何使用 mongo-go-driver 查找集合中的所有文档?
我尝试传递一个nil
过滤器,但这不会返回任何文档,而是返回nil
. 我还检查了文档,但没有看到任何提及归还所有文档的内容。这是我尝试过的上述结果。
client, err := mongo.Connect(context.TODO(), "mongodb://localhost:27017")
coll := client.Database("test").Collection("albums")
if err != nil { fmt.Println(err) }
// we can assume we're connected...right?
fmt.Println("connected to mongodb")
var results []*Album
findOptions := options.Find()
cursor, err := coll.Find(context.TODO(), nil, findOptions)
if err != nil {
fmt.Println(err) // prints 'document is nil'
}
另外,我很困惑为什么我需要在集合上findOptions
调用Find()
函数时指定(或者我不需要指定?)。