collection.distinct(...).query(...).as(Long.class) not casting as expected
Closed this issue · 0 comments
Hello,
Using Jongo 1.3.0 and Java 1.8
Database side:
Collection of object where one of the field is a number (no decimal) (let's say the field is named 'a').
Code side:
theCollection.distinct("a").query("{a:{$lte:5}}").as(Long.class)
Result:
List where the type of the elements is Integer (seen through Eclipse debugger). (Please see screenshot)
Expected:
List where elements are of type Long.
private final Map<String, List<Long>> cacheLocMMSI = new HashMap<>();
and
this.cacheLocMMSI.put(positionBefore.toString(), this.positionCollection.distinct("MMSI")//
.query("{location: {$near:{$geometry:{type:'Point',coordinates:#},$maxDistance: 5000}},SOG:{$lte:1}}", positionBefore)//
.as(Long.class));
An error is thrown later when I do a for loop:
final List<Long> mmsis = new ArrayList<>(this.cacheLocMMSI.get(positionBefore.toString()));
for (int i = 0; i < mmsis.size(); i++) {
final Long mmsi = mmsis.get(i); // Error thrown here
}
The error is :
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
Workaround:
I create a list of Integer then I stream it like this:
.stream().map(Long::valueOf).collect(Collectors.toList())
From here, I got a List of Long that is usable on a list without exceptions.