在 Spring Data MongoDB 中,你可以使用下面五种重载方法中的任意一个方法实现从数据库中删除文档:
(1)从 GOT 集合中删除由其 _id 指定的单个文档:
template.remove(tywin, "GOT");
(2)从 GOT 集合中删除符合查询条件的所有文档:
template.remove(query(where("lastname").is("lannister")), "GOT");
(3)删除 GOT 集合中的前三个文档。与(2)不同,要删除的文档由其 id 标识,运行给定的查询,首先应用排序(sort)、限制(limit)和跳过(skip)选项,然后在单独的步骤中一次性删除所有文档:
template.remove(new Query().limit(3), "GOT");
(4)从 GOT 集合中删除所有符合查询条件的文档。与(3)不同的是,文档不会在批处理中删除,而是逐个删除:
template.findAllAndRemove(query(where("lastname").is("lannister"), "GOT");
(5)删除 GOT 集合中的前三个文档。与(3)不同的是,文档不会在批处理中删除,而是逐个删除:
template.findAllAndRemove(new Query().limit(3), "GOT");
本示例将演示上面五种删除方法的用法,代码如下:
package com.hxstrive.springdata.mongodb; import com.hxstrive.springdata.mongodb.entity.Person; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Query; import java.util.Arrays; import java.util.List; import static org.springframework.data.mongodb.core.query.Criteria.where; import static org.springframework.data.mongodb.core.query.Query.query; @SpringBootTest public class RemoveDocumentTest { @Autowired private MongoTemplate mongoTemplate; @BeforeEach public void init() { // 删除集合 mongoTemplate.dropCollection(Person.class); // 准备数据 mongoTemplate.insert(Person.builder().id("1").name("Bill").age(26).build()); mongoTemplate.insert(Person.builder().id("2").name("Helen").age(29).build()); mongoTemplate.insert(Person.builder().id("3").name("Tom").age(32).build()); mongoTemplate.insert(Person.builder().id("4").name("Joe").age(38).build()); mongoTemplate.insert(Person.builder().id("5").name("Lucen").age(44).build()); } @Test public void removeOne() { Query query = query(where("id").is("1")); Person person = mongoTemplate.findOne(query, Person.class); if(null != person) { // 删除指定对象 mongoTemplate.remove(person, "person"); } long count = mongoTemplate.count(query, "person"); System.out.println("count = " + count); // count = 0 } @Test public void removeAll() { Query query = query(where("age").gte(30)); long count = mongoTemplate.count(query, "person"); System.out.println("删除前,count=" + count); // 删除前,count=3 // 删除所有匹配的文档 mongoTemplate.remove(query, "person"); count = mongoTemplate.count(query, "person"); System.out.println("删除后,count=" + count); // 删除后,count=0 } @Test public void removeLimit() { Query query = query(where("age").gte(30)); long count = mongoTemplate.count(query, "person"); System.out.println("删除前,count=" + count); // 删除前,count=3 // 删除前两个文档 mongoTemplate.remove(query.limit(2), "person"); count = mongoTemplate.count(query, "person"); System.out.println("删除后,count=" + count); // 删除后,count=1 } @Test public void findAllAndRemoveAll() { Query query = query(where("age").gte(30)); long count = mongoTemplate.count(query, "person"); System.out.println("删除前,count=" + count); // 删除前,count=3 // 删除所有匹配的文档 List<Person> list = mongoTemplate.findAllAndRemove(query, "person"); System.out.println("删除的数据:" + Arrays.toString(list.toArray())); // 删除的数据:[Person(id=3, name=Tom, age=32), Person(id=4, name=Joe, age=38), Person(id=5, name=Lucen, age=44)] count = mongoTemplate.count(query, "person"); System.out.println("删除后,count=" + count); // 删除后,count=0 } @Test public void findAllAndRemoveLimit() { Query query = query(where("age").gte(30)); long count = mongoTemplate.count(query, "person"); System.out.println("删除前,count=" + count); // 删除前,count=3 // 删除前两个文档 List<Person> list = mongoTemplate.findAllAndRemove(query.limit(2), "person"); System.out.println("删除的数据:" + Arrays.toString(list.toArray())); // 删除的数据:[Person(id=3, name=Tom, age=32), Person(id=4, name=Joe, age=38)] count = mongoTemplate.count(query, "person"); System.out.println("删除后,count=" + count); // 删除后,count=1 } }