Spring boot 弹簧靴&x2B;Webflux&x2B;被动MongoDB-按属性Id获取文档
我想按Offer.ProductProperties.brand查找所有报价文档:Spring boot 弹簧靴&x2B;Webflux&x2B;被动MongoDB-按属性Id获取文档,spring-boot,reactive-programming,spring-webflux,Spring Boot,Reactive Programming,Spring Webflux,我想按Offer.ProductProperties.brand查找所有报价文档: @Document(collection = "offers") public class Offer { @Id private String id; @NotNull @DBRef private ProductProperties properties; 产品属性: @Document(collection = "product_properties") pub
@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);
Flux all=productPropertiesRepository.findAllByBrand(品牌);
List PRODUCTPROPID=all.toStream()
.map(ProductProperties::getId)
.collect(Collectors.toList());
Flux byProperties=OfferPrepository.findAllByProperties\u Id(productPropIds);
但不幸的是,byProperties是空的。为什么?
我的存储库:
public interface OfferRepository extends ReactiveMongoRepository<Offer, String> {
Flux<Offer> findAllByProperties_Id(List<String> productPropertiesIds);
}
公共接口offerposition扩展了reactiveMongoreposition{
Flux findAllByProperties\u Id(列出ProductProperties Id);
}
如何按ProductProperties.brand查找所有优惠
谢谢 在阅读一些内容后,您发现无法使用@DBRef
进行查询。这就是信息
路径引用属性无效。品牌!关联只能是
直接指向或通过其id属性指向
如果从字段中删除DBRef,则应该能够通过findAllByProperties\u BrandAndProperties\u Capacity
进行查询
所以,唯一的办法就是你如何做。i、 e.获取id并按id查询。正如我在评论中所说的,它不起作用的原因是
findallbyproperty\u Id
的返回类型是一个变量。因此,除非执行终端操作,否则不会有任何结果。试一试
byProperties.collectList().block()
你试过调试吗?
productPropId
变量是空的还是有值?其次,findAllByProperties的retutn类型是一种流量。因此,除非执行终端操作,否则不会有任何结果。请尝试flux.collectList().block()
productPropIds工作正常,不为空,正确的值为receivedIt应为findAllByProperties\u IdIn
I怀疑。但为什么这么复杂?为什么不干脆findAllByProperties\u Brand
这样你就有一个查询了?@Denium是对的。您可以只使用findAllByProperties\u BrandAndProperties\u Capacity
如果您使用的是品牌和容量这是不可能的,我会得到错误:无效的路径引用properties.brand!只能直接或通过其id属性指向协会。我仍然无法按品牌和容量获得我的报价。我试图通过properties.id获得我的报价,如下:@Query(“{'properties.id':?0}”)Flux findAllByProperties_id(String propertiesId);但是没有成功你尝试了'collectList().block()'是的,我使用了fluxResult.toStream().collect(Collectors.toList());但结果仍然是0。它必须与目标有关。我的JSON看起来像:{“_id”:ObjectId(“5d7a0a9c4313ec506f473595”),“companyId:“1234”,“properties:{“$ref:”product_properties“,“$id”:ObjectId(“5d5fc17b96764a5bfe152daf”)。我尝试了许多选项,包括@Query({properties:{id:?0}”)fluFindByproperties\u id(String propertiesId);从shell开始工作非常出色:db.offers.find({“properties.$id”:ObjectId(“5d5fc17b96764a5bfe152daf”)),但是我在Spring Boot中的查询仍然存在问题您的实体类中有注释@id吗?