功能是将昨天的数据 用spCode和deviceCode分组统计,
统计的时候报错No converter found capable of converting from type [org.bson.BsonUndefined] to type [java.lang.String]
java代码
/**
* 获取昨天分组统计后的数据
*/
@Override
public Iterator<AlarmStatis> getPerData(Date beginDate, Date endDate) {
Criteria criteria = Criteria.where("createDate").gte(beginDate).lte(endDate);
//Criteria criteria = new Criteria();
/*
* Aggregation aggregation1 =
* Aggregation.newAggregation(Aggregation.match(criteria),
* Aggregation.group("spCode","deviceCode") .first("spCode").as("spCode")
* .first("deviceCode").as("deviceCode") .count().as("total"));
*/
GroupBy groupBy = new GroupBy("spCode", "deviceCode").
initialDocument("{spCode : '', productCode : '', deviceType : '', deviceCode : '', total : 0}").
reduceFunction("function(doc, prev){"
+ "prev.spCode = doc.spCode;"
+ "prev.productCode = doc.productCode;"
+ "prev.deviceType = doc.deviceType;"
+ "prev.deviceCode = doc.deviceCode;"
+ "prev.deviceGroupCodes = doc.deviceGroupCodes;"
+ "prev.total += 1 ;"
+ "}");
GroupByResults<AlarmStatis> statisResults = mongoTemplate.group(criteria, "alarm_info", groupBy,
AlarmStatis.class);
return statisResults.iterator();
}
异常详情:
[ERROR][2019-08-01 00:17:00,045][pool-11-thread-1][com.star.ac.task.AlarmDataStatisTaskPerDay][AlarmDataStatisTaskPerDay error]
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.bson.BsonUndefined] to type [java.lang.String]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead(MappingMongoConverter.java:887)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1392)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1335)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readProperties(MappingMongoConverter.java:335)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:297)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:253)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:202)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:198)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:86)
at org.springframework.data.mongodb.core.MongoTemplate$ReadDocumentCallback.doWith(MongoTemplate.java:2785)
at org.springframework.data.mongodb.core.MongoTemplate.group(MongoTemplate.java:1798)
at com.star.ac.dao.impl.AlarmMongoDataDaoImpl.getPerData(AlarmMongoDataDaoImpl.java:61)
at com.star.ac.dao.impl.AlarmMongoDataDaoImpl$$FastClassBySpringCGLIB$$2dc19c66.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
上述代码对应的mongoDB语句
db.alarm_info.group({
key:{"spCode":1,"deviceCode":1},
find:{"deviceCode":{$type:"undefined"}},
initial:{"spCode" : "", "productCode" : "", "deviceType" : "", "deviceCode" :"", total : 0},
"$reduce" :function(doc, prev){
prev.spCode = doc.spCode;
prev.productCode = doc.productCode
prev.deviceType = doc.deviceType;
prev.deviceCode = doc.deviceCode;
prev.deviceGroupCodes = doc.deviceGroupCodes;
prev.total += 1;
}
}
)
因为是文档型数据库, 统计的数据里面有的没有deviceCode productCode这几个字段, 因此统计结果会显示undefined
上面代码里定义的结果接收类 AlarmStatis 中的字段都定义为String,所以会出现[org.bson.BsonUndefined] to type [java.lang.String]转化失败