[JAVA-3711] Storing unsigned integer to Mongodb from java Created: 28/Apr/20  Updated: 27/Oct/23  Resolved: 01/May/20

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: 3.12.3
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: harish Assignee: Jeffrey Yemin
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

We are trying to insert an unsigned integer (20 digits) to database using java driver (3.12.13 version).   

Stored unsigned integer in BigInteger() java class.

BigInteger value = new BigInteger("18446744073709551614");

When attempting to insert to db following exception is thrown:

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.math.BigInteger.

 

Not sure if the issue is with driver or the MongoDB.

We would like to have the facility to store such data to database.

 



 Comments   
Comment by harish [ 01/May/20 ]

Thank you Jeffrey. This solves my problem.

Comment by Jeffrey Yemin [ 28/Apr/20 ]

Hi harish.alwala@gmail.com

MongoDB does not support arbitrarily large integral values, so the only type that you could use that could support the full range of BigInteger is a string.  

However, there is support for Decimal128, a 16 byte IEEE 754-2008 decimal floating point, which is large enough to hold at least the value you used in the description:

        var bigInt = new BigInteger("18446744073709551614");
        var dec = Decimal128.parse(bigInt.toString());
        bigInt = dec.bigDecimalValue().toBigIntegerExact();
        System.out.println(bigInt);

 

Comment by harish [ 28/Apr/20 ]

We are using Mongodb 4.2.5 version

Generated at Thu Feb 08 09:00:15 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.