theara
May 21, 2019, 12:04pm
1
I base on Financial System
.
Which one better 1 VS 2
:
1- Use mongo-decimal
to store Mongo data as NumberDecimal
2- Use as normal Double
, but we convert Double
to Decimal
by $toDecimal
in Query Data
Ex:
Collection.aggregate([
{
addFields: { amountDbl: {$toDecimal: '$amount'}}
},
{
$group:{_id: null, totalAmount: { $sum: "amountDbl"}}
},
{
$project: {total: {$toDouble: '$total'}}
}
])
Converting between number types is almost certainly going to lead to trouble.
I suggest you read the MongoDB recommendations for working with monetary data.
3 Likes
Very thanks @robfallows .
I will check soon
Or crate the new field for Decimal Type
???
Collection = {
amount : {type : Number},
amountDbl: ............... // Auto create
}
(Sorry my english not good)
Sorry. I’m not sure what you’re asking.
theara
May 21, 2019, 11:25pm
6
Thanks again, I will try read again.
Now I try to use Decimal
type in Mongo.
(mongo-decimal
package)
If you can have a fractional quantity of something, monetary prices should be specified using the mongo
decimal format.
HI all, now I have problem when fetching data with Aggregate
.
1- Meteor collection
let data = Collection.find, findOne
---
doc.decimalField.toNumber()
// Work fine with Decimal JS Method (Client/Server)
2- Use sakulstra:aggregate
or Native node driver
let data = Collection.aggregate([.......]), Collection.rawCollection().aggregate([.....])
---
doc.decimalField.toNumber()
// Don't work with Decimal JS Method (Client/Server)
Please help me…
Please don’t double-post. I’ve already provided an answer to this:
Collection.rawCollection().aggregate() returns a Promise.
If you are using it inside a Meteor Method, define your method as async and then await the result of the aggregation. You will also need to convert the aggregation cursor to an array. So something like this:
Meteor.methods({
async doMyAggregation() {
try {
return await Collection.rawCollection().aggregate(aggPipeline).toArray();
} catch(error) {
throw new Meteor.Error(error.message);
}
},
});