I am moving from oracle to mongo db, as part of this migration I need to protect the data type(don't want to store number as string in mongo to enable proper indexing/aggregation performance).One of the number column in oracle db has precision 38(it can have 38 digits) and scale 0(its an integer). I am using java to persist this into mongo but parsing the number String with Long.parseLong("") won't allow more than Long.MAX_VALUE which is 19 digit long and BigInteger is not supported nativly by mongo db driver. So how can I store 38 digit long number in mongo while maintaining data type and using java driver?
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
public class Main {
public static void main(String[] args) {
MongoClient mongoClient = new MongoClient();
MongoDatabase db = mongoClient.getDatabase("test");
MongoCollection<Document> migration = db.getCollection("migration");
Document document = new Document("name","MAX_VALUE").append("number",Long.MAX_VALUE);
migration.insertOne(document);
}
}
results in
{
"_id" : ObjectId("5669c0dfd64eab11860d8e83"),
"name" : "MAX_VALUE",
"number" : NumberLong(9223372036854775807)
}
but if I do
Document document = new Document("name","20_DIGIT").append("number",Long.parseLong("12345678901234567890"));
It'll result in
Exception in thread "main" java.lang.NumberFormatException: For input string: "12345678901234567890"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:592)
at java.lang.Long.parseLong(Long.java:631)
at Main.main(Main.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
because its out of long range
Document document = new Document("name","BIG_INTEGER").append("number",new BigInteger("12345678901234567890"));
will result in
Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.math.BigInteger.
p.s. I am not planning to use entity mapping/orm.