spring data mongodb find by collection

1.4k views Asked by At

I am using spring-data-mongodb.

Here is my controller method:

Controller:

@RequestMapping(value = "/getByCategory",method = RequestMethod.GET, consumes=MediaType.APPLICATION_JSON, produces=MediaType.APPLICATION_JSON)    
    public Iterable<Node> getByCategory(@RequestParam(value="categories") List<String> categories) throws EntityNotFoundException {
        Iterable<Node> nodes = nodeService.getByCategory(categories);
        return nodes;
    }

Here I am passing list of string as a request parameter.

My service method is:

public Iterable<Node> getByCategory(List<String> categories) {

        return repository.findByCategories(categories);
    }

Repository code:

@RepositoryRestResource
public interface NodeRepository extends MongoRepository<Node, String> , PagingAndSortingRepository<Node, String>{


    @Query("{categories:{$in: ?0}}")
    Iterable<Node> findByCategories(List<String> categories);

}

Here, my query in repository always returns empty array []. I am not getting what is wrong with this query.

My request URL is:

http://localhost:8080/document/getByCategory?categories="category1"&categories="category2"
1

There are 1 answers

1
Nikolay Rusev On BEST ANSWER

i think the problem is in your url. try to remove the quotes.

http://localhost:8080/document/getByCategory?categories=category1&categories=category2