【问题标题】:MongoReactiveRepository - find by list of values (IN operator)MongoReactiveRepository - 按值列表查找(IN 运算符)
【发布时间】:2019-11-11 19:04:56
【问题描述】:

我尝试在 Offer.ProductProperties.id IN (String1, String2...) 中查找所有 Offer

public interface OfferRepository extends ReactiveMongoRepository<Offer, String> {

    @Query("{'properties.id': {'$in': ?0}}")
    Flux<Offer> findByPropertiesIds(List<String> propertiesId);

不幸的是,我得到了 null... 为什么?我不知道...

但这有效:

@Query("{'properties.id': ?0 }")
    Flux<Offer> findByPropertiesId(String propertiesId);

报价文件:

    @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;
    (...)

}

我也尝试过使用 ReactiveMongoTemplate,但没有成功:

List<String> propIds = products.stream()
                .map(product -> product.getProperties().getId())
                .collect(Collectors.toList());

        Query query = new Query();
        query.addCriteria(Criteria.where("properties.id").in(propIds));
        return mongoTemplate.find(query, Offer.class);

【问题讨论】:

    标签: mongodb spring-webflux


    【解决方案1】:

    好的,我找到了解决办法:

    @Query("{'properties.$id': {'$in': ?0}}")
        Flux<Offer> findByPropertiesIds(List<ObjectId> propertiesId);
    

    我必须将 $ 添加到 id 并将 String 更改为 ObjectId 作为相关文档的参考 id。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      相关资源
      最近更新 更多