当前位置:网站首页>MongonDB API使用
MongonDB API使用
2022-07-26 05:18:00 【北海怪兽Monster】
mongodb-driver是mongo官方推出的java连接mongoDB的驱动包,相当于JDBC驱动。我们现在来使用mongodb-driver完成对Mongodb的操作。
添加依赖,注意自己使用得版本
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb‐driver</artifactId>
<version>3.10.1</version>
</dependency>
测试代码
public class MongoTest {
// 客户端
private MongoClient mongoClient;
// 集合
private MongoCollection<Document> comment;
@Before
public void init() {
//1. 创建操作MongoDB的客户端
mongoClient = new MongoClient("192.168.200.128");
// MongoClient mongoClient = new MongoClient("192.168.200.128",27017);
//2. 选择数据库 use commentdb
MongoDatabase commentdb = mongoClient.getDatabase("commentdb");
//3. 获取集合 db.comment
comment = commentdb.getCollection("comment");
}
//查询所有数据db.comment.find()
@Test
public void test1() {
//4. 使用集合进行查询,查询所有数据db.comment.find()
FindIterable<Document> documents = comment.find();
//5. 解析结果集(打印)
// "_id" : "1", "content" : "到底为啥出错", "userid" : "1012", "thumbup" : 2020 }
for (Document document : documents) {
System.out.println("------------------------------");
System.out.println("_id:" + document.get("_id"));
System.out.println("content:" + document.get("content"));
System.out.println("userid:" + document.get("userid"));
System.out.println("thumbup:" + document.get("thumbup"));
}
}
@After
public void after() {
// 释放资源,关闭客户端
mongoClient.close();
}
// 根据条件_id查询数据,db.comment.find({"_id" : "1"})
@Test
public void test2() {
// 封装查询条件 ,可以传一个Map集合,进行多条件查询
BasicDBObject bson = new BasicDBObject("_id", "1");
// 执行查询
FindIterable<Document> documents = comment.find(bson);
for (Document document : documents) {
System.out.println("------------------------------");
System.out.println("_id:" + document.get("_id"));
System.out.println("content:" + document.get("content"));
System.out.println("userid:" + document.get("userid"));
System.out.println("thumbup:" + document.get("thumbup"));
}
}
// 新增db.comment.insert({"_id" : "5", "content" : "坚持就是胜利123", "userid" : "1018", "thumbup" : 1212})
@Test
public void test3() {
// 封装新增数据
Map<String, Object> map = new HashMap<>();
map.put("_id", "6");
map.put("content", "新增测试");
map.put("userid", "1019");
map.put("thumbup", "666");
// 封装新增文档对象
Document document = new Document(map);
// 执行新增 新增多条:insertMany(List<Document>)
comment.insertOne(document);
}
// 修改,db.comment.update({"_id" : "5"},{$set:{"userid" : "888"}})
@Test
public void test4() {
//创建修改的条件
BasicDBObject filter = new BasicDBObject("_id", "6");
//创建修改的值
BasicDBObject update = new BasicDBObject("$set", new Document("userid", "999"));
// 执行修改
comment.updateOne(filter, update);
}
// 删除,db.comment.remove({"_id" : "5"})
@Test
public void test5() {
// 创建删除的条件
BasicDBObject bson = new BasicDBObject("_id", "6");
// 执行删除
comment.deleteOne(bson);
}
}
Boot 使用
yml 配置
SpringData 查询
持久层编写,只需要集成指定接口,框架就会扫描到。
而且而且!!!!!!!!!!!!
SpringDataMongoDB,支持通过查询方法名进行查询定义的方式
你只需要按照规定,指定方法名字!!!框架帮你生成 MongoDB 查询语句!
但是一定要继承这个类(只针对查询语句!!!!!),并且springData框架还封装了一些方法,可以直接使用!
import org.springframework.data.mongodb.repository.MongoRepository;
public interface CommentRepository extends MongoRepository<Comment, String> {
//SpringDataMongoDB,支持通过查询方法名进行查询定义的方式
//根据文章id查询文章评论数据
List<Comment> findByArticleid(String articleId);
//根据发布时间和点赞数查询查询
// List<Comment> findByPublishdateAndThumbup(Date date, Integer thumbup);
//根据用户id查询,并且根据发布时间倒序排序
// List<Comment> findByUseridOrderbOrderByPublishdateDesc(String userid);
}
MongoRepository 这和接口默认提供了一些方法,便捷使用
模板类使用
private MongoTemplate mongoTemplate;
上面得查询使用得是SpringData提供得。
针对一些修改操作,还是只有通过模板类进行使用。
解决重复点赞问题
将 用户id和文章id 作为key ,存放到redis中,因为经常读写,所以redis性能更高!
在每次进行点赞操作之前进行判断即可。
在加载时,也可以将对应点赞信息加载出来,在前端给用户设置 点赞按钮不可用
边栏推荐
猜你喜欢

Code audit CMS

Common solutions for distributed ID - take one

MySQL basic learning

如何优雅的复现YOLOv5官方历程(二)——标注并训练自己的数据集

FPGA question brushing sequence detection

Security permission management details

测试必备工具之Fiddler,你真的了解吗?

Go exceed API source code reading (VI) -- deletesheet (sheet string)

Teach you how to use code to realize SSO single sign on

Day011 一维数组
随机推荐
LAMP架构
DOM操作--操作节点
Hack The Box - Web Requests Module详细讲解中文教程
Week 6 Learning Representation: Word Embedding (symbolic →numeric)
ALV程序收集
嵌入式开发小记,实用小知识分享
Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0
普林斯顿微积分读本02第一章--函数的复合、奇偶函数、函数图像
Teach you how to use code to realize SSO single sign on
C语言实现发牌功能基本方法
List converted to tree real use of the project
推荐必读:测试人员如何快速熟悉新业务?
YOLOV3预备工作
[pytorch] install torch 1.8.1 and check whether torch version and GPU are available
Shell process control (emphasis), if judgment, case statement, let usage, for ((initial value; loop control condition; variable change)) and for variable in value 1 value 2 value 3..., while loop
Real scientific weight loss
Week 6 Learning Representation: Word Embedding (symbolic →numeric)
Nacos introduction and deployment
Development to testing: a six-year road to automation from scratch
Hack The Box - Introduction to Networking Module详细讲解中文教程