【问题标题】:Each Database Operation takes 30 seconds每个数据库操作需要 30 秒
【发布时间】: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


【解决方案1】:

您没有对查找进行错误检查。您的查找失败,因为您的客户端无法连接到您的集群,并且默认的服务器选择超时为 30 秒。

“每个数据库操作需要 30 秒”只是部分正确。数据库操作的每次尝试需要 30 秒才能失败。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 2023-02-06
    • 2017-06-29
    • 2011-04-30
    • 1970-01-01
    • 2018-08-30
    相关资源
    最近更新 更多