数据库建立
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
import java.util.ArrayList;
import java.util.List;
//mongodb 连接数据库工具类
public class MongoDBUtils {
//不通过认证获取连接数据库对象
public static MongoDatabase getConnect(){
//连接到 mongodb 服务
MongoClient mongoClient = new MongoClient("10.58.225.161", 27017);
//连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
//返回连接数据库对象
return mongoDatabase;
}
//需要密码认证方式连接
public static MongoDatabase getConnect2(){
List<ServerAddress> adds = new ArrayList<>();
//ServerAddress()两个参数分别为 服务器地址 和 端口
ServerAddress serverAddress = new ServerAddress("localhost", 27017);
adds.add(serverAddress);
List<MongoCredential> credentials = new ArrayList<>();
//MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
MongoCredential mongoCredential = MongoCredential.createScramSha1Credential("username", "databaseName", "password".toCharArray());
credentials.add(mongoCredential);
//通过连接认证获取MongoDB连接
MongoClient mongoClient = new MongoClient(adds, credentials);
//连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
//返回连接数据库对象
return mongoDatabase;
}
}
CRUD
增
1. 增加一条文档
@Test
public void testAddOne() {
// 获取数据库
MongoDatabase mongoDatabase = MongoDBUtils.getConnect();
// 获取集合
MongoCollection<Document> collection = mongoDatabase.getCollection("test");
// 方法一:直接用Document格式
Document document = new Document("name", "banana")
.append("category", "fruit")
.append("origin", "hainan");
collection.insertOne(document);
// 方法二:用哈希表格式转成Document格式
Map<String, Object> map = new HashMap<>();
map.put("name", "bird");
map.put("category", "animal");
map.put("amount", 200);
Document document1 = new Document(map);
collection.insertOne(document1);
}
2. 增加多条文档
@Test
public void testAddMany() {
// 获取数据库
MongoDatabase mongoDatabase = MongoDBUtils.getConnect();
// 获取集合
MongoCollection<Document> collection = mongoDatabase.getCollection("test");
// 方法一
List<Document> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Map<String, Object> map = new HashMap<>();
map.put("name", "hamster" + i);
map.put("category", "animal");
map.put("amount", 188 - i * 5);
Document document = new Document(map);
list.add(document);
}
collection.insertMany(list);
// 方法二
List<Document> list1 = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Document document = new Document("name", "hamster" + i)
.append("category", "animal")
.append("amount", 188 - i * 5);
list1.add(document);
}
collection.insertMany(list1);
}
删
@Test
public void testMinus() {
// 获取数据库
MongoDatabase mongoDatabase = MongoDBUtils.getConnect();
// 获取集合
MongoCollection<Document> collection = mongoDatabase.getCollection("test");
// 创建过滤器
Bson filter = Filters.eq("name","hamster1");
collection.deleteOne(filter); // 删除满足条件的第一条文档
collection.deleteMany(filter); // 删除满足条件的所有文档
}
改
@Test
public void testUpdate() {
// 获取数据库
MongoDatabase mongoDatabase = MongoDBUtils.getConnect();
// 获取集合
MongoCollection<Document> collection = mongoDatabase.getCollection("test");
Bson filter = Filters.eq("name","hamster0");
Document document = new Document("$set",new Document("name","hamster").append("origin","Japan"));
collection.updateOne(filter,document); // 修改满足条件的第一条文档
collection.updateMany(filter,document); // 修改满足条件的所有文档
}
查
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.junit.Test;
@Test
public void testFind() {
// 获取数据库
MongoDatabase mongoDatabase = MongoDBUtils.getConnect();
// 获取集合
MongoCollection<Document> collection = mongoDatabase.getCollection("test");
// 1. 获取全部文档(直接遍历)
FindIterable<Document> findIterable = collection.find();
for (Document document : findIterable) {
System.out.println(document);
}
// 2. 获取满足条件的文档
Bson filter = Filters.eq("category","fruit");
FindIterable findIterable1 = collection.find(filter);
// 找到第一个满足条件的
Document first = (Document) findIterable1.first();
System.out.println(first);
// 找到所有满足条件的(迭代器遍历)
MongoCursor cursor = findIterable1.iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}
引用