1

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.

1 Answer 1

3

I don't think it is possible to save a 38 digit integer in a single field in mongo. Looking at Mongo's supported types we see that the biggest integer they support are 64 bit longs. The biggest value of a 64 bit number is 9223372036854775807, which is less than 38 digits.

Sign up to request clarification or add additional context in comments.

1 Comment

So this means that you need to to store it as a String and then convert it back to a BigInteger in you application logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.