mahendra kawde
mahendra kawde

Reputation: 847

spring data mongodb find by collection

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"

Upvotes: 2

Views: 1453

Answers (1)

Nikolay Rusev
Nikolay Rusev

Reputation: 4230

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

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

Upvotes: 1

Related Questions