springboot调用mongo
随便测试了一下,有问题请指出
pom.xml中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
application.yml中添加连接信息
spring:
data:
mongodb:
uri: mongodb://xxx:27017/school
测试类
@SpringBootTest
@RunWith(SpringRunner.class)
public class MongoDBTest {
@Autowired
private MongoTemplate mongoTemplate;
/*@Autowired
private MongoRepository mongoRepository;*/
@Autowired
private GridFsTemplate gridFsTemplate;
....
}
添加
通过三种形式来添加数据到mongo
@Test
public void add() {
// 1. 这种插入的数据的id 就直接是mongodb中的_id
Student student = new Student(1, "鲁班7号", "1", 80D, Arrays.asList("玩", "虚坤", "天书世界", "贪玩蓝月"));
mongoTemplate.insert(student);
// 2. 使用类似map的写法,这种插入的id,是mongodb中的一个字段,会另外自动生成_id
BasicDBObject obj = new BasicDBObject();
obj.put("id", 2);
obj.put("name", "cxk");
obj.put("score", 95);
obj.put("clazz", "2");
String[] hobby = {"唱", "跳", "篮球", "rap"};
obj.put("hobby", hobby);
mongoTemplate.insert(obj, "student");
// 3. 使用json数据 会自动另外生成_id
BasicDBObject parse = BasicDBObject.parse("{\n" +
" \"id\": 3,\n" +
" \"clazz\": \"2\",\n" +
" \"score\": 90,\n" +
" \"name\": \"渣渣辉\",\n" +
" \"hobby\":[\"贪\",\"玩\",\"蓝\", \"月\"]\n" +
"}");
mongoTemplate.insert(parse, "student");
}
student.java
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Builder
public class Student {
// 与mongodb中的_id对应
private Integer id;
private String name;
/**
* 班级
*/
private String clazz;
private Double score;
private List<String> hobby;
}
删除
@Test
public void delete() {
// 根据id删除
// DeleteResult id = mongoTemplate.remove(Query.query(Criteria.where("_id").is(1)), "student");
Query query = new Query();
mongoTemplate.remove(query, "student");
}
文档操作更新
/**
* 文档操作
*/
@Test
public void update() {
// 只修改第一个条记录
mongoTemplate.updateFirst(Query.query(Criteria.where("id").gt(2)), Update.update("name", "鸡太美"), "student");
// 修改匹配到的所有数据
mongoTemplate.updateMulti(Query.query(Criteria.where("id").gt(2)), Update.update("name", "鸡太美"), "student");
// 向文档中添加数据 有则跟新 没有则添加
Update update = new Update();
update.addToSet("desc", "练习时长两年半的练习生");
mongoTemplate.upsert(Query.query(Criteria.where("id").gte(2)), update, "student");
// 删除文档中的数据
Update delUpdate = new Update();
delUpdate.unset("desc");
mongoTemplate.updateMulti(Query.query(Criteria.where("id").gt(2)), delUpdate, "student");
}
简单聚合操作 count, distinct
/**
* 简单聚合操作 count, distinct
*/
@Test
public void runCommongd() {
/************* count *********************/
// student集合中的总人数 select count(1) from student
Document document = mongoTemplate.executeCommand("{count: 'student'}");
mongoTemplate.getCollection("student").countDocuments();
System.out.println(document);
// 班级为2的人数 注意字符串要有引号 js中单引号和双引号没啥区别 select count(1) from student where clazz = '2'
Document document1 = mongoTemplate.executeCommand("{count: 'student', query:{clazz: {$eq:'2'}}}");
System.out.println(document1);
/************* distinct **************/
// 去掉重复的班级 select distinct clazz from student
Document document2 = mongoTemplate.executeCommand("{distinct: 'student', key: 'clazz'}");
mongoTemplate.getCollection("person").distinct("clazz", Document.class);
System.out.println(document2);
}
普通查询
/**
* 查询
*/
@Test
public void findTest() {
// 这个id默认是mognodb中的_id
Student student = mongoTemplate.findById(1, Student.class, "student");
System.out.println(student);
Document doc = mongoTemplate.findById("5da57cb7f150ea3be420daf8", Document.class, "student");
//Document{{_id=5da57cb7f150ea3be420daf8, id=2, name=cxk, hobby=[唱, 跳, rap]}}
System.out.println(doc);
// Criteria用来构建条件 Query 用来封装所有条件
Query query = new Query(Criteria.where("_id").is("5da57cb7f150ea3be420daf9"));
Document one = mongoTemplate.findOne(query, Document.class, "student");
System.out.println(one);
// 正则表达式查询 (查询名字中有数字的数据) 如果不需要后面的Pattern.CASE_INSENSITIVE,直接写正则字符串就行
//Criteria regCriteria = Criteria.where("name").regex(Pattern.compile(".*\\d+.*", Pattern.CASE_INSENSITIVE));
Criteria regCriteria = Criteria.where("name").regex(".*\\d+.*");
Query regQuery = new Query(regCriteria);
List<Student> student1 = mongoTemplate.find(regQuery, Student.class, "student");
System.out.println(student1);
// 查询文档中的部分记录
BasicDBObject dbObject = new BasicDBObject();
// 添加查询条件 等于的那种
//dbObject.put("id", 2);
// 指定返回的字段
BasicDBObject fieldsObject = new BasicDBObject();
fieldsObject.put("id", true);
fieldsObject.put("name", true);
fieldsObject.put("_id", false);
Query basicQuery = new BasicQuery(dbObject.toJson(), fieldsObject.toJson());
// 添加查询条件 更灵活
query.addCriteria(Criteria.where("id").gt(2));
List<Document> docs = mongoTemplate.find(basicQuery, Document.class, "student");
System.out.println(docs);
}
分组
group
/**
* group分组
*/
@Test
public void group() {
// 查找80分以上的人的平均得分
GroupBy groupBy = GroupBy.key("clazz")
.initialDocument("{total:0, count:0}")
// curr表示当前doc文档,result表示上一次处理之后的{total:0, count:0}对象
.reduceFunction("function(curr, result) {result.total += curr.score; result.count++}")
// 类似于having,比having更强大,js语法去操作最后的结果
.finalizeFunction("function(result) {result.avg = Math.round(result.total/result.count);}");
Criteria criteria = Criteria.where("score").gte(80);
GroupByResults<Document> brs = mongoTemplate.group(criteria, "student", groupBy, Document.class);
for (Document document : brs) {
System.out.println(document);
}
}
Aggregate
/**
* aggregate聚合查询
*/
@Test
public void aggregateTest() {
//封装查询条件
// 按班级clazz分组查询得分80以上的总分数
List<AggregationOperation> operations = new ArrayList<>();
// where
operations.add(Aggregation.match(Criteria.where("score").gte(80)));
// group by [这个求sum]
operations.add(Aggregation.group("clazz").sum("score").as("totleScore"));
// 这个求count
//operations.add(Aggregation.group("clazz").count().as("totleScore"));
// having
//operations.add(Aggregation.match(Criteria.where("totleScore").gt(80)));
Aggregation aggregation = Aggregation.newAggregation(operations);
//查询、并获取结果
AggregationResults<Document> results = mongoTemplate.aggregate(aggregation, "student", Document.class);
Document map = results.getRawResults();
System.out.println(map);
}
mapReduce
@Test
public void mapReduce() {
Query query = new Query();
// emit中 key:指定分组的字段;values:要聚合的字段(数组)
String mapFunction = "function() {emit(this.clazz, this.score);}";
// key:分组字段,values:根据key分组之后的值放在一个数组中,这个values就是这个数组
String reduceFunction = "function(key, values) {return Array.sum(values);}";
// 可以对结果做一些处理
MapReduceOptions mapReduceOptions = new MapReduceOptions();
// mapReduceOptions.outputCollection("aaaa"); //生成的结果表(在mongo服务器上可以看到)
// mapReduceOptions.limit(2);
MapReduceResults<Document> results = mongoTemplate.mapReduce(query, "student", mapFunction, reduceFunction, mapReduceOptions, Document.class);
Iterator<Document> it = results.iterator();
for (;it.hasNext();) {
System.out.println(it.next());
}
// 查询每个人的兴趣爱好个数
Query query1 = new Query();
String mapFunction1 = "function() {emit(this.name, this.hobby);}";
String reduceFunction1 = "function(key, values) {" +
"reduceVal = {name: key, hobbys: values};" +
"return reduceVal;" +
"}";
String func_finalize = "function(name, hobbys) {return hobbys.length}";
MapReduceOptions mapReduceOptions1 = new MapReduceOptions();
mapReduceOptions1.finalizeFunction(func_finalize);
MapReduceResults<Document> results1 = mongoTemplate.mapReduce(query1, "student", mapFunction1, reduceFunction1, mapReduceOptions1, Document.class);
for (Iterator<Document> it1 = results1.iterator(); it1.hasNext();) {
System.out.println(it1.next());
}
}
group和mapReduce的区别在于group不能跨片查询,如果是分片集群的话 使用mapReduce
查看mongo拓展的js的Array方法:
> for (var key in Array) {
print(key)
}
contains
unique
shuffle
tojson
fetchRefs
sum
avg
stdDev
分页查询
/**
* 分页查询
*/
@Test
public void pageQuery() {
Query query = new Query();
query.skip(1);
query.limit(2);
List<Document> student = mongoTemplate.find(query, Document.class, "student");
System.out.println(student);
}
文件上传
/**
* 文件上传
*/
@Test
public void uploadGridFs() throws Exception {
File file = new File("E:\\1.xml");
FileInputStream fin = new FileInputStream(file);
Document doc = new Document();
doc.put("filename", file.getName());
doc.put("uploadDate", new Date());
doc.put("author", "joe");
gridFsTemplate.store(fin, file.getName(), "xml", doc);
}
文件下载
/**
* 文件下载
*/
@Test
public void downLoadGridFs() throws Exception {
Query query = new Query();
// query.addCriteria(Criteria.where("filename").is("1.xml"));
query.addCriteria(Criteria.where("metadata.author").is("joe"));
// 1.从fs.files中查询文件的相关信息
GridFSFile gfsFile = gridFsTemplate.findOne(query);
// 2.从fs.chunks中获取文件(通过1中查询的files_id)
GridFsResource resource = gridFsTemplate.getResource(gfsFile);
InputStream in = resource.getInputStream();
FileOutputStream fout = new FileOutputStream(new File("E:\\1bak.xml"));
try {
byte[] buf = new byte[1024];
for (int len;(len = in.read(buf, 0, 1024)) != -1;) {
fout.write(buf, 0, len);
}
fout.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
springboot调用mongo的更多相关文章
- java springboot调用第三方接口 借助hutoool工具类 爬坑
楼主是个后端小白一枚,之前没接触过后端,只学了java基本语法,还是在学校老师教的,学的很浅,什么ssh.ssm框架都没有学,最近在自学spring boot,看书学也看不是很懂,就在b站上看教学视频 ...
- springboot调用微信的jscode2session报JSONObject异常
问题背景: 服务器为Centos6.5 JDK:OpenJDK1.7 tomcat7 项目为微信小程序api的后端代码,解密用户信息,代码是采用springboot写的 异常信息: 代码: json异 ...
- springboot 调用webservice
参考:https://www.liangzl.com/get-article-detail-12711.html https://www.cnblogs.com/e206842/p/9047294.h ...
- Springboot调用Oracle存储过程的几种方式
因工作需要将公司SSH项目改为Spingboot项目,将项目中部分需要调用存储过程的部分用entityManagerFactory.unwrap(SessionFactory.class).openS ...
- 求助:springboot调用存储过程并使用了pagehelper分页时报错com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
存储过程如下: dao层的sql Controller层调用: html页面 没有使用pagehelper分页之前,可以正常使用 使用了pagehelper之后就报错 ### Error queryi ...
- springboot整合mongo多数据源
该实例已测试 POM.XML <!-- Spring Boot mongodb 依赖--> <dependency> <groupId>org.springfram ...
- springboot集成mongo
大家可以关注我的微信公众号“秦川以北” 后续更多精彩实用内容分享 在项目中配置,mongoDB数据库,spring整合 1. 引入pom依赖 <dependency> <group ...
- springboot调用http方式
1.httpclient 2.okhttp 3.feign 安装Gradle包 compile 'org.springframework.cloud:spring-cloud-starter-open ...
- SpringBoot 调用 mysql存储过程的实战
网络上写的一堆都不能用的 好吧.. 首先创建 存储过程 DROP PROCEDURE IF EXISTS dfsSons; CREATE PROCEDURE dfsSons(IN rootid INT ...
随机推荐
- vue中样式被覆盖的问题
在我们引入外部的样式时,发现自己无论如何都改不了外部的样式,自己的样式老被覆盖,究其原因还是我们的 外部样式放的位置不对 main.js 我们应该在 main.js 的开头引入样式,这样的话就不存在覆 ...
- Kali Linux又增加一个顶级域名kali.download
Kali Linux又增加一个顶级域名kali.download 现阶段,kali.download只提供软件包和镜像下载.大家可以把该域名作为备选软件源来使用.形式如下: deb http://k ...
- ffmpeg-php扩展
php视频缩略图,较常用的是ffmpeg-php 1: 安装 ffmpeg ffmpeg的下载链接 http://ffmpeg.org/download.html 解压安装包 tar -jxvf f ...
- radioButon的使用
界面: <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android=& ...
- Python 保存数据的方法:
open函数保存 使用with open()新建对象 写入数据(这里使用的是爬取豆瓣读书中一本书的豆瓣短评作为例子) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例
既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...
- ES6深入浅出-6 ES 6 模块-1.模块化速学
把模块先默认认为是豆腐块 为什么前端需要模块? 没有模块的方式 预览这个html页面 一共200行js代码 前100行在做一件事 ,另外100行在做另一件事,这样就是两个模块 main.js来 ...
- linux记录-安装zabbix监控系统
1. 安装依赖yum -y install libcurl-devel libxml2-devel net-snmp net-snmp-devel2. 安装 nginxyum -y install n ...
- String类型字符 常用的方法
例子: String r=“我是谁” System.out.println(r.length())
- Linux系统调优——内核相关参数(五)
修改内核参数有3种办法:一种临时修改,两种永久修改. 临时修改是使用sysctl [选项] [参数名=值]命令:永久修改是修改/etc/sysctl.conf文件或修改/proc/sys/目录下的对应 ...