How to round decimals on DB without using Decimal128 type

Hi all, I have a question about the decimal support. I want to work with decimals and I know Mongo suggests to use Decimal128 type. It solves the rounding format on db but on the other hand I need to write decimal(BSON) to number converter getters while getting data.

I want to use decimals without changing data type. I use mongoose’s $inc, $dec operators a lot with bulk operations. If I continue to use Number(on mongoose, Double on Mongo) type, I can see very long decimals end of the some operations. For example, if I $inc 2.4 with 1.2 result will be 3.5999999999999996 (floating point numbers problem).

Is there a way to say Mongo to round selected fields’ final values’ to max 2 via mongoose model or directly on db somehow? Setters don’t work because there is no problem with 2.4 or 1.2, the problem is the result of the inc operation. Or post hooks don’t work with bulks.

Thanks in advance.

Hi Furkan,

Can you provide a small example code sample showing exactly how you are inserting and updating the documents in Mongoose please?