使用spring mongotemplate进行条件查询

时间:2016-08-03 06:01:21

标签: spring mongodb aggregation-framework spring-mongodb

我想使用条件查询。

这是我的查询

db.projects.aggregate([
{
    "$group": {
        "_id": "$iecode",
        "treatmentArms": { "$first": "$evaluationDTOList" }
    }
},
{ "$unwind": "$treatmentArms" },
{
    "$group": {
        "_id": null,
        "Package": { 
            "$sum": { 
               "$cond": [ 
                   { "$eq": [ "$treatmentArms.mechanismOrPkg", "Package" ] }, 
                   1, 0
                ] 
            }
        },
        "Constraint-relaxing mechanisms": { 
            "$sum": { 
               "$cond": [ 
                    { 
                        "$and": [
                            { "$eq": [ "$treatmentArms.mechanismOrPkg", "Mechanism" ] },
                            { "$eq": [ "$treatmentArms.mechanismTested1", "Constraint-relaxing mechanisms" ] }
                        ]
                    }, 
                    1, 
                    0 ]
            }
        },
        "Delivery mechanisms": { 
            "$sum": { 
               "$cond": [ 
                    { 
                        "$and": [
                            { "$eq": [ "$treatmentArms.mechanismOrPkg", "Mechanism" ] },
                            { "$eq": [ "$treatmentArms.mechanismTested1", "Delivery mechanisms" ] }
                        ]
                    }, 
                    1, 
                    0 ]
            }
        },
        "Other": { 
            "$sum": { 
               "$cond": [ 
                    { 
                        "$and": [
                            { "$eq": [ "$treatmentArms.mechanismOrPkg", "Mechanism" ] },
                            { "$eq": [ "$treatmentArms.mechanismTested1", "Other" ] }
                        ]
                    }, 
                    1, 
                    0 ]
            }
        }
    }
}
])

这是我的 java代码

DBObject groupByIECode = new BasicDBObject("$group",
                new BasicDBObject("_id", new BasicDBObject("iecode","$iecode")).append("treatmentArms",new BasicDBObject("$first","$evaluationDTOList")));
        System.out.println("groupByIECode: "+groupByIECode.toString());

        DBObject unwind = new BasicDBObject("$unwind","$treatmentArms");
        System.out.println("unwind: "+unwind.toString());


        DBObject finalCalculation = new BasicDBObject("$group",new BasicDBObject("_id",null))
                                    .append(
                                            "Package", new BasicDBObject(
                                                "$sum", new BasicDBObject(
                                                    "$cond", new Object[]{
                                                        new BasicDBObject(
                                                            "$eq", new Object[]{ "$treatmentArms.mechanismOrPkg", "Package"}
                                                        ),
                                                        1,
                                                        0
                                                    }
                                                )
                                            )
                                        );

        System.out.println("finalCalculation: "+finalCalculation);
        final AggregationOutput output = projects.aggregate(match,groupByIECode,unwind,finalCalculation);

它给了我MongoException$DuplicateKey

后来我发现$cond不支持spring mongotemplate运算符。那么如何使用spring mongotemplate实现此条件查询。

This链接有一些解释,但它没有显示完整的实现

1 个答案:

答案 0 :(得分:1)

documentation开始,使用Spring Data MongoDB对MongoDB聚合框架的支持的规范示例如下:

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

Aggregation agg = newAggregation(
    pipelineOP1(),
    pipelineOP2(),
    pipelineOPn()
);

AggregationResults<OutputType> results = mongoTemplate.aggregate(agg,
    "INPUT_COLLECTION_NAME", OutputType.class);
List<OutputType> mappedResult = results.getMappedResults();
  

请注意,如果您提供输入类作为第一个参数   newAggregation方法将MongoTemplate派生出来的名称   从这个类输入集合。否则,如果您没有指定   输入类必须提供输入集合的名称   明确。如果提供了输入类和输入集合   后者优先。

对于您的查询,请创建一个实施 AggregationOperation 接口的变通方法,以便在DBObject中使用来表示聚合管道中的单个组操作$cond 运营商:

public class GroupAggregationOperation implements AggregationOperation {
    private DBObject operation;

    public GroupAggregationOperation (DBObject operation) {
        this.operation = operation;
    }

    @Override
    public DBObject toDBObject(AggregationOperationContext context) {
        return context.getMappedObject(operation);
    }
}

然后在聚合管道中将$group操作实现为DBObject,该操作与您拥有的操作相同:

DBObject operation = (DBObject) new BasicDBObject("$group", new BasicDBObject("_id", null))
    .append(
        "Package", new BasicDBObject(
            "$sum", new BasicDBObject(
                "$cond", new Object[]{
                    new BasicDBObject(
                        "$eq", new Object[]{ "$treatmentArms.mechanismOrPkg", "Package"}
                    ),
                    1,
                    0
                }
            )
        )
    );

然后您可以将其用作:

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

GroupAggregationOperation groupOp = new GroupAggregationOperation(operation);
Aggregation agg = newAggregation(
    group("iecode").first("treatmentArms").as("treatmentArms"),
    unwind("treatmentArms"),
    groupOp 
);
AggregationResults<Entity> results = mongoTemplate.aggregate(agg, Entity.class); 
List<Entity> entities = results.getMappedResults();