2017-06-09 94 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)