[英]Use $or operator in @Query annotation in spring data mongodb repository
我正在使用spring-data-mongodb。
我想在我的存儲庫中使用$或operator。
這是我的查詢:
@Query("{'type':?0}")
List<Doc> findByType(String type, Pageable pageable);
如何使用$或@Query,因為它可以匹配任何類型或名稱,並獲取文檔。 請幫忙。
我找到了我的問題的答案,它也處理了可選的查詢參數。
查詢應該是:
@Query("{'type':?0,'$or':[{ $where: '?1 == null' },{'key':?1},{ $where: '?2 == null' },{'username':?2}]}")
List<Doc> findByType(String type, String key, String username, Pageable pageable);
您甚至不需要手動定義的查詢:
Page<Doc> findByTypeOrName(String type, String name, Pageable pageable);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.