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;
}
それで、なぜその47.0をlongに変換できないのか、そしてその解決策は何かを知りたいのです。
注:seqObj.get("seq")
BSONObjectが生成されます。(MongoDbクエリ)
私は通常、次のことを行います。
long l = ((Number) seqObj.get("seq")).longValue();
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加