MongoDB学习笔记(三)JAVA连接MongoDB

本文最后更新于:December 3, 2021 pm

MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统。是一个面向文档存储的数据库,操作起来比较简单和容易。在高负载的情况下,添加更多的节点,可以保证服务器性能。MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。

目录

创建一个Maven项目。导入依赖:

1
2
3
4
5
6
7
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.10</version>
</dependency>
</dependencies>

1.直接连接(无认证连接)

1
2
3
4
5
6
7
8
9
10
public static void main(String[] args) {
//连接到 mongodb 服务
MongoClient mongoClient = new MongoClient("localhost", 27017);
//连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("loong");
//获取集合
MongoCollection collection = mongoDatabase.getCollection("to");
// MongoCollection mongoCollection = MongoDBUtils.getCollection("loong","to");
System.out.println("OK");
}
  • 封装成工具类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.tothefor;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

/**
* @Author DragonOne
* @Date 2021/11/14 10:19
*/
public class MongoDBUtils {
private static MongoClient mongoClient = null;
//连接到 mongodb 服务
static {
if(mongoClient==null){
mongoClient = new MongoClient("localhost", 27017);
}
}
//连接到数据库
public static MongoDatabase getMongoDatabase(String dbName){
return mongoClient.getDatabase(dbName);
}
//获取集合
public static MongoCollection getCollection(String dbName,String setName){
MongoDatabase dbs = getMongoDatabase(dbName);
return dbs.getCollection(setName);
}
}

主函数代码:

1
2
3
4
public static void main(String[] args) {
MongoCollection mongoCollection = MongoDBUtils.getCollection("loong","to");
System.out.println("OK");
}

2.池连方式(无认证连接)

工具类代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.tothefor;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

/**
* @Author DragonOne
* @Date 2021/11/14 10:47
*/
public class MongoDBPoolUtils {
private static MongoClient mongoClient = null;
//连接到 mongodb 服务
static {
if(mongoClient==null){
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
builder.connectionsPerHost(10);//每个地址的最大连接数
builder.connectTimeout(5000);//连接超时时间,单位毫秒
builder.socketTimeout(5000);//设置读写操作超时时间
ServerAddress address = new ServerAddress("localhost", 27017);
mongoClient = new MongoClient(address,builder.build());
}
}
//连接到数据库
public static MongoDatabase getMongoDatabase(String dbName){
return mongoClient.getDatabase(dbName);
}
//获取集合
public static MongoCollection getCollection(String dbName, String setName){
MongoDatabase dbs = getMongoDatabase(dbName);
return dbs.getCollection(setName);
}
}

主函数代码:

1
2
3
4
public static void main(String[] args) {
MongoCollection mongoCollection = MongoDBPoolUtils.getCollection("loong","to");
System.out.println("OK");
}

3.池连方式(认证连接)

同无认证连接一样。只不过有一个地方不同而已,其他相同。不同之处:

1
2
3
4
5
6
7
8
9
10
11
12
//连接到 mongodb 服务
static {
if(mongoClient==null){
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
builder.connectionsPerHost(10);//每个地址的最大连接数
builder.connectTimeout(5000);//连接超时时间,单位毫秒
builder.socketTimeout(5000);//设置读写操作超时时间
MongoCredential credential = MongoCredential.createCredential("username","database","password".toCharArray());
ServerAddress address = new ServerAddress("localhost", 27017);
mongoClient = new MongoClient(address,credential,builder.build());
}
}

4.直接连接(认证连接)

同无认证连接一样。只不过有一个地方不同而已,其他相同。不同之处:

1
2
3
4
5
6
7
8
//连接到 mongodb 服务
static {
if(mongoClient==null){
MongoCredential credential = MongoCredential.createCredential("username","database","password".toCharArray());
ServerAddress address = new ServerAddress("localhost", 27017);
mongoClient = new MongoClient(address, Arrays.asList(credential));
}
}