I am trying to retrieve an integer from bson document using the following code:
MongoCursor<Document> cursor = collection.find().iterator();
while (cursor.hasNext()) {
Document rowDoc = cursor.next();
int myNum = rowDoc.getInteger("number");
}
then I got this exception:
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer
As I thought number is double my change was:
double myNum = rowDoc.getDouble("number");
But this time I got:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
I've checked the value type in mongo shell returning number. So what am I doing wrong?
((Number)rowDoc.get("number")).intValue()public <T> T get("number", Number.class)is more appropriate.myNumvariable?int myNum = rowDoc.getInteger("number");will work as expected.