【问题标题】:Create criteria object using map in mongo在 mongo 中使用地图创建标准对象
【发布时间】:2025-11-25 07:00:01
【问题描述】:

我有一个映射 Map filterParams,它的键值对将成为 Criteria 的一部分。如何创建 Criteria 对象。

之前我使用的是 Query.addCriteria。但是现在我想要一个条件对象,因为我需要将它传递给 mongo 中的 Aggregation.match()。

    filterParams.entrySet().forEach(e -> query.addCriteria(criteria.where(e.getKey()).is(e.getValue())));


        Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria ),Aggregation.group("property_type.name").count().as("count"),
                Aggregation.project("property_type").andExclude("_id"));

【问题讨论】:

    标签: spring mongodb spring-boot criteria


    【解决方案1】:

    试试这个:

    Criteria criteria = new Criteria();
    filterParams.entrySet().forEach(e -> criteria.and(e.getKey()).is(e.getValue()));
    
    Aggregation aggregation = Aggregation.newAggregation(
        Aggregation.match(criteria), 
        Aggregation.group("property_type.name").count().as("count"),
        Aggregation.project("property_type").andExclude("_id")
    );
    

    【讨论】:

      最近更新 更多