How to delete items in MongoRepository using query annotation?

后端 未结 6 1196
北海茫月
北海茫月 2021-02-05 06:03

I\'m using Spring Data with MongoDB using MongoRepository.

I was wondering if it is possible do a delete by filter using query annotation. I have been looking here and g

6条回答
  •  庸人自扰
    2021-02-05 06:46

    Repository:

    @Component
    public interface SomeRepository extends MongoRepository {
    
        @Query("{ '_id' : ?0 }")
        SomeObject findById(String _id);
    }
    

    Code in some class:

    @Autowired
    private SomeRepository pRepo;
    
    public void delete(String id) {
    
        pRepo.delete(pRepo.findById(id));
    }
    

提交回复
热议问题