Spring Boot + Webflux + Reactive MongoDB - get document by property Id

怎甘沉沦 提交于 2019-12-25 01:08:41

问题


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;

ProductProperties:

@Document(collection = "product_properties")
public class ProductProperties {
    @Id
    private String id;

    @NotNull
    @NotEmpty
    private String brand;

Service:

Flux<ProductProperties> all = productPropertiesRepository.findAllByBrand(brand);
        List<String> productPropIds = all.toStream()
                .map(ProductProperties::getId)
                .collect(Collectors.toList());
        Flux<Offer> byProperties = offerRepository.findAllByProperties_Id(productPropIds);

But unfortunately byProperties is empty. Why?

My repository:

public interface OfferRepository extends ReactiveMongoRepository<Offer, String> {

    Flux<Offer> findAllByProperties_Id(List<String> productPropertiesIds);
}

How to find all Offers by ProductProperties.brand?

Thanks!


回答1:


After reading some documentation found out that You cannot query with @DBRef. Hence the message

Invalid path reference properties.brand! Associations can only be pointed to directly or via their id property

If you remove DBRef from the field, you should be able to query by findAllByProperties_BrandAndProperties_Capacity.

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()


来源:https://stackoverflow.com/questions/58165835/spring-boot-webflux-reactive-mongodb-get-document-by-property-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!