问题
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