之前的文章提到过如何通过命令行客户端来操作MongoDB,现在讲如何通过JavaApi来操作MongoDB
前置工作
创建maven工程,并导入依赖的jar包
<!-- mongodb java驱动包 -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.2</version>
</dependency>
<!-- junit测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
1 连接数据库
为方便研究,本次使用无认证的方式进行连接
// 获取MongoDB客户端
MongoClient mongoClient = new MongoClient("mongodb服务器的ip地址",27017);
// 获取想要连接的数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("blog");
2 基本操作
2.1 集合/表相关
2.1.1 创建集合
@Test
public void testCreateCollection() {
mongoDatabase.createCollection("bl_collection");
}
插入前
插入后
2.1.2 查看集合
@Test
public void testShowCollections() {
ListCollectionsIterable<Document> collections = mongoDatabase.listCollections();
MongoCursor<Document> iter = collections.iterator();
while(iter.hasNext()) {
System.out.println(iter.next());
}
}
2.1.3 修改集合
暂时没看到相应的api
2.1.4 删除集合
暂时没看到相应的api
3.1 文档/表记录相关
3.1.1 插入文档
/**
* 插入文档
*/
@Test
public void testInsertDoc() {
// 获取集合
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("bl_collection");
// 创建文档
Document document = new Document()
.append("commentContent", "我是评论的内容")
.append("commentUser", 123456)
.append("commentBlog", "1301794986045005824 ")
.append("commentGood", 0)
.append("createdTime", "2020-09-21 15:30:30");
mongoCollection.insertOne(document);
插入前
插入后
3.1.2 查询文档
我们再插入两条数据方便进行测试
/**
* 插入文档2
*/
@Test
public void testInsertDoc2() {
// 获取集合
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("bl_collection");
// 创建文档
Document document = new Document()
.append("commentContent", "我是评论的内容2")
.append("commentUser", 222222)
.append("commentBlog", "1301794986045005825")
.append("commentGood", 1)
.append("createdTime", "2020-09-21 15:30:31");
mongoCollection.insertOne(document);
}
/**
* 插入文档3
*/
@Test
public void testInsertDoc3() {
// 获取集合
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("bl_collection");
// 创建文档
Document document = new Document()
.append("commentContent", "我是评论的内容3")
.append("commentUser", 333333)
.append("commentBlog", "1301794986045005826")
.append("commentGood", 2)
.append("createdTime", "2020-09-21 15:30:32");
mongoCollection.insertOne(document);
}
a) 查看所有文档
* 查询文档
*/
@Test
public void testQueryDoc() {
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("bl_collection");
FindIterable<Document> docs = mongoCollection.find();
MongoCursor<Document> iter = docs.iterator();
while(iter.hasNext()) {
Document document = iter.next();
System.out.println(document);
// 获取里面的内容
// 比如获取id
Object id = document.get("_id");
System.out.println(id);
// 比如获取评论内容
Object content = document.get("commentContent");
System.out.println(content);
}
}
b) 限制输出的文档数
/**
* 查询文档 限制输出的文档数
*/
@Test
public void testQueryDoc2() {
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("bl_collection");
FindIterable<Document> docs = mongoCollection.find().limit(2);
MongoCursor<Document> iter = docs.iterator();
while(iter.hasNext()) {
Document document = iter.next();
System.out.println(document);
}
}
c) 查找指定记录
/**
* 查询文档 查找指定记录
*/
@Test
public void testQueryDoc3() {
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("bl_collection");
BasicDBObject query = new BasicDBObject("commentGood", 2);
FindIterable<Document> docs = mongoCollection.find(query);
MongoCursor<Document> iter = docs.iterator();
while(iter.hasNext()) {
Document document = iter.next();
System.out.println(document);
}
}
d) and查询
/**
* 查询文档 and查询
*/
@Test
public void testQueryDoc4() {
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("bl_collection");
BasicDBObject query = new BasicDBObject()
.append("commentGood", 2)
.append("commentUser", 333333);
FindIterable<Document> docs = mongoCollection.find(query);
MongoCursor<Document> iter = docs.iterator();
while(iter.hasNext()) {
Document document = iter.next();
System.out.println(document);
}
}
e) or 查询
/**
* 查询文档 or查询
*/
@Test
public void testQueryDoc5() {
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("bl_collection");
// 编写or的查询条件
BasicDBList or = new BasicDBList();
or.add(new BasicDBObject("commentGood",2));
or.add(new BasicDBObject("commentUser",222222));
BasicDBObject query = new BasicDBObject("$or",or);
FindIterable<Document> docs = mongoCollection.find(query);
MongoCursor<Document> iter = docs.iterator();
while(iter.hasNext()) {
Document document = iter.next();
System.out.println(document);
}
}
f) 条件查询
/**
* 查询文档 条件查询
*/
@Test
public void testQueryDoc6() {
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("bl_collection");
FindIterable<Document> docs = mongoCollection.find(Filters.gt("commentGood", 1));
MongoCursor<Document> iter = docs.iterator();
while(iter.hasNext()) {
Document document = iter.next();
System.out.println(document);
}
}
h) 模糊查询
/**
* 查询文档 模糊查询
*/
@Test
public void testQueryDoc7() {
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("bl_collection");
Pattern pattern = Pattern.compile(".*内容3.*");
BasicDBObject query = new BasicDBObject()
.append("commentContent", pattern);
FindIterable<Document> docs = mongoCollection.find(query);
MongoCursor<Document> iter = docs.iterator();
while(iter.hasNext()) {
Document document = iter.next();
System.out.println(document);
}
}
3.1.3 修改文档
/**
* 修改文档
*/
@Test
public void testUpdateDoc() {
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("bl_collection");
Document doc = new Document("$set",new Document().append("commentGood", 888).append("commentContent", "我是评论的内容"));
mongoCollection.updateMany(Filters.eq("commentGood", 2), doc);
}
结果查看
3.1.4 删除文档
/**
* 删除文档
*/
@Test
public void testDeleteDoc() {
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("bl_collection");
DeleteResult deleteRes = mongoCollection.deleteMany(Filters.eq("commentUser", 333333));
long deleteCnt = deleteRes.getDeletedCount();
if (deleteCnt > 0) {
System.out.println("删除成功");
}
}
用模糊查询匹配删除了所有的数据
/**
* 删除文档
*/
@Test
public void testDeleteDoc2() {
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("bl_collection");
Pattern pattern = Pattern.compile(".*内容.*");
BasicDBObject query = new BasicDBObject("commentContent",pattern);
DeleteResult deleteRes = mongoCollection.deleteMany(query);
long deleteCnt = deleteRes.getDeletedCount();
if (deleteCnt > 0) {
System.out.println("删除成功");
}
}