Spring throws ConverterNotFound for range search on date in MongoDB

馋奶兔 提交于 2019-12-13 07:45:31

问题


I'm trying to do a range search on DateTime data format. The dates are being stored in Joda's DateTime format in the MongoDB.

When I do a FIND query for a data greater than a given date, the ConverterNotFoundException is being thrown.

Could someone please share information on why the conversation of Date object is being done to DateTime (Joda) &how can I possibly resolve this?

Exception

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type java.util.Date to type org.joda.time.DateTime
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:313) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:176) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1179) ~[spring-data-mongodb-1.8.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.access$200(MappingMongoConverter.java:78) ~[spring-data-mongodb-1.8.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1133) ~[spring-data-mongodb-1.8.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:869) ~[spring-data-mongodb-1.8.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:282) ~[spring-data-mongodb-1.8.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:270) ~[spring-data-mongodb-1.8.1.RELEASE.jar:na]
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:309) ~[spring-data-commons-1.11.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:270) ~[spring-data-mongodb-1.8.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:231) ~[spring-data-mongodb-1.8.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:191) ~[spring-data-mongodb-1.8.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:187) ~[spring-data-mongodb-1.8.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:78) ~[spring-data-mongodb-1.8.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doWith(MongoTemplate.java:2215) ~[spring-data-mongodb-1.8.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1891) ~[spring-data-mongodb-1.8.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1714) ~[spring-data-mongodb-1.8.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1697) ~[spring-data-mongodb-1.8.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:602) ~[spring-data-mongodb-1.8.1.RELEASE.jar:na]

Application Code

Query query = new Query();
        DateTime dateTest = new DateTime(2014, 1, 1, 0, 0);
            query.addCriteria(Criteria.where("monthYear").gte(dateTest.toDateTimeISO()));
        System.out.println(query.toString());
        List<Model> spendingsList = dbConfig.getMongoOperations().find(query, Model.class, collectionName);

The generated query looks like below:

Query: { "monthYear" : { "$gte" : { $java : 2014-01-01T00:00:00.000+01:00 } } }, Fields: null, Sort: null

Annotation used in the Model class

@DateTimeFormat(iso = ISO.DATE_TIME)
    private DateTime monthYear;

回答1:


After investigating this behavior, found the following issues and solutions.

Issue

The data stored in the MongoDB is interpreted differently when the data type is read from different MongoDB UIs. Eg: Mongovue interprets the type as DateTime, DBeaver interprets it as Timestamp

Solution Query the DB from the application with the date time object in Date format.

Example

int year;
DateTime monthYearFrom = new DateTime(year, 1, 1, 1, 0);
DateTime monthYearTo = new DateTime((year+1), 1, 1, 1, 0);
query.addCriteria(Criteria.where("monthYear").gte(monthYearFrom.toDate()).lte(monthYearTo.toDate()));


来源:https://stackoverflow.com/questions/33920945/spring-throws-converternotfound-for-range-search-on-date-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!