Hi @Navin_Jha and welcome in the MongoDB Community !
The current version of the MongoDB Java driver is 4.2.3. Please make sure to use the correct version of the driver and also not the legacy one. But this won’t solve this “issue”.
Your number is 20 billions. It’s greater than 2,147,483,647 which is the maximum positive value for a 32-bit signed binary integer. The only way for MongoDB (or any computer for that matter) to store this value is in a 64-bit integer == a long.
MongoDB is a BSON database. So it’s capable to handle basic JSON… But also more complex data types like dates, decimal128, … and longs that JSON can’t handle.
So the reason you get a long back using Java, it’s because Java is kind enough to transform automatically for you your 20000000000 into a long and avoid an integer overflow. And I guess you probably have a warning in your code that says that you should actually type 20000000000L instead.
Driver updates happen at a slow pace in large firms as you know.
The data is kept in json files that get loaded to mongo. When retrieved from mongo it is sent to consumers as json. So ideally I would like to keep json intact.
Is there a way to tell mongo:
I am sending
“rateLimit”: 20000000000
please give me back the same in in the returned json and not
“rateLimit”: {"$numberLong": “20000000000”}
You can hack the final string that is returned by toJson() before you are sending it, but I’m not even sure this is legit JSON that you are sending in the end.
If you want the same experience for integers and longs, maybe you could use their respective string values instead? With this solution, I’m sure the JSON is actually legit and it’s your client’s problem to deal with the parsing of this value into the right type.
Nice. I didn’t know that RELAXED mode. But looks like this is the default behaviour in 4.2.3 because I didn’t get the $numberLong in my example. Update !