Mongo 3.4. decimal support documentation

#1

Hi,

I noticed this pull requested being merged recently in Meteor:

It looks like that it is to add support to Mongo 3.4 NumberDecimal type that can help representing money amounts.

It there any documentation around it? Does anybody have an example on how to store and retrieve values from mongo, how to handle them in the UI, and similar?

#2

I’m not aware of any Meteor documentation/guide.
What I can see from the source code, for DB operations you obviously want to use the MongoDB documentation for it:
https://docs.mongodb.com/manual/tutorial/model-monetary-data/

For client it seems to be using decimal.js, so if I’m getting this correctly then you import it like this:

import { Decimal } from 'meteor/mongo-decimal';

And then you can use all the decimal.js functionality:
https://mikemcl.github.io/decimal.js/

The additions from the mongo-decimal package seems to be that it adds some little things like type name, toJSONValue(), clone() and adds EJSON type as well.

I have yet to use it, so this is just what comes from my understanding of the PR.

1 Like
#3

Hi storyteller and thanks for the answer.

I also indeed went through the PR code and test it a bit, and I manage to get it working.
It is basically as you say, the library relies on decimal.js so if you use a Decimal type when updating a mongo collection, thanks to the changes to EJSON it will be stored correctly in mongo 3.4+

The only difference is that I use typescript and to get the types working I had to do a little extra step:
there’s no types published for mongo-decimal, but decimal.js ships with typescript types in it.
Moreover decimal is also exposed globally to the app, not only through ES6 modules.

So the easier way for me to get going was to grab the decimal.global.d.ts file from the decimal.js repo, and put it in a place that typescript checks, and then I simply relied on the global Decimal type being there.

Anyway thanks again for the help!

1 Like
#4

How do I sum field decimal type with aggregate ?

#5

Use the aggregation $sum operator.

#6

let test = Students.aggregate([
{
$group: {
_id: null,
total: { $sum: ‘$score’ },
},
},
])[0]
console.log(test.total)
It show result

Decimal128 {
_bsontype: ‘Decimal128’,*
bytes: <Buffer 06 00 00 00 00 00 00 00 00 00 00 00 00 00 3e 30> }

How can I access properties total sum of number?

#7

We should add something about this into the docs or guide.

#8

I had the same problem with decimals and ended up with custom aggregate function

1 Like
#9

Can I insertMany with field price to store decimal type?

let data=[{id:'1', price:10}, {id:'2', price:20}]
insertMany(data)
#10

Yes, but it’s not in minimongo, so you can only do this on the server and you have to use Collection.rawCollection().insertMany.

If you want to use it on the client, you can use a Meteor method and rely on pub/sub to synchronise with the client.

#12

yes Collection.rawCollection().insertMany. but how to set value price 10 and 20 decimal type in mongo ?

#13

https://docs.mongodb.com/manual/tutorial/model-monetary-data/#using-the-decimal-bson-type

#15

I try use NumberDecimal() to insert

let data=[{id:'1', price: NumberDecimal(10)}, {id:'2', price:NumberDecimal(20)}]
Collection.rawCollection().insertMany(data)

but not working with meteor please you can example.