mongodb - 带有 golang mongo-driver 的多个 maxPoolSize 配置

标签 mongodb go

当使用 golang 连接到 mongodb 实例时,我们可以指定 maxPoolSize 的值通过 connection string或使用 ClientOptions.SetMaxPoolSize 来自 mongo-driver 包裹。
这意味着初始化连接的代码片段可能如下所示:

    opts := options.Client().ApplyURI(mongodb://foor:bar@localhost:27017/test?maxPoolSize=123)
    opts.SetMaxPoolSize(234)
    client, err := mongo.NewClient(opts)
    
鉴于上述情况,我的问题是 maxPoolSize 中的哪一个规范优先,是连接字符串中的那个还是通过驱动程序指定的那个?

最佳答案

借助 D. SM 的提示我能够找到文档中指定的位置。 mongo-driver的优先规则可以在函数的 godoc 中找到 ClientOptions.ApplyURI :

If the connection string contains any options that have previously been set, it will overwrite them. Options that correspond to multiple URI parameters, such as WriteConcern, will be completely overwritten if any of the query parameters are specified. If an option is set on ClientOptions after this method is called, that option will override any option applied via the connection string.


这意味着最后指定的内容优先。所以在上述情况下 maxPoolSize通过驱动程序设置的将是客户端使用的那个。

关于mongodb - 带有 golang mongo-driver 的多个 maxPoolSize 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63943152/

相关文章:

mongodb - 比较 Jade 和 MongoDB 中的 ObjectID

go - 如何在新进程中执行方法

parsing - 在 Go 中并发解析二进制文件中的记录

mongodb - 数据库文件在哪里?在/var/lib/mongodb 我找不到任何增加的大小。我运行了非常大的循环来创建数十万个对象

sql - 数据库推荐

go - 带有 go 和 dep : `push rejected: failed to compile Go app` 的 Heroku

go - 引用内部结构

go - 拥有多模块 golang 存储库是否有意义,这样人们就不必仅仅因为它的类型而导入我的库

node.js - MongoDb Node.js 驱动程序 - 没有给定字段的 findOneAndUpdate() 行为

performance - 用于分片 + 冗余 MongoDB 环境的硬件