How to get items on a specific parameter?

Viewed 51

I recently work with MongoDB and with spring-data and I have this question. I want to get all the items in which categoryId = cat120026. For this purpose I wrote this method:

Interface:

public interface ProductService {

    @Query("{ 'categoryId' : '\"cat120026\"' }")
    ProductModelResponse getProductByCategory();
}

Interface impl:

 @Autowired
    @Qualifier("productRepository")
    private MongoRepository<ProductModel, String> repository;

    @Override
    public ProductModelResponse getProductByCategory() {
        return new ProductModelResponse(repository.findAll());
    }

But my query is not picked up, I get a complete list of items. I saw mongoTemplate examples, but do not quite understand what kind of class is specified in the parameters. My ProductResponseModel is my JSON response. How it can be improved?

Answer:

@Override
    public ProductModelResponse getProductByCategory() {
        Query query = new Query(Criteria.where("categoryId").is("\"cat120026\""));
        return new ProductModelResponse(pp.find(query, ProductModel.class));
    }
0 Answers
Related