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 ...
随机推荐
- linux xlearn安装
机器学习中的又一个利器,广泛用于Kaggle或类似的数据比赛. xlearn的优势: 1.通用性好,包括主流的算法(lr, fm, ffm 等),用户不用再切换于不同软件之间 2.性能好,测试 xL ...
- python 输出‘\xe8\xb4\x9d\xe8\xb4\x9d’, ‘\xe6\x99\xb6\xe6\x99\xb6’, ‘\xe6\xac\xa2\xe6\xac\xa2’]
如上代码块,结果输出为: [‘\xe8\xb4\x9d\xe8\xb4\x9d’, ‘\xe6\x99\xb6\xe6\x99\xb6’, ‘\xe6\xac\xa2\xe6\xac\xa2’] 北京 ...
- JVM 自定义类加载器在复杂类情况下的运行分析
一.自定义类加载器在复杂类情况下的运行分析 1.使用之前创建的类加载器 public class MyTest16 extends ClassLoader{ private String classN ...
- linux cat 文件编码
test.log是utf-16的编码 cat test.log会报错 但是我们可以cat的时候指定编码格式 iconv -f 文件编码 -t 终端编码 input.log iconv -f utf-1 ...
- 我大概知道他在说什么了,是对内存单元的竞争访问吧。Python有GIL,在执行伪码时是原子的。但是伪码之间不保证原子性。 UDP丢包,你是不是做了盲发?没有拥塞控制的情况下,确实会出现丢包严重的情况。你先看看发送速率,还有是否带有拥塞控制。
我大概知道他在说什么了,是对内存单元的竞争访问吧.Python有GIL,在执行伪码时是原子的.但是伪码之间不保证原子性. UDP丢包,你是不是做了盲发?没有拥塞控制的情况下,确实会出现丢包严重的情 ...
- Javascript自定义事件功能与用法实例分析
原文地址:https://www.jb51.net/article/127776.htm 本文实例讲述了javascript自定义事件功能与用法.分享给大家供大家参考,具体如下: 概述 自定义事件很难 ...
- mount: unknown filesystem type ‘ntfs’ 解决方法
问题: mount –t ntfs /dev/sde1 /mnt/mount: unknown filesystem type ‘ntfs’ 这是由于CentOS release 6.6(Final) ...
- shell编程系列7--shell中常用的工具find、locate、which、whereis
shell编程系列7--shell中常用的工具find.locate.which.whereis .文件查找之find命令 语法格式:find [路径] [选项] [操作] 选项 -name 根据文件 ...
- PPT插件(islide)
https://www.islide.cc/features iSlide 主要功能模块 一键优化 将PPT中不规则的字体,段落,色彩,参考线布局,风格样式等一键化全局统一设置,构建专业和规范. ...
- [ kvm ] 嵌套虚拟化
1. 前言 在学习 kvm 的过程中,需要在虚拟机中再次开启虚拟机,这里就需要使用到嵌套虚拟化,做个记录吧. 2. 配置嵌套虚拟化 2.1 查看物理机是否支持嵌套虚拟化 cat /sys/module ...