Reputation: 529
I am using MongoDb. I have a collection named 'counter'. I am trying to retrieve a value of the field 'seq', which is stored as double inside the MongoDb.
First the value of the 'seq' field is increment by 1, which works fine. If I end up printing the value of the seq just after converting it to the string it works fine. It returns a string value.
However when I try to parse the String value such as 1.0 to Long using the Long.parseLong() or Long.valueOf() function, it throws an error.
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container java.lang.NumberFormatException: For input string: "47.0"
public long getNextSeq(){
DBCollection coll = getCollection(getDb(DB_NAME), "counters");
coll.update(new BasicDBObject().append("_id","blogId"), new BasicDBObject().append("$inc",new BasicDBObject("seq",1)));
DBObject seqObj = coll.findOne();
String seqStr = seqObj.get("seq").toString();
long seq = Long.valueOf(seqStr);
return seq;
}
So I would like to know why I can't convert that 47.0 to long, and what is the solution.
Note:
seqObj.get("seq")
would produce BSONObject. (MongoDb query)
Upvotes: 0
Views: 495
Reputation: 5322
I usually do the following:
long l = ((Number) seqObj.get("seq")).longValue();
Upvotes: 1