如何使用查询注释删除MongoRepository中的项目?

时间:2013-07-05 08:13:21

标签: java spring mongodb spring-data

我正在使用MongoDB将Spring Data与MongoDB一起使用。

我想知道是否可以使用查询注释通过过滤器进行删除。我一直在这里看谷歌,我找不到任何文件。

6 个答案:

答案 0 :(得分:14)

也许您可以使用存储库删除查询。以下是documentation的示例:

public interface PersonRepository extends MongoRepository<Person, String> {
  List <Person> deleteByLastname(String lastname);

  Long deletePersonByLastname(String lastname);         
}

使用返回类型List将在实际删除之前检索并返回所有匹配的文档。数字返回类型直接删除匹配的文档,返回删除的文档总数。

答案 1 :(得分:13)

@Query(value="{id : $0}", delete = true)
public Person deleteBy

答案 2 :(得分:3)

不幸的是,spring数据没有提供任何基于查询删除文档的方法。 @Query注释仅适用于查找文档。

您可以执行的是custom repository,根据您的需要删除文档。

答案 3 :(得分:2)

如何在查询中删除ID列表?

@Query(value="{idList : $0}", delete = true)

答案 4 :(得分:1)

存储库:

@Component
public interface SomeRepository extends MongoRepository<SomeObject, String> {

    @Query("{ '_id' : ?0 }")
    SomeObject findById(String _id);
}

某些课程中的代码:

@Autowired
private SomeRepository pRepo;

public void delete(String id) {

    pRepo.delete(pRepo.findById(id));
}

答案 5 :(得分:0)

尝试一下,它对我有用。

let employeeFormGroups = team.employees.map(employee => this.formBuilder.group(employee));
let employeeFormArray = this.formBuilder.array(employeeFormGroups);
this.teamForm.setControl('employees', employeeFormArray);