MongoClient添加连接配置信息

在MongoClient添加连接配置信息主要是将配置信息添加到url来实现的

MongoClient的都有哪些配置 ,在MongoClients的create方法中有这样一个方法,方法传的是mongodb的连接地址,然后会创建一个ConnectionString对象,这个对象里保存着MongoClient的所有的配置信息,像最大连接数、连接超时时间等信息

public static MongoClient create(String connectionString) {
    return create(new ConnectionString(connectionString));
}

 这里是对传来的ConnectionString地址进行解析成unprocessedConnectionString

String unprocessedConnectionString;
if (isMongoDBProtocol) {
    unprocessedConnectionString = connectionString.substring("mongodb://".length());
} else {
    unprocessedConnectionString = connectionString.substring("mongodb+srv://".length());
}

这两行代码是将unprocessedConnectionString和txtRecordsQueryParameters的连接信息进行解析封装成map

Map<String, List<String>> connectionStringOptionsMap = this.parseOptions(unprocessedConnectionString);
Map<String, List<String>> txtRecordsOptionsMap = this.parseOptions(txtRecordsQueryParameters);

这里将前两个map进行整合成参数map

Map<String, List<String>> combinedOptionsMaps = this.combineOptionsMaps(txtRecordsOptionsMap, connectionStringOptionsMap);

然后调用this.translateOptions(combinedOptionsMaps)方法,给ConnectionString对象的连接信息参数赋值

this.translateOptions(combinedOptionsMaps);
while(var4.hasNext()) {
	String key = (String)var4.next();
	String value = this.getLastValue(optionsMap, key);
	if (value != null) {
		if (key.equals("maxpoolsize")) {
			this.maxConnectionPoolSize = this.parseInteger(value, "maxpoolsize");
		} else if (key.equals("minpoolsize")) {
			this.minConnectionPoolSize = this.parseInteger(value, "minpoolsize");
		} else if (key.equals("maxidletimems")) {
			this.maxConnectionIdleTime = this.parseInteger(value, "maxidletimems");
		} else if (key.equals("maxlifetimems")) {
			this.maxConnectionLifeTime = this.parseInteger(value, "maxlifetimems");
		} else if (key.equals("waitqueuetimeoutms")) {
			this.maxWaitTime = this.parseInteger(value, "waitqueuetimeoutms");
		} else if (key.equals("connecttimeoutms")) {
			this.connectTimeout = this.parseInteger(value, "connecttimeoutms");
		} else if (key.equals("sockettimeoutms")) {
			this.socketTimeout = this.parseInteger(value, "sockettimeoutms");
		} else if (key.equals("tlsallowinvalidhostnames")) {
			this.sslInvalidHostnameAllowed = this.parseBoolean(value, "tlsAllowInvalidHostnames");
			tlsAllowInvalidHostnamesSet = true;
		} else if (key.equals("sslinvalidhostnameallowed")) {
			this.sslInvalidHostnameAllowed = this.parseBoolean(value, "sslinvalidhostnameallowed");
			tlsAllowInvalidHostnamesSet = true;
		} else if (key.equals("tlsinsecure")) {
			this.sslInvalidHostnameAllowed = this.parseBoolean(value, "tlsinsecure");
			tlsInsecureSet = true;
		} else if (key.equals("ssl")) {
			this.initializeSslEnabled("ssl", value);
		} else if (key.equals("tls")) {
			this.initializeSslEnabled("tls", value);
		} else if (key.equals("replicaset")) {
			this.requiredReplicaSetName = value;
		} else if (key.equals("readconcernlevel")) {
			this.readConcern = new ReadConcern(ReadConcernLevel.fromString(value));
		} else if (key.equals("serverselectiontimeoutms")) {
			this.serverSelectionTimeout = this.parseInteger(value, "serverselectiontimeoutms");
		} else if (key.equals("localthresholdms")) {
			this.localThreshold = this.parseInteger(value, "localthresholdms");
		} else if (key.equals("heartbeatfrequencyms")) {
			this.heartbeatFrequency = this.parseInteger(value, "heartbeatfrequencyms");
		} else if (key.equals("appname")) {
			this.applicationName = value;
		} else if (key.equals("retrywrites")) {
			this.retryWrites = this.parseBoolean(value, "retrywrites");
		} else if (key.equals("retryreads")) {
			this.retryReads = this.parseBoolean(value, "retryreads");
		} else if (key.equals("uuidrepresentation")) {
			this.uuidRepresentation = this.createUuidRepresentation(value);
		} else if (key.equals("directconnection")) {
			this.directConnection = this.parseBoolean(value, "directconnection");
		}
	}
}

 从上面的参数中找到需要的配置参数然后加到mongodb的连接地址中即可生效,例如 

mongodb://admin:Abc123++@127.0.0.1:27017/?authSource=local&serverselectiontimeoutms=1000

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,你可以使用MongoDB的Java驱动程序来连接和操作MongoDB数据库。以下是一个简单的示例代码,展示了如何连接MongoDB数据库: 首先,你需要在你的Java项目中添加MongoDB的Java驱动程序依赖。你可以在你的构建工具(如Maven或Gradle)的配置文件中添加以下依赖: Maven: ```xml <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.12.7</version> </dependency> ``` Gradle: ```groovy implementation 'org.mongodb:mongo-java-driver:3.12.7' ``` 接下来,你可以使用以下代码来连接MongoDB数据库: ```java import com.mongodb.MongoClient; import com.mongodb.MongoCredential; import com.mongodb.client.MongoDatabase; public class MongoDBConnector { public static void main(String[] args) { // 设置MongoDB连接信息 String host = "localhost"; // MongoDB主机名或IP地址 int port = 27017; // MongoDB端口 String database = "mydb"; // 数据库名称 String username = "myuser"; // 用户名 String password = "mypassword"; // 密码 // 创建MongoCredential实例 MongoCredential credential = MongoCredential.createCredential(username, database, password.toCharArray()); // 创建MongoClient实例 MongoClient mongoClient = new MongoClient(host, port); // 获取MongoDatabase实例 MongoDatabase mongoDatabase = mongoClient.getDatabase(database); // 打印数据库连接成功信息 System.out.println("成功连接MongoDB数据库"); // 关闭MongoClient连接 mongoClient.close(); } } ``` 在这个示例中,我们使用MongoClient类来连接MongoDB数据库。我们提供了MongoDB的主机名和端口号,以及数据库的名称、用户名和密码。然后,我们使用这些信息创建一个MongoCredential实例,并使用它来验证连接。接下来,我们使用MongoClient实例来获取MongoDatabase实例,可以使用该实例来进行数据库操作。最后,我们打印出连接成功的信息,并关闭MongoClient连接。 请注意,这只是一个简单的示例,实际情况可能会更复杂。你可能需要处理异常、使用连接池等。另外,MongoDB还提供了更高级的功能和API,如集合操作、查询和更新等。你可以查阅MongoDB的官方文档以了解更多详细信息:https://docs.mongodb.com/drivers/java/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值