赞
踩
目录
首先我们需要在项目中导入所需要的依赖,有两种方式:在项目创建之初选择对应的依赖
如果项目已经创建则在pom.xml文件中加入以下依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-mongodb</artifactId>
- </dependency>
在依赖导入后,我们需要在项目的配置文件中配置相对应的信息
spring: data: mongodb: host: IP port: 27017 database: 数据库
在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 {
- @Id
- private String id;
- private String nickname;
- private String content;
- @Indexed
- private Integer userId;
- private LocalDateTime createTime;
- private Integer like;
- private Integer parentId;
- }
命令:db.comment.insert({_id:"4",nickname:"ww",content:"这位是谁啊",userId:3,createTime:……})
这种方式插入数据会插入到实体类上Document注解对应的集合中
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- Comment comment = new Comment("4","ww","这位是谁啊",3, LocalDateTime.now(),1,0);
- mongoTemplate.insert(comment);
- }
- }
命令:db.comment.insert({})
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 构建数据
- Comment comment = new Comment("5","ww","这位是老师",3, LocalDateTime.now(),1,0);
- // 2. 存储数据库
- mongoTemplate.insert(comment,"comment");
- }
- }
命令:db.comment.insert([{},{}])
使用insert进行批量插入时必须指定集合名
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 构建数据
- Comment comment1 = new Comment("4","ww","这位是老师啊",3, LocalDateTime.now(),1,0);
- Comment comment2 = new Comment("6","ww","这位是啊",3, LocalDateTime.now(),1,0);
- List<Comment> list = new ArrayList<>();
- list.add(comment1);
- list.add(comment2);
-
- // 2. 存储数据库
- mongoTemplate.insert(list,"comment");
- }
- }
命令:db.comment.save({})
使用save进行插入时会根据id进行判断,如果要插入数据中的id在数据库存在,则会将旧的数据覆盖,如果不存在则插入数据
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 构建数据
- Comment comment = new Comment("4","ww","这位是老师啊12",3, LocalDateTime.now(),1,0);
-
-
- // 2. 存储数据库
- mongoTemplate.save(comment);
- }
- }
命令:db.comment.save({})
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 构建数据
- Comment comment = new Comment("4","ww","这位是老师啊12",3, LocalDateTime.now(),1,0);
-
-
- // 2. 存储数据库
- mongoTemplate.save(comment,"comment");
- }
- }
在MongoTemplate中,save()和insert()方法有以下区别:
命令:db.comment.update({},{})
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void 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);
- }
- }
命令:db.comment.update({},{},{multi:true})
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void 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);
- }
- }
命令:db.comment.remove({})
-
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 构建筛选条件
- Query query = new Query(Criteria.where("userId").is(2));
-
- // 2. 执行删除操作
- DeleteResult remove = mongoTemplate.remove(query, Comment.class);
- }
- }
命令:db.comment.remove({})
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 执行删除操作
- DeleteResult remove = mongoTemplate.remove(new Query(), Comment.class);
- }
- }
命令:db.comment.remove({})
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 构建删除条件
- Query query = new Query(Criteria.where("userId").is(1));
-
- // 2. 删除数据接收返回的文档
- Comment andRemove = mongoTemplate.findAndRemove(query, Comment.class);
-
- // 3. 打印删除的文档
- System.out.println(andRemove);
- }
- }
命令:db.comment.remove({})
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 删除所有满足条件并返回
- List<Comment> comments = mongoTemplate.findAllAndRemove(new Query(Criteria.where("userId").is(3)), Comment.class);
-
- // 2. 打印删除
- System.out.println(comments);
- }
- }
命令:db.comment.find()
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 查询全部文档
- List<Comment> comments = mongoTemplate.findAll(Comment.class);
- // 2. 打印结果
- System.out.println(comments);
- }
- }
命令:db.comment.find({_id:"id"})
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 查询文档
- Comment comment = mongoTemplate.findById("1", Comment.class);
- // 2. 打印结果
- System.out.println(comment);
- }
- }
命令:db.comment.findOne({})
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 查询文档
- Comment comment = mongoTemplate.findOne(new Query(Criteria.where("nickname").is("ww")),Comment.class);
- // 2. 打印结果
- System.out.println(comment);
- }
- }
命令:db.comment.find({})
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 查询文档
- List<Comment> comments = mongoTemplate.find(new Query(Criteria.where("nickname").is("ww")),Comment.class);
- // 2. 打印结果
- System.out.println(comments);
- }
- }
命令:db.comment.find({$and:[{},{}]})
- @SpringBootTest
- public class PersonServiceTest {
-
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- public 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);
- List<Comment> result = mongoTemplate.find(query, Comment.class);
- System.out.println("查询结果:" + result.toString());
- }
- }
命令:db.comment.find({$or:[{},{}]})
下面代码简写,具体可参照上述and写法,效果一致
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 查询文档
- List<Comment> 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);
- }
- }
命令:db.comment.find({count:{$in:[11,12]}})
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 查询文档
- List<Integer> ins = Arrays.asList(1,0,2);
- List<Comment> comments = mongoTemplate.find(new Query(Criteria.where("like").in(ins)), Comment.class);
-
- // 2. 打印结果
- System.out.println(comments);
- }
- }
命令:db.comment.find({ "like": { $gt: 0, $lt: 5 } })
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 查询文档
- Query query = new Query(Criteria.where("like").gt(0).lt(5)); // 查询大于0小于5
- List<Comment> comments = mongoTemplate.find(query, Comment.class);
-
- // 2. 打印结果
- System.out.println(comments);
- }
- }
命令:db.collectionName.find({ "nickname": { $regex: '^w' } })
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 查询文档
- List<Comment> comments = mongoTemplate.find(new Query(Criteria.where("nickname").regex("^w")), Comment.class); // 查询nickname中w开头的数据
-
- // 2. 打印结果
- System.out.println(comments);
- }
- }
命令:db.collectionName.find({ "nickname": "ww" }).sort({ "like": 1 })
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 查询文档
- Query query = new Query(Criteria.where("nickname").is("ww")).with(Sort.by("id").descending());
- List<Comment> comments = mongoTemplate.find(query, Comment.class);
-
- // 2. 打印结果
- System.out.println(comments);
- }
- }
命令:db.comment.find({}).skip(2).limit(2)
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 查询文档
- Query query = new Query(Criteria.where("nickname").is("ww")).skip(2).limit(2);
- List<Comment> comments = mongoTemplate.find(query, Comment.class);
-
- // 2. 打印结果
- System.out.println(comments);
- }
- }
命令:db.comment.count({})
- @SpringBootTest
- class CommentServiceTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void testInsert() {
- // 1. 查询文档
- Query query = new Query(Criteria.where("nickname").is("ww"));
- long count = mongoTemplate.count(query, Comment.class);
-
- // 2. 打印结果
- System.out.println(count);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。