Spring -data-mongodb optional request parameter

I am using spring -data-mongodb.

I want to query the database by passing an optional parameter in my query.

I have a domain class.

public class Doc { @Id private String id; private String type; private String name; private int index; private String data; private String description; private String key; private String username; // getter & setter } 

My controller:

 @RequestMapping(value = "/getByCategory", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON) public Iterable<Doc> getByCategory( @RequestParam(value = "key", required = false) String key, @RequestParam(value = "username", required = false) String username, @RequestParam(value = "page", required = false, defaultValue = "0") int page, @RequestParam(value = "size", required = false, defaultValue = "0") int size, @RequestParam(value = "categories") List<String> categories) throws EntityNotFoundException { Iterable<Doc> nodes = docService.getByCategory(key, username , categories, page, size); return nodes; } 

Here Key and username are optional request parameters.

If I transfer any of them, it should return the corresponding document with the given key or username.

My service method:

 public Iterable<Doc> getByCategory(String key, String username, List<String> categories, int page, int size) { return repository.findByCategories(key, username, categories, new PageRequest(page, size)); } 

Repository:

 @Query("{ $or : [ {'key':?0},{'username':?1},{categories:{$in: ?2}}] }") List<Doc> findByCategories(String key, String username,List<String> categories, Pageable pageable); 

But, using the query above, it does not return the document with the specified key or username. What is wrong with my request?

This is how I make the request http: // localhost: 8080 / document / getByCategory? Key = key_one & username = ppotdar & categories = category1 & categories = category2

+5
source share
1 answer

Personally, I delete the repository template driven by the interface, at this point I create a DAO, which is an @Autowire object, and then query the database using Criteria . So you have clear code that does not stretch the possibilities of @Query annotation.

So something like this (untested, pseudocode):

 @Repository public class DocDAOImpl implements DocDAO { @Autowired private MongoTemplate mongoTemplate; public Page<Doc> findByCategories(UserRequest request, Pageable pageable){ //Go through user request and make a criteria here Criteria c = Criteria.where("foo").is(bar).and("x").is(y); Query q = new Query(c); Long count = mongoTemplate.count(q); // Following can be refactored into another method, given the Query and the Pageable. q.with(sort); //Build the sort from the pageable. q.limit(limit); //Build this from the pageable too List<Doc> results = mongoTemplate.find(q, Doc.class); return makePage(results, pageable, count); } ... } 

I know that this flies in the face of a tendency to generate database code over time, but, in my opinion, this is still the best approach to more complex operations with the database, because it loads easier to see what is actually happening.

0
source

All Articles