当前位置: 首页 > news >正文

100个免费货源网站网站外链建设培训

100个免费货源网站,网站外链建设培训,岳阳建网站,电子商务是干什么的工作目录 一、导入依赖与配置信息 二、导入测试数据创建实体类 三、插入数据 1、Insert默认集合插入 2、Insert指定集合插入 3、Insert批量插入数据 4、save默认集合插入 5、save指定集合插入 6、insert与save的区别 四、修改数据 1、修改符合条件的第一条数据 2、全…目录 一、导入依赖与配置信息 二、导入测试数据创建实体类 三、插入数据 1、Insert默认集合插入 2、Insert指定集合插入  3、Insert批量插入数据  4、save默认集合插入 5、save指定集合插入  6、insert与save的区别  四、修改数据 1、修改符合条件的第一条数据 2、全部修改 五、删除数据 1、删除满足条件的所有文档 2、删除集合里所有文档 3、删除满足条件的单个文档并返回 4、删除满足条件的所有文档并返回 六、查找数据 1、查询全部文档 2、查询指定id的文档 3、查询满足条件的一条文档 4、查询满足条件的所有文档 5、And查询 6、Or查询 7、In查询 8、比较查询 9、正则查询 10、排序查询 11、分页查询 12、统计查询 一、导入依赖与配置信息 首先我们需要在项目中导入所需要的依赖有两种方式在项目创建之初选择对应的依赖 如果项目已经创建则在pom.xml文件中加入以下依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-mongodb/artifactId/dependency 在依赖导入后我们需要在项目的配置文件中配置相对应的信息 spring:data:mongodb:host: IPport: 27017database: 数据库 二、导入测试数据创建实体类 在MongoDB中导入数据 db.comment.insertMany([{_id:1,nickname:zs,content:这是一本好书,userId:1,createTime:2021-01-01T00:00:00,like:1,parentId:0},{_id:2,nickname:ls,content:可是一本好书,userId:2,createTime:2021-01-01T12:00:00,like:11,parentId:0},{_id:3,nickname:ls,content:不是一本好书,userId:2,createTime:2021-01-11T12:00:00,like:111,parentId:0}]) 创建对应的实体类 package com.example.demo.pojo.mongodbVo;import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.mapping.Document;import java.io.Serializable; import java.time.LocalDateTime;Data Document(collection comment) public class Comment implements Serializable {Idprivate String id;private String nickname;private String content;Indexedprivate Integer userId;private LocalDateTime createTime;private Integer like;private Integer parentId; }三、插入数据 1、Insert默认集合插入 命令db.comment.insert({_id:4,nickname:ww,content:这位是谁啊,userId:3,createTime:……}) 这种方式插入数据会插入到实体类上Document注解对应的集合中 SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {Comment comment new Comment(4,ww,这位是谁啊,3, LocalDateTime.now(),1,0);mongoTemplate.insert(comment);} } 2、Insert指定集合插入  命令db.comment.insert({}) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 构建数据Comment comment new Comment(5,ww,这位是老师,3, LocalDateTime.now(),1,0);// 2. 存储数据库mongoTemplate.insert(comment,comment);} } 3、Insert批量插入数据  命令db.comment.insert([{},{}]) 使用insert进行批量插入时必须指定集合名 SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 构建数据Comment comment1 new Comment(4,ww,这位是老师啊,3, LocalDateTime.now(),1,0);Comment comment2 new Comment(6,ww,这位是啊,3, LocalDateTime.now(),1,0);ListComment list new ArrayList();list.add(comment1);list.add(comment2);// 2. 存储数据库mongoTemplate.insert(list,comment);} } 4、save默认集合插入 命令db.comment.save({}) 使用save进行插入时会根据id进行判断如果要插入数据中的id在数据库存在则会将旧的数据覆盖如果不存在则插入数据 SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 构建数据Comment comment new Comment(4,ww,这位是老师啊12,3, LocalDateTime.now(),1,0);// 2. 存储数据库mongoTemplate.save(comment);} } 5、save指定集合插入  命令db.comment.save({}) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 构建数据Comment comment new Comment(4,ww,这位是老师啊12,3, LocalDateTime.now(),1,0);// 2. 存储数据库mongoTemplate.save(comment,comment);} } 6、insert与save的区别  在MongoTemplate中save()和insert()方法有以下区别 save()方法save()方法用于插入新文档或更新现有文档。如果要保存的文档没有id字段将插入一个新文档。如果文档具有id字段MongoDB将尝试使用匹配的id值更新文档。如果找不到具有匹配id的文档则插入一个新文档。  insert()方法insert()方法用于向集合中插入新文档。如果要插入的文档已经具有id字段并且集合中已经存在具有相同id值的文档则会抛出异常。这确保插入的文档具有唯一的_id值。 总结save()方法用于插入和更新操作而insert()方法专门用于插入新文档并确保_id字段的唯一性。 四、修改数据 1、修改符合条件的第一条数据 命令db.comment.update({},{}) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 构建更新的数据Comment comment new Comment(4,ww,这位是老师啊12,3, LocalDateTime.now(),1,0);// 2. 构建更新的条件Query query new Query(Criteria.where(id).is(comment.getId()));// 3. 构建更新值Update update new Update().set(content,comment.getContent()).set(createTime,comment.getCreateTime());// 4. 更新第一条满足条件的数据UpdateResult updateResult mongoTemplate.updateFirst(query, update, Comment.class);// 5. 判断是否更新System.out.println(update);} } 2、全部修改 命令db.comment.update({},{},{multi:true}) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 构建更新的数据Comment comment new Comment(4,ww,修改全部数据,3, LocalDateTime.now(),1,0);// 2. 构建更新的条件Query query new Query(Criteria.where(nickname).is(comment.getNickname()));// 3. 构建更新值Update update new Update().set(content,comment.getContent()).set(createTime,comment.getCreateTime());// 4. 更新第一条满足条件的数据UpdateResult updateResult mongoTemplate.updateMulti(query, update, Comment.class);// 5. 判断是否更新System.out.println(update);} } 五、删除数据 1、删除满足条件的所有文档 命令db.comment.remove({}) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 构建筛选条件Query query new Query(Criteria.where(userId).is(2));// 2. 执行删除操作DeleteResult remove mongoTemplate.remove(query, Comment.class);} } 2、删除集合里所有文档 命令db.comment.remove({}) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 执行删除操作DeleteResult remove mongoTemplate.remove(new Query(), Comment.class);} } 3、删除满足条件的单个文档并返回 命令db.comment.remove({}) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 构建删除条件Query query new Query(Criteria.where(userId).is(1));// 2. 删除数据接收返回的文档Comment andRemove mongoTemplate.findAndRemove(query, Comment.class);// 3. 打印删除的文档System.out.println(andRemove);} } 4、删除满足条件的所有文档并返回 命令db.comment.remove({}) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 删除所有满足条件并返回ListComment comments mongoTemplate.findAllAndRemove(new Query(Criteria.where(userId).is(3)), Comment.class);// 2. 打印删除System.out.println(comments);} } 六、查找数据 1、查询全部文档 命令db.comment.find() SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 查询全部文档ListComment comments mongoTemplate.findAll(Comment.class);// 2. 打印结果System.out.println(comments);} } 2、查询指定id的文档 命令db.comment.find({_id:id}) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 查询文档Comment comment mongoTemplate.findById(1, Comment.class);// 2. 打印结果System.out.println(comment);} } 3、查询满足条件的一条文档 命令db.comment.findOne({}) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 查询文档Comment comment mongoTemplate.findOne(new Query(Criteria.where(nickname).is(ww)),Comment.class);// 2. 打印结果System.out.println(comment);} } 4、查询满足条件的所有文档 命令db.comment.find({}) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 查询文档ListComment comments mongoTemplate.find(new Query(Criteria.where(nickname).is(ww)),Comment.class);// 2. 打印结果System.out.println(comments);} } 5、And查询 命令db.comment.find({$and:[{},{}]}) SpringBootTest public class PersonServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testpublic void findByAndCondition() {// 1.创建将要and的条件Criteria criteriaNickName Criteria.where(nickame).is(ww);Criteria criteriaLike Criteria.where(like).is(1);// 2.将上面条件进行and关联Criteria criteria new Criteria().andOperator(criteriaNickName, criteriaLike);// 3.条件对象添加到其中Query query new Query(criteria);ListComment result mongoTemplate.find(query, Comment.class);System.out.println(查询结果 result.toString());} } 6、Or查询 命令db.comment.find({$or:[{},{}]}) 下面代码简写具体可参照上述and写法效果一致 SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 查询文档ListComment comments mongoTemplate.find(new Query(new Criteria().orOperator(Criteria.where(nickname).is(ww),Criteria.where(like).is(10))), Comment.class);// 2. 打印结果System.out.println(comments);} } 7、In查询 命令db.comment.find({count:{$in:[11,12]}}) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 查询文档ListInteger ins Arrays.asList(1,0,2);ListComment comments mongoTemplate.find(new Query(Criteria.where(like).in(ins)), Comment.class);// 2. 打印结果System.out.println(comments);} } 8、比较查询 命令db.comment.find({ like: { $gt: 0, $lt: 5 } }) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 查询文档Query query new Query(Criteria.where(like).gt(0).lt(5)); // 查询大于0小于5ListComment comments mongoTemplate.find(query, Comment.class);// 2. 打印结果System.out.println(comments);} } 9、正则查询 命令db.collectionName.find({ nickname: { $regex: ^w } }) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 查询文档ListComment comments mongoTemplate.find(new Query(Criteria.where(nickname).regex(^w)), Comment.class); // 查询nickname中w开头的数据// 2. 打印结果System.out.println(comments);} } 10、排序查询 命令db.collectionName.find({ nickname: ww }).sort({ like: 1 }) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 查询文档Query query new Query(Criteria.where(nickname).is(ww)).with(Sort.by(id).descending());ListComment comments mongoTemplate.find(query, Comment.class);// 2. 打印结果System.out.println(comments);} } 11、分页查询 命令db.comment.find({}).skip(2).limit(2) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 查询文档Query query new Query(Criteria.where(nickname).is(ww)).skip(2).limit(2);ListComment comments mongoTemplate.find(query, Comment.class);// 2. 打印结果System.out.println(comments);} } 12、统计查询 命令db.comment.count({}) SpringBootTest class CommentServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testvoid testInsert() {// 1. 查询文档Query query new Query(Criteria.where(nickname).is(ww));long count mongoTemplate.count(query, Comment.class);// 2. 打印结果System.out.println(count);} }
http://www.zqtcl.cn/news/895208/

相关文章:

  • 记事本做网站素材代码国内十大4a广告公司
  • 一米八效果图网站商业网站平台
  • 做搜狗手机网站优化产品推广计划怎么写
  • 网站链接优化怎么做ftp服务器
  • 什么网站可以接单做海报网站信息员队伍建设方案
  • 淘宝联盟 网站怎么做网站运营推广方案设计
  • 网站建设数据库类型百度seo现状
  • 德州网站优化公司平面设计公司企业logo设计
  • 山东平台网站建设价位网站广告文案
  • 可以做哪方面的网站万网董事长是谁
  • 京东网站开发费用程序员找工作的网站
  • 怎么做网站首页psdwordpress 注册验证
  • 商丘做网站的公司有哪些郑州网站公司排名
  • 竞价网站与竞价网站之间做友情链接企业邮箱查询
  • 国外jquery网站wordpress 下一页 模板
  • 安卓手机做网站云南建设厅网站职称评定
  • 国外域名注册商网站邮箱登陆登录入口
  • 男女做那个的网站是什么深圳市8号公告
  • 做网站收款支付宝接口廊坊市网站建设公司
  • 文档下载网站 建设做cpa用什么网站
  • 网站制作合同注意事项百度网页版电脑版
  • 怎样做模板网站手机营销型网站制作
  • 如何采集网站内容如何做网站导航栏的搜索引擎优化
  • 网站关键词排名外包织梦大气婚纱影楼网站源码
  • 网站建设执行力冠县哪里有做网站的
  • 免费网站推广咱们做网络营销推广的应用场景
  • 深圳正规网站制作哪家公司好做网站代理属于开设赌场罪吗
  • 江西宜春市建设局网站wordpress博客下载器
  • 汕头站扩建效果图微信怎么引流营销呢
  • 小学学校网站建设计划wordpress博客示例