Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

Issue with decimal in string while persisting to DB

Hello Team,

I am facing an issue in my Java Application.

  1. While I am saving a normal string to mongo which has a decimal , I am able to persist to DB
    Ex: {“price”:“20.00”}

  2. But When I am trying to save list of decimals as a string which has precision ‘00’ (5.00) as list of strings, only one zero in precision can be able persist as below.
    Ex:
    {“values”:[
    “6.0”,
    “5.0”
    ]
    }

Our DB Server version is 3.6.+

Trying with spring-data-mongodb version 1.8.+

Please help me out in this what could be the issue.

Thanks,
Ajay Prasad.

Hello Ajay_Prasad_Goli :wave:,

It will help if you post the code which you are using to store the string decimals into the array values. Also, please specify the MongoDB Java driver and Java versions.

As such MongoDB v3.4 started supporting the decimal data type - which is more conducive to store decimal numbers (e.g., using as monetary data).

Hello @Prasad_Saya,

I am using Java 8 version & org.mongodb:mongo-java-driver:2.13.3 & org.springframework.data:spring-data-mongodb:1.8.+::

Below is the code snippet:

List list = new ArrayList();
list.add(“6.00”);
list.add(“5.00”);
Query query = new Query(Criteria.where());
Update update = new Update();
update.set(“values”, list);
mongoOperations.upsert(query, update, ValuesPojo.class); or
mongoTemplate.upsert(query, update, ValuesPojo.class);

Please let me know if any info required.

This is required: ValuesPojo.java (with the variable, and get/set methods associated with the array in question).

Hello @Prasad_Saya,

This is my class lets say it has get/set methods associated with variable.
myCollection–> is my collection name .

@Document(collection = “myCollection”)
public class ValuesPojo {

private List<String>  values;

}

Everything is available but my data persisting is different. After precision it must be two ‘0’ s like “5.00” instead of “5.0” in my collection object array as mentioned…

Please let me know if any info required.

Hi @Ajay_Prasad_Goli,

I just tried your code with available setup of Spring Data MongoDB 2.2.7, MongoDB v4.2.3, and Java SE 8.

I used MongoTemplate API for the update / upsert operation. With this input list data:
List list = Arrays.asList("100.0","6.00", "5.00");

I get the updated document as expected: { "_id" : "1", "values" : [ "100.0", "6.00", "5.00" ] }

I suspect it might be an issue with the older versions of the APIs or the database. Cannot conclude what the issue is.

Thanks @Prasad_Saya
Will check on the higher versions and try for this possibility.