I'm trying to use the aggregate but encounters a problem with a large number.
Schema
amount: Number
Input data when inserting
{ type: "Deposit", amount: 10000000000000000000000 },
{ type: "Withdraw", amount: 50000000000000000000000 },
stored as
{ type: "Deposit", amount: 1e+22 }
{ type: "Withdraw", amount: 5e+22 }
Next, I try to use the aggregator to match both types and convert withdraw amount to negative by multiplying by -1
[
{
'$match': {
'$or': [
{
'event': 'Deposit'
}, {
'event': 'Withdraw'
}
]
}
}, {
'$project': {
'event': '$event',
'amount': {
'$cond': {
'if': {
'$eq': [
'$event', 'Deposit'
]
},
'then': '$amount',
'else': {
'$multiply': ['$amount', -1]
}
}
}
}
}
]
However, the problem Can't coerce out of range value 1e+22 to long. My question is, is there any other way to accomplish this with the aggregator or should I store the data different way to prevent it to become ...e+...
when the number exceeds the range.
I'm currently using MongoDB v4.4.3
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…