添加maven依赖
<!--mongodb 驱动-->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.6.4</version>
</dependency>
<!--mongodb Java 驱动-->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.6.4</version>
</dependency>
<!--单元测试支持-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
连接方式
/**
* 无权限的连接方式
*/
@Test
public void noAuthConnection(){
try{
//通过连接认证获取MongoDB连接
MongoClient client = new MongoClient("127.0.0.1", 27017);
// 连接到数据库: 如果指定的数据库不存在,mongo会自动创建数据库
MongoDatabase database = client.getDatabase("DEMO");
System.out.println("Mongodb connection successful...");
//创建一个集合(可以理解为创建一张表)
database.createCollection("user");
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 权限认证的方式
*/
@Test
public void authConnection(){
try{
// 连接到 mongodb 服务
ServerAddress addr = new ServerAddress("127.0.0.1", 27017);
// 设置权限认证:三个参数分别为 用户名 数据库名称 密码
MongoCredential credential = MongoCredential.createScramSha1Credential("demo", "DEMO", "demo123456".toCharArray());
//mongo的参数设置, 如最大连接、超时时间等
MongoClientOptions options = MongoClientOptions.builder().build();
//通过连接认证获取MongoDB连接
MongoClient client = new MongoClient(addr, credential, options);
// 连接到数据库: 如果指定的数据库不存在,mongo会自动创建数据库
MongoDatabase database = client.getDatabase("DEMO");
System.out.println("Mongodb connection successful...");
//创建一个集合(可以理解为创建一张表)
database.createCollection("user");
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 权限认证的方式2
*/
@Test
public void authConnectionByUri(){
try{
MongoClientURI uri = new MongoClientURI("mongodb://demo:[email protected]:27017/DEMO");
//通过连接认证获取MongoDB连接
MongoClient client = new MongoClient(uri);
// 连接到数据库: 如果指定的数据库不存在,mongo会自动创建数据库
MongoDatabase database = client.getDatabase("DEMO");
System.out.println("Mongodb connection successful...");
//创建一个集合
database.createCollection("add_table");
}catch (Exception e){
e.printStackTrace();
}
}
举几个个栗子
先封装一个简单的连接工具类
public class MongoDBJDBC {
private MongoClient client;
private MongoDatabase database;
public MongoClient getClient(){
return this.client;
}
public MongoDatabase getDatabase(){
return this.database;
}
public MongoDBJDBC(){
try{
// 连接到 mongodb 服务
ServerAddress addr = new ServerAddress("127.0.0.1", 27017);
// 设置权限认证:三个参数分别为 用户名 数据库名称 密码
MongoCredential credential = MongoCredential.createScramSha1Credential("demo", "DEMO", "demo123456".toCharArray());
//mongo的参数设置, 如最大连接、超时时间等
MongoClientOptions options = MongoClientOptions.builder().build();
//通过连接认证获取MongoDB连接
client = new MongoClient(addr, credential, options);
// 连接到数据库: 如果指定的数据库不存在,mongo会自动创建数据库
database = client.getDatabase("DEMO");
System.out.println("Mongodb connection successful...");
}catch (Exception e){
throw new RuntimeException(e);
}
}
}
- 添加、获取 集合
@Test public void addAndGetTable(){ MongoDBJDBC mongoDBJDBC = new MongoDBJDBC(); MongoDatabase database = mongoDBJDBC.getDatabase(); //添加集合 database.createCollection("add_table"); System.out.println("add table successful"); //获取集合 MongoCollection<Document> collection = database.getCollection("add_table"); System.out.println("get table successful"); }
- 添加文档
@Test public void insertData(){ MongoDBJDBC mongoDBJDBC = new MongoDBJDBC(); MongoDatabase database = mongoDBJDBC.getDatabase(); //获取集合 MongoCollection<Document> collection = database.getCollection("user"); System.out.println("get table successful"); //创建文档 Document document = new Document() .append("name", "张三") .append("age", 23) .append("sex", "男") .append("description", "this is a bad boy!"); //插入到数据库 collection.insertOne(document);//插入单条数据 List<Document> list = new ArrayList<>(); Document document1 = new Document() .append("name", "李四") .append("age", 6) .append("sex", "男") .append("description", "this is a good boy!"); Document document2 = new Document() .append("name", "赵八") .append("age", 15) .append("sex", "男") .append("description", "this is a bad boy!"); list.add(document1); list.add(document2); collection.insertMany(list);//插入多条条数据 }
- 检索所有文档
@Test public void findAllDoc(){ MongoDBJDBC mongoDBJDBC = new MongoDBJDBC(); MongoDatabase database = mongoDBJDBC.getDatabase(); //获取集合 MongoCollection<Document> collection = database.getCollection("user"); System.out.println("get table successful"); //执行查询 FindIterable<Document> findIterable = collection.find(); for (Document aFindIterable : findIterable) { System.out.println(aFindIterable.toString()); } }
- 更新文档
@Test public void updateDoc(){ MongoDBJDBC mongoDBJDBC = new MongoDBJDBC(); MongoDatabase database = mongoDBJDBC.getDatabase(); //获取集合 MongoCollection<Document> collection = database.getCollection("user"); System.out.println("get table successful"); //更新文档 将文档中name= 赵八 的文档修改为description= this is a good boy! collection.updateMany(Filters.eq("name", "赵八"), new Document("$set",new Document("description","this is a good boy!"))); }
- 删除文档
@Test public void delDoc(){ MongoDBJDBC mongoDBJDBC = new MongoDBJDBC(); MongoDatabase database = mongoDBJDBC.getDatabase(); //获取集合 MongoCollection<Document> collection = database.getCollection("user"); System.out.println("get table successful"); //执行删除 collection.deleteMany(Filters.eq("name", "赵八")); }
原文地址:https://www.cnblogs.com/vchar/p/12243723.html
时间: 2024-10-10 04:06:25