如何使用 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()(或者我不需要指定?)感到困惑。
慕姐4208626
qq_笑_17
相关分类