$and/$or/$nor必须是非空数组mongotemplate java micronaut 2.2.1

ycggw6v2  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(762)

我有一个问题

  1. List<Bson> query = QueryBuilder.QueryBuilder(ProductSearchCriteria, Bson.class);
  2. var item = Flowable.fromPublisher(
  3. repository.getCollection(ProductConstrants.PRODUCT_COLLECTION_NAME, Product.class)
  4. .find(Filters.and(query))).blockingIterable();
  5. item.forEach(value -> {
  6. productLists.add(
  7. new ProductViewModel(
  8. value.getId().toString(),
  9. value.getName(),
  10. value.getDescription(),
  11. value.getPrice()
  12. ));
  13. });

现在 List<Bson> query 有时计数大于0,有时==0。当计数大于0时,工作正常,但是当计数为0时,会出现异常 (BadValue): '$and/$or/$nor must be a nonempty array' 这是有道理的。
我的意思是当count>0时,如何使查询动态化

  1. Flowable.fromPublisher(
  2. repository.getCollection(ProductConstrants.PRODUCT_COLLECTION_NAME, Product.class)
  3. .find(Filters.and(query))).blockingIterable();

当计数==0时

  1. Flowable.fromPublisher(
  2. repository.getCollection(ProductConstrants.PRODUCT_COLLECTION_NAME, Product.class)
  3. .find()).blockingIterable();

我不想写if&else语句,有没有更好的方法?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题