2017-06-09 92 views
0

我在我的mongodb集合中收集了新聞。Spring Boot MongoDB通過列表中的值查找記錄

{ 
     "_id" : ObjectId("593a97cdb17cc6535522d16a"), 
     "title" : "Title", 
     "text" : "Test", 
     "data" : "9.06.2017, 14:39:33", 
     "author" : "Admin", 
     "categoryList" : [ 
      { 
       "_id" : null, 
       "text" : "category1" 
      }, 
      { 
       "_id" : null, 
       "text" : "category2" 
      }, 
      { 
       "_id" : null, 
       "text" : "category3" 
      } 
     ] 
    } 

每個新聞記錄都有類別列表。我很想找到所有有category1的新聞categoryList我試着做到這一點 newsRepository.findByCategoryList("category1");但不工作。

如何做到這一點?

回答

1

根據您目前的資料庫方法生成的查詢是

{ "categoryList" : "category1"} 

你需要的是

{ "categoryList.text" : "category1"} 

您可以通過兩種方式創建查詢。

使用庫

findByCategoryListText(String category) 

使用查詢方法

@Query("{'categoryList.text': ?0}") 
findByCategoryList(String category)