2015-06-15 17 views
5

Tôi đang sử dụng spring-data-mongodb.spring-data-mongodb tham số truy vấn tùy chọn

Tôi muốn truy vấn cơ sở dữ liệu bằng cách chuyển một số tham số tùy chọn trong truy vấn của mình.

Tôi có một lớp miền.

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 
} 

điều khiển của tôi:

@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; 
    } 

Đây chínhTên truy nhập là tham số truy vấn tùy chọn.

Nếu tôi vượt qua bất kỳ một trong số họ, nó phải trả lại tài liệu phù hợp với khóa hoặc tên người dùng đã cho.

phương pháp dịch vụ của tôi là:

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); 

Nhưng bằng cách sử dụng trên truy vấn nó không trả về một tài liệu với một trong hai chìa khóa nhất định hoặc tên người dùng. Có gì sai trong truy vấn của tôi?

Đây là cách tôi đang làm theo yêu cầu http://localhost:8080/document/getByCategory?key=key_one&username=ppotdar&categories=category1&categories=category2

+0

Bạn có thể bắt đầu đặt bạn MongoDB Profiler để ghi nhật ký tất cả các thao tác (và do đó truy vấn) bằng lệnh 'db.setProfilingLevel (2) 'và sau đó xem chính xác truy vấn bạn đang thực hiện. Hãy nhớ đặt thành 0 sau khi bạn hoàn tất. – araknoid

Trả lời

0

Cá nhân, tôi muốn bỏ cái dự án mô hình kho giao diện điều khiển tại thời điểm đó, tạo ra một DAO rằng @Autowire sa đối tượng MongoTemplate, và sau đó truy vấn DB sử dụng thay vào đó, hãy nhập số Criteria. theo cách đó, bạn có mã rõ ràng không kéo dài khả năng chú thích @Query.

Vì vậy, một cái gì đó như thế này (chưa được kiểm tra, pseudo-code):

@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); 
    } 

    ... 
} 

Tôi biết điều này trái ngược với các xu hướng thế hệ thời gian chạy của mã DB, nhưng tâm trí của tôi, nó vẫn là tốt nhất phương pháp tiếp cận cho các hoạt động DB khó khăn hơn, bởi vì nó tải dễ dàng hơn để xem những gì đang thực sự xảy ra.

Các vấn đề liên quan