如何在 mongo 模板中使用参数进行 IN 查询

How do a IN Query with parameter in mongo template

我是 spring 的新手 我会在 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();
            }


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

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

MongoPlayground

Spring-数据:

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