“无法从BSON类型字符串转换为日期”MongoDB for Java 3.5

时间:2017-12-11 12:43:11

标签: java mongodb mongodb-query aggregation-framework

这里是Bson中的查询:

var match = {


$match : {

    "dt_extract": {
    "$gte": new ISODate("2016-12-05T13:29:28.737+01:00"),
    "$lte": new ISODate("2017-12-11T13:29:29.929+01:00")
  }

}


}


var group = {

$group :{
        "_id":{
                weekNum : {"$isoWeek" :"$dt_extract"} ,
                year :{"$isoWeekYear": "$dt_extract"},
                dt_extract_date: "$dt_extract"
        },

        "Sum_Reliability_Rate_Numerator":{
        "$sum":"$mlf16_txfiabnum"   
        }

}
}

var project = {


$project: { 

    "Sum_Reliability_Rate_Numerator":1
}


}

var sort = {
    $sort :{_id : -1}
}


 db.mplf_tmp_v3.aggregate([match,group,project,sort])

这个请求工作,我没有任何问题但是当我想在Mongo for Java 3.5中编写它时,我遇到了将Bson类型String转换为Date的问题。

private List<Bson> filtersQueryCard(QueryParamWarningDto queryParamWarningDto) {

        List<Bson> filters = new ArrayList<>();

        LocalDate localDate = LocalDate.now();

        Date date = new Date() ;

        DateTimeFormatter sdf1 = DateTimeFormat.forPattern("yyyy-MM-dd");


        localDate = LocalDate.parse(localDate.toString(), sdf1);

        LocalDate localDateMin = localDate.minusWeeks(53);


        LocalDate localDateMax = localDate;


        DateTime dateTimeMin = localDateMin.toDateTimeAtCurrentTime();
        DateTime dateTimeMax = localDateMax.toDateTimeAtCurrentTime();



        if (queryParamWarningDto.getIdRefNum() != null) {

            filters.add(eq(SupplierIndicatorFieldEnum.MLF16_CODRAFT.getLabel(), queryParamWarningDto.getIdRefNum()));
            filters.add(gte(SupplierIndicatorFieldEnum.DT_EXTRACT.getLabel(), "new ISODate('"+dateTimeMin.toString()+"')"));
            filters.add(lte(SupplierIndicatorFieldEnum.DT_EXTRACT.getLabel(),"new ISODate('"+dateTimeMax.toString()+"')"));

        } else {
            filters.add(eq(SupplierIndicatorFieldEnum.MLF16_CPTFOU_SIX.getLabel(), queryParamWarningDto.getIdSupNum()));
            filters.add(eq(SupplierIndicatorFieldEnum.MLF16_CODADRES.getLabel(), queryParamWarningDto.getCodeSuppNum()));

            filters.add(gte(SupplierIndicatorFieldEnum.DT_EXTRACT.getLabel(),"new ISODate('"+dateTimeMin.toString()+"')"));
            filters.add(lte(SupplierIndicatorFieldEnum.DT_EXTRACT.getLabel(),"new ISODate('"+dateTimeMax.toString()+"')"));
        }
        return filters;
    }


private List<SupplierIndicatorDTOM> getAllIndicatorForSupp(QueryParamWarningDto queryParamWarningDto) {

        List<SupplierIndicatorDTOM> results = new ArrayList<>();

        List<Bson> filters = filtersQueryCard(queryParamWarningDto);

        MongoDatabase database = mongoUtil.mongoClient();

        MongoCollection<Document> collection = database.getCollection("mplf_tmp_v3");


        try {

            AggregateIterable<Document> resultMongo = collection.aggregate(Arrays.asList(
                    Aggregates.match(and(filters)),
                    Aggregates.group(and(eq("weekNum", eq("$isoWeek", "$dt_extract"))
                            , eq("year", eq("$isoWeekYear","$dt_extract")),
                            eq("dt_extract_date", "$dt_extract"),
                            eq(SupplierIndicatorFieldEnum.MLF16_TXFIABNUM.getLabel(), SupplierIndicatorFieldEnum.MLF16_TXFIABNUM.getLabel()),
                            eq(SupplierIndicatorFieldEnum.MLF16_TXFIABDEN.getLabel(), SupplierIndicatorFieldEnum.MLF16_TXFIABDEN.getLabel())
                            ), Accumulators.sum("Sum_Reliability_Rate_Numerator", SupplierIndicatorFieldEnum.MLF16_TXFIABNUM.getLabel())
                            , Accumulators.sum("Sum_Reliability_Rate_Denominator", SupplierIndicatorFieldEnum.MLF16_TXFIABDEN.getLabel())
                            , Accumulators.sum("Sum_Doc_Rate_Numerator", SupplierIndicatorFieldEnum.MLF16_TXDOCNUM.getLabel())
                            , Accumulators.sum("Sum_Doc_Rate_Denominator", SupplierIndicatorFieldEnum.MLF16_TXDOCDEN.getLabel())
                            , Accumulators.sum("Sum_Service_Rate_Numerator", SupplierIndicatorFieldEnum.MLF16_TXSERVNUM.getLabel())
                            , Accumulators.sum("Sum_Service_Rate_Denominator", SupplierIndicatorFieldEnum.MLF16_TXSERVDEN.getLabel())),
                    Aggregates.project(include("Sum_Reliability_Rate_Numerator", "Sum_Reliability_Rate_Denominator", "Sum_Doc_Rate_Numerator"
                            , "Sum_Doc_Rate_Denominator", "Sum_Service_Rate_Numerator", "Sum_Service_Rate_Denominator"))));

1 个答案:

答案 0 :(得分:2)

总结发布评论中的问题。

ISODate是一种shell类型。将条件值传递给过滤器

时,您需要使用java.util.Date类型

使用DateTime.toDate()

将jodatime转换为Date

filters.add(gte(SupplierIndicatorFieldEnum.DT_EXTRACT.getLab‌​‌​‌​el(),dateTimeMin‌​.toDate()))‌​; filters.add(lte(SupplierIndicatorFieldEnum.DT_EXTRACT.getLab‌​‌​‌​el(),dateTimeMax‌​.toDate()))‌​;

在常量中包含$以将字符串计算为mongodb中的变量。

and(eq("weekNum", eq("$isoWeek", "$" + SupplierIndicatorFieldEnum.DT_EXTRACT.getLabel())) , eq("year", eq("$isoWeekYear","$" + SupplierIndicatorFieldEnum.DT_EXTRACT.getL‌​abel())