问题描述
我正在使用 spring boot vpn free 1.5.1 和 MongoDB 3.4.6 版.
我有一个 mongo 文档 Hotel ,其中包含 Review 列表.
I have a mongo document Hotel which has a list of Review .
Review 类具有属性userName.
@Document
public class Hotel {
@Id
private String id;
private List<Review> reviews;
我想通过 免费vpn Review userName 搜索所有酒店.
I want to search all the hotel by Review userName.
我的 vpn下载 HotelRepository 有 public List
当我与用户Salman"通话时 -
When I am calling with user 'Salman' -
List<Hotel> list = this.hotelRepository.findByReviewsUserName(user);
此方法返回如下结果:
[
免费vpn {
"id": "59b23c39c70ff63135f76b14",
"name": "Signature",
"reviews": [
free vpn vpn下载 {
免费vpn 免费vpn下载 "id": 1,
"userName": "Salman",
"rating": vpn下载 8,
免费vpn free 免费vpn下载 vpn free vpn "approved": true
},
免费vpn 免费vpn {
vpn free 免费vpn下载 vpn下载 免费vpn "id": 2,
免费vpn 免费vpn "userName": "Shahrukh",
"rating": 5,
"approved": false
免费vpn下载 }
vpn free ]
}
]
我只想要Salman"的评论,但它也为其他人返回.
What I want the reviews only of 'Salman' but it's also returning for others also.
我缺少什么或怎么做?
我注意到,如果单个评论用户匹配它会返回我不想要的评论的整个列表,我想要按名称搜索的评论.
推荐答案
命名查询按原样工作.您并没有明确说您只想要文档的一部分,因此查询返回整个文档.要实现这一点您不能使用命名查询 (请参阅@alexefimov 答案,了解在 @Query 注释的帮助下使用命名查询),但您可以使用 <MongoRepository 旁边的代码>MongoTemplate.为此,您必须进行一些更改:
The named query works as it should 免费vpn下载 be. You are not explicitly saying that you want 免费vpn下载 only a portion of document so query returns whole document. To achieve that you cannot use named queries (see @alexefimov answer for using named queries with help of @Query annotation) but you can use MongoTemplatebeside of MongoRepository. to do that you have to 免费vpn make some changes:
首先你的仓库应该是这样的:
First your repository should be like this:
public interface HotelRepository extends MongoRepository<Hotel, String>, MongoTemplateRepository {
// vpn free You can continue to write your named queries here. Spring will create that.
}
MongoTemplateRepository:
MongoTemplateRepository:
public interface MongoTemplateRepository {
// You 免费vpn will write your queries which will use mongoTemplate here.
List<Hotel> findByReviewsUserName(String userName);
}
为了实现 MongoTemplateRepository 方法,您将编写一个新类.重要的是你应该将这个类命名为你的存储库类名+Impl.否则 spring-data 无法找到 MongoTemplateRepository 中定义的方法实现的位置.所以你的实现类的名字应该是 HotelRepositoryImpl
For implementation of MongoTemplateRepository methods, you will write a new class. The important thing here is that you should named this class your repository class name + Impl. Otherwise spring-data cannot find where your methods 免费vpn implementation those defined in 免费vpn vpn下载 MongoTemplateRepository. So your implementation class's name should be HotelRepositoryImpl
public class HotelRepositoryImpl implements MongoTemplateRepository {
免费vpn @Autowired
private MongoTemplate mongoTemplate; // we will use this to query mongoDb
@Override
public List<Hotel> findByReviewsUserName(String userName) {
vpn free Query query = new Query();
query.addCriteria(Criteria.where("reviews.username").is(userName));
免费vpn vpn下载 query.fields().include("reviews.$");
return mongoTemplate.find(query, Hotel.class);
vpn free }
}
用法:
hotelRepository.findByReviewsUserName("userName");
正如您在代码中看到的,我们可以为查询添加 .include() 或 .exclude 字段.当您想包含数组字段的匹配部分时,我们使用 $ 运算符和数组字段名称.
As you can vpn下载 see in codes free vpn we can .include() or .excludefields for the query. While you want to include just matched part of an array field, we use vpn下载 $operator with array field vpn下载 name.
结论:您仍然可以使用 spring-data free vpn下载 免费vpn vpn 支持良好的命名查询,此外,如果您需要 聚合 或对无法构建命名查询的子文档进行一些复杂查询到春天,您可以在新创建的 mongoTemplate 存储库类中执行此操作.您可以从 HotelRepository 访问所有存储库方法.
Conclusion: vpn下载 You can still use free vpn spring-data well supported named queries and vpn下载 additionally if you need aggregation or some complex queries for sub documents that a named query cannot be built by spring, you can do it in your newly created vpn free mongoTemplate 免费vpn repository class. And you can access vpn free all of your repository methods from HotelRepository.
这篇关于如何使用 free vpn Spring Data 从 Mongo 中的文档数组字段中仅获取匹配的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司免费vpn源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构免费vpn源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)