Spring Boot + Webflux + Reactive MongoDB - 通过属性 Id 获取文档 [英] Spring Boot + Webflux + Reactive MongoDB - get document by property Id
问题描述
我想按 Offer.ProductProperties.brand 查找所有优惠文件:
I'd like to find all Offer documents by Offer.ProductProperties.brand:
@Document(collection = "offers")
public class Offer {
@Id
private String id;
@NotNull
@DBRef
private ProductProperties properties;
产品属性:
@Document(collection = "product_properties")
public class ProductProperties {
@Id
private String id;
@NotNull
@NotEmpty
private String brand;
服务:
Flux<ProductProperties> all = productPropertiesRepository.findAllByBrand(brand);
List<String> productPropIds = all.toStream()
.map(ProductProperties::getId)
.collect(Collectors.toList());
Flux<Offer> byProperties = offerRepository.findAllByProperties_Id(productPropIds);
但不幸的是 byProperties 是空的.为什么?
But unfortunately byProperties is empty. Why?
我的仓库:
public interface OfferRepository extends ReactiveMongoRepository<Offer, String> {
Flux<Offer> findAllByProperties_Id(List<String> productPropertiesIds);
}
如何按 ProductProperties.brand 查找所有优惠?
How to find all Offers by ProductProperties.brand?
谢谢!
推荐答案
阅读一些documentation 发现您无法使用 @DBRef
进行查询.因此消息
After reading some documentation found out that You cannot query with @DBRef
. Hence the message
无效的路径引用属性.品牌!关联只能是直接或通过他们的 id 属性指向
Invalid path reference properties.brand! Associations can only be pointed to directly or via their id property
如果您从字段中删除 DBRef,您应该可以通过 findAllByProperties_BrandAndProperties_Capacity
进行查询.
If you remove DBRef from the field, you should be able to query by findAllByProperties_BrandAndProperties_Capacity
.
所以唯一的方法就是你的表现.即获取 id 并按 id 查询.
正如我在评论中所说,它不起作用的原因是因为 findAllByProperties_Id
的返回类型是 Flux.所以除非你执行终端操作,否则你不会有任何结果.试试
So the only ways is how you are doing. i.e. Fetch id's and query by id.
As I said in the comment, the reason it is not working is because return type of findAllByProperties_Id
is a Flux. So unless u execute a terminal operation, you wont have any result. Try
byProperties.collectList().block()
这篇关于Spring Boot + Webflux + Reactive MongoDB - 通过属性 Id 获取文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!