【发布时间】:2021-06-01 18:12:38
【问题描述】:
即使是最简单的代码:
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb+srv://standard:example@cluster0.f5yec.mongodb.net/blog-application?retryWrites=true&w=majority"))
if err != nil {
log.Fatal("Error connect to DB: ", err.Error())
}
db := client.Database("blog-application")
fmt.Println(time.Now().Second()) // 9
db.Collection("user").Find(context.Background(), bson.M{})
fmt.Println(time.Now().Second()) // 39
}
运行需要 30 秒。
为什么需要这么长时间才能运行? 任何帮助表示赞赏!
【问题讨论】:
-
你测量时间的方式是有缺陷的。我并不是说它不是 30 秒,但根据输出,它也可能是 1 分 30 秒,或者 1 小时 27 分 30 秒,或者其他任何 30 秒。
-
是的,但是当我运行它时,我注意到它需要 30 秒。
标签: mongodb go mongo-go-driver