2017-06-22 48 views
-1

我有一個像MongoDB的比賽或像查詢

{ 
     "id": "5948b044ef91edb15d222794", 
     "articleId": "4", 
     "author": "asdfghjkl", 
     "publicationDate": 1454371200000, 
     "rating": 4, 
     "numViews": 600000, 
     "summary": "In the end, bombastic bozo Donald Trump’s predictions of an Iowa victory were all a big, sad joke.", 
     "articleUrl": "http://www.asdfghjkl.com/news/politics/donald-trump-predicts-iowa-victory-voters-caucusing-article-1.2516776", 
     "keywords": [ 
      "Trump", 
      "Election" 
     ], 
     "imgPath": [ 
      "http://assets.asdfghjkl.com/polopoly_fs/1.2516971.1454384425!/img/httpImage/image.jpg_gen/derivatives/article_1200/usa-election-trump.jpg" 
     ], 
     "articleName": "Ted Cruz, after trailing in polls, beats Donald Trump in Iowa caucus; Marco Rubio finishes a close third" 
    } 

我有一個關鍵字列表的MongoDB中的文檔列表(如字符串[「王牌」,「唐納德」,「選舉」]),我想與這些文件中的keywordsarticleName列匹配。

那麼,我怎樣才能得到匹配的文件集合使用$in or $or or regex在java spring-boot?

+0

利用彈簧的數據!如果你想讓人們幫助你,請爲此表現出一些努力。 –

+0

將你的'@ Repository'放在問題中,並且用這些信息實現的接口我們可以幫助你 – MaQuiNa1995

回答

1
@Autowired 
private MongoTemplate mongoTemplate; 

Query query = new Query(); 
List<String> list = Arrays.asList("Trump", "Election"); 
query.addCriteria(Criteria.where("keywords").in(list)); 
List<YourModel> yourModel = mongoTemplate.find(query, YourModel.class); 
0

我已經這樣做,通過使用正則表達式$以下實現:

BasicQuery query = new BasicQuery("{ keywords: { $regex: '(?:[\\s]|^)("+keywords+")(?=[\\s]|$)',$options: 'i' } } "); 
    articlesList = mongoTemplate.find(query, Articles.class); 

其中的關鍵字是這樣的:關鍵字1 |關鍵字2 | KEYWORD3