网站建设合同封面模板下载,黑龙江建设网监理证书,网站设计与建设系统,凡科网做的网站能直接用吗文章目录 一、环境准备二、集合操作三、文档操作3.1 实体类3.2 添加文档3.3 查询文档3.4 修改文档3.5 删除文档 提示#xff1a;以下是本篇文章正文内容#xff0c;MongoDB 系列学习将会持续更新 一、环境准备
①添加 SpringData 依赖#xff1a;
dependency… 文章目录 一、环境准备二、集合操作三、文档操作3.1 实体类3.2 添加文档3.3 查询文档3.4 修改文档3.5 删除文档 提示以下是本篇文章正文内容MongoDB 系列学习将会持续更新 一、环境准备
①添加 SpringData 依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-mongodb/artifactId
/dependency②配置 yml 文件官方连接配置文档
方式一
spring:data:mongodb:uri: mongodb://root:1234561.15.76.95:27017/library?authSourceadmin方式二
spring:data:mongodb:username: rootpassword: 123456host: 1.15.76.95port: 27017database: libraryauthentication-database: admin③直接注入 MongoTemplate 进行操作
Resource
MongoTemplate mongoTemplate;二、集合操作
Test
public void testCollection(){boolean exists mongoTemplate.collectionExists(borrow);if(exists) {// 删除集合mongoTemplate.dropCollection(borrow);}else {// 创建集合mongoTemplate.createCollection(borrow);}SetString collectionNames mongoTemplate.getCollectionNames();System.out.println(collectionNames.toString()); // [borrow, book, user]
}回到目录…
三、文档操作
3.1 实体类
Document 修饰范围用在类上。作用用来映射这个类的一个对象为 Mongo 中一条文档数据。属性(value 、collection) 用来指定操作的集合名称。 MongoId 修饰范围用在成员变量、方法上。作用用来将成员变量的值映射为文档的 _id 的值。 Field 修饰范围用在成员变量、方法上。作用用来将成员变量及其值映射为文档中一个 key:value 对。属性(name , value) 用来指定在文档中 key 的名称,默认为成员变量名。 Transient 修饰范围用在成员变量、方法上。作用用来指定此成员变量不参与文档的序列化。
Data
AllArgsConstructor
NoArgsConstructor
Document(user)
public class User {MongoIdprivate Integer uid;Fieldprivate String name;Fieldprivate String password;Fieldprivate Integer age;
}回到目录…
3.2 添加文档
insert 方法返回值是新增的 Document 对象里面包含了新增后 id 的值。如果集合不存在会自动创建集合。
Test
public void test1() {// _id存在时更新数据mongoTemplate.save(new User(1, aaa, 123456, 20));// _id存在时抛出异常mongoTemplate.insert(new User(2, bbb, 113456, 21));ListUser list Arrays.asList(new User(3, ccc, 124266, 22),new User(4, ddd, 136521, 23),new User(5, eee, 147258, 24));// 批量插入mongoTemplate.insert(list, User.class);
}通过 Spring Data MongoDB 还会给集合中多加一个 _class 的属性存储新增时 Document 对应 Java 中类的全限定路径。这么做为了查询时能把 Document 转换为 Java 类型。
回到目录…
3.3 查询文档
Query 类作为查询条件的容器用于放置 Criteria 条件接口。Criteria 是标准查询的接口可以引用静态的 Criteria.where() 将字段和条件组合在一起进行查询。
Test
public void test1() {System.out.println(查询所有文档);ListUser list mongoTemplate.findAll(User.class);list.forEach(System.out::println);System.out.println(根据_id查询);User user mongoTemplate.findById(3, User.class);System.out.println(user);System.out.println(findOne返回第一个文档);User one mongoTemplate.findOne(new Query(), User.class);System.out.println(one);
}Test
public void test2() {System.out.println(条件查询);// 查询name为eee的用户Query query1 new Query(Criteria.where(name).is(eee));// 查询 age23 的用户Query query2 new Query(Criteria.where(age).lt(23));// 查询 21age24 的用户Query query3 new Query(Criteria.where(age).gte(21).lt(24));// 模糊查询Query query4 new Query(Criteria.where(password).regex(123));System.out.println(多条件查询);// 查询 age24 且 密码包含123 的用户Criteria criteria1 new Criteria();criteria1.andOperator(Criteria.where(age).lt(24), Criteria.where(password).regex(123));Query query5 new Query(criteria1);// 查询 age23 或 密码包含456 的用户Criteria criteria2 new Criteria();criteria2.orOperator(Criteria.where(age).gt(23), Criteria.where(password).regex(456));Query query6 new Query(criteria2);ListUser list mongoTemplate.find(query6, User.class);list.forEach(System.out::println);
}Test
public void test3() {System.out.println(排序);Query query1 new Query();query1.with(Sort.by(Sort.Order.desc(age)));System.out.println(分页);Query query2 new Query();query2.skip(0).limit(3);ListUser list mongoTemplate.find(query2, User.class);list.forEach(System.out::println);
}使用 JSON 字符串方式查询
Test
public void testFindByJson() {// 等值查询Query query1 new BasicQuery({name:eee});// 多条件查询Query query2 new BasicQuery({age:{$lt:24}, password:{$regex:123}});Query query3 new BasicQuery({$or:[{age:{$gt:23}}, {password:{$regex:456}}]});ListUser list mongoTemplate.find(query3, User.class);list.forEach(System.out::println);
}回到目录…
3.4 修改文档
在 Mongodb 中无论是使用客户端 API 还是使用 Spring Data更新返回结果一定是受行数影响。如果更新后的结果和更新前的结果是相同返回 0。
updateFirst()只更新满足条件的第一条记录。updateMulti()更新所有满足条件的记录。upsert()没有符合条件的记录则插入数据。
Test
public void test1() {Query query new Query(Criteria.where(name).is(eee));Update update new Update();update.set(age, 25);// updateFirst() 只更新满足条件的第一条记录UpdateResult updateResult mongoTemplate.updateFirst(query, update, User.class);System.out.println(返回修改的记录数: updateResult.getModifiedCount());
}Test
public void test2() {Query query new Query(Criteria.where(age).gt(23));Update update new Update();update.inc(age, 1);// updateMulti() 更新所有满足条件的记录UpdateResult updateResult mongoTemplate.updateMulti(query, update, User.class);System.out.println(返回修改的记录数: updateResult.getModifiedCount());
}Test
public void test3() {// query查询结果不存在Query query new Query(Criteria.where(name).is(ggg));Update update new Update();update.set(age, 28);update.setOnInsert(_id, 7); //不存在时插入// upsert() 没有符合条件的记录则插入数据UpdateResult updateResult mongoTemplate.upsert(query, update, User.class);System.out.println(返回修改的记录数: updateResult.getModifiedCount());
}3.5 删除文档
Test
public void testDelete() {//删除所有文档, 不如用dropCollection()//mongoTemplate.remove(new Query(),Employee.class);//条件删除Query query new Query(Criteria.where(name).is(fff));mongoTemplate.remove(query, User.class);
}回到目录… 总结: 提示这里对文章进行总结 本文是对MongoDB的学习SpringBoot整合MongoDB的SpringData配置文件中如何连接实体类如何映射到文档如何通过API进行集合操作和文档的增删查改。之后的学习内容将持续更新 文章转载自: http://www.morning.hbhnh.cn.gov.cn.hbhnh.cn http://www.morning.wnjrf.cn.gov.cn.wnjrf.cn http://www.morning.pdgqf.cn.gov.cn.pdgqf.cn http://www.morning.srnth.cn.gov.cn.srnth.cn http://www.morning.pynzj.cn.gov.cn.pynzj.cn http://www.morning.qscsy.cn.gov.cn.qscsy.cn http://www.morning.plqsz.cn.gov.cn.plqsz.cn http://www.morning.tpnx.cn.gov.cn.tpnx.cn http://www.morning.nlgnk.cn.gov.cn.nlgnk.cn http://www.morning.c7510.cn.gov.cn.c7510.cn http://www.morning.ljbpk.cn.gov.cn.ljbpk.cn http://www.morning.wcqkp.cn.gov.cn.wcqkp.cn http://www.morning.mxxsq.cn.gov.cn.mxxsq.cn http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn http://www.morning.kkjhj.cn.gov.cn.kkjhj.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.kqzt.cn.gov.cn.kqzt.cn http://www.morning.mcgsq.cn.gov.cn.mcgsq.cn http://www.morning.bpmfn.cn.gov.cn.bpmfn.cn http://www.morning.xhddb.cn.gov.cn.xhddb.cn http://www.morning.smdkk.cn.gov.cn.smdkk.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.wjxtq.cn.gov.cn.wjxtq.cn http://www.morning.xlwpz.cn.gov.cn.xlwpz.cn http://www.morning.lpcpb.cn.gov.cn.lpcpb.cn http://www.morning.yysqz.cn.gov.cn.yysqz.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.ydwnc.cn.gov.cn.ydwnc.cn http://www.morning.ljsxg.cn.gov.cn.ljsxg.cn http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.ckhpg.cn.gov.cn.ckhpg.cn http://www.morning.srwny.cn.gov.cn.srwny.cn http://www.morning.sqqds.cn.gov.cn.sqqds.cn http://www.morning.crdtx.cn.gov.cn.crdtx.cn http://www.morning.grpbt.cn.gov.cn.grpbt.cn http://www.morning.gbcnz.cn.gov.cn.gbcnz.cn http://www.morning.xfmzk.cn.gov.cn.xfmzk.cn http://www.morning.chhhq.cn.gov.cn.chhhq.cn http://www.morning.fmswb.cn.gov.cn.fmswb.cn http://www.morning.rxxdk.cn.gov.cn.rxxdk.cn http://www.morning.bnxfj.cn.gov.cn.bnxfj.cn http://www.morning.bktly.cn.gov.cn.bktly.cn http://www.morning.bnpn.cn.gov.cn.bnpn.cn http://www.morning.bfysg.cn.gov.cn.bfysg.cn http://www.morning.dygqq.cn.gov.cn.dygqq.cn http://www.morning.thrcj.cn.gov.cn.thrcj.cn http://www.morning.gwgjl.cn.gov.cn.gwgjl.cn http://www.morning.ssjtr.cn.gov.cn.ssjtr.cn http://www.morning.lsfzq.cn.gov.cn.lsfzq.cn http://www.morning.demoux.com.gov.cn.demoux.com http://www.morning.dtrzw.cn.gov.cn.dtrzw.cn http://www.morning.xqspn.cn.gov.cn.xqspn.cn http://www.morning.nnpfz.cn.gov.cn.nnpfz.cn http://www.morning.lstmg.cn.gov.cn.lstmg.cn http://www.morning.qbgff.cn.gov.cn.qbgff.cn http://www.morning.rjznm.cn.gov.cn.rjznm.cn http://www.morning.cmdfh.cn.gov.cn.cmdfh.cn http://www.morning.8yitong.com.gov.cn.8yitong.com http://www.morning.nqdkx.cn.gov.cn.nqdkx.cn http://www.morning.pgkpt.cn.gov.cn.pgkpt.cn http://www.morning.yjtnc.cn.gov.cn.yjtnc.cn http://www.morning.dsprl.cn.gov.cn.dsprl.cn http://www.morning.xxwl1.com.gov.cn.xxwl1.com http://www.morning.gywxq.cn.gov.cn.gywxq.cn http://www.morning.klzdy.cn.gov.cn.klzdy.cn http://www.morning.gbnsq.cn.gov.cn.gbnsq.cn http://www.morning.schwr.cn.gov.cn.schwr.cn http://www.morning.ckrnq.cn.gov.cn.ckrnq.cn http://www.morning.nnpwg.cn.gov.cn.nnpwg.cn http://www.morning.rmlz.cn.gov.cn.rmlz.cn http://www.morning.xkjrs.cn.gov.cn.xkjrs.cn http://www.morning.txzqf.cn.gov.cn.txzqf.cn http://www.morning.wkrkb.cn.gov.cn.wkrkb.cn http://www.morning.yrlfy.cn.gov.cn.yrlfy.cn http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn http://www.morning.nmbbt.cn.gov.cn.nmbbt.cn http://www.morning.frpm.cn.gov.cn.frpm.cn http://www.morning.gnhsg.cn.gov.cn.gnhsg.cn http://www.morning.fchkc.cn.gov.cn.fchkc.cn