【问题标题】:How do a IN Query with parameter in mongo template如何在mongo模板中使用参数进行IN查询
【发布时间】:2021-12-07 16:59:31
【问题描述】:

我是春天的新手,我会在 mongo 模板查询中引用这个查询。 有人可以帮我吗?

public List<Prodotto>selectedList(List<Integer>categories){
        
                TypedQuery<Product> findSelectedQuery1 = em
                        .createQuery( "SELECT DISTINCT p FROM Product p WHERE p.category.idCategory IN :categories ",
                                Product.class);
                findSelectedQuery1.setParameter("categories", categories);
                
                return findSelectedQuery1.getResultList();
            }


【问题讨论】:

    标签: spring spring-data-mongodb mongotemplate


    【解决方案1】:

    我假设你的“类别”是一个对象

    db.Product.aggregate([
      {
        $match: {
          "category.idCategory": {
            $in: categories
          }
        }
      }
    ])
    

    MongoPlayground

    弹簧数据:

    @Autowired
    private MongoTemplate mongoTemplate;
    
    ...
    
    List<AggregationOperation> pipeline = new ArrayList<>();
    
    //$match
    pipeline.add(Aggregation.match(Criteria.where("category.idCategory").in(categories)));
    
    Aggregation agg = Aggregation.newAggregation(pipeline);
    return mongoTemplate.aggregate(agg, Product.class, Product.class).getMappedResults();
    

    【讨论】:

      猜你喜欢
      • 2011-09-17
      • 2021-08-14
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 2011-08-23
      • 2013-05-13
      相关资源
      最近更新 更多