I tried to run some Javascript NumberDecimal tests in Mongodb3.4, but however the output is never correct, if I used the NumberInt instead, then the result is correct. Does Javascript unsupports NumberDecimal type?
The following is my testing script:
var X=new NumberDecimal("100");
var Y=new NumberDecimal("100");
var RTNEqual=(X.valueOf()==Y.valueOf());
var RTNPlus=X+Y;
print("Equal=>" + RTNEqual + " RTNPlus=>" + RTNPlus)
Output:
Equal=>false RTNPlus=>NumberDecimal("100")NumberDecimal("100")
Thank you~
I tried to run some Javascript NumberDecimal tests in Mongodb3.4, but however the output is never correct, if I used the NumberInt instead, then the result is correct. Does Javascript unsupports NumberDecimal type?
The following is my testing script:
var X=new NumberDecimal("100");
var Y=new NumberDecimal("100");
var RTNEqual=(X.valueOf()==Y.valueOf());
var RTNPlus=X+Y;
print("Equal=>" + RTNEqual + " RTNPlus=>" + RTNPlus)
Output:
Equal=>false RTNPlus=>NumberDecimal("100")NumberDecimal("100")
Thank you~
Share Improve this question asked Jun 15, 2017 at 10:47 Sally ChengSally Cheng 431 silver badge3 bronze badges 3- What are you actually trying to do? Are you going to insert the number decimal in mongo or trying to fetch it in form of Number Decimal? – Jeet Commented Jun 15, 2017 at 11:51
- Hi, I am trying to plus two values which are all in the form of NumberDecimal, here are the example: L1:{"X" : NumberDecimal("1234.01")} L2:{"Y" : NumberDecimal("5678.02")} How to write script to plus X and Y. Thanks. – Sally Cheng Commented Jun 16, 2017 at 8:55
- where are you trying to add these values? in mongo shell or in a node program? – Jeet Commented Jun 16, 2017 at 9:58
3 Answers
Reset to default 3The mongo
shell (as at 3.4.5) does not support arithmetic with NumberDecimal
values. The decimal type is not native to JavaScript, so decimal values in the shell are strings representing the BSON value stored in MongoDB.
As per discussion on SERVER-19676 in the MongoDB issue tracker:
Due to the fundamental problems inherent to doing non-floating point math in javascript, there isn't any easy system to hook into to provide arithmetic support for our types. If/when javascript provides operator overloading, or something that looks more live value types, we may revisit the issue. In the meantime, we aren't planning on re-implementing arithmetic via methods.
However, decimal values are fully supported if you manipulate on the server via the aggregation framework:
var X=new NumberDecimal("100");
var Y=new NumberDecimal("100");
db.omnum.aggregate(
{ $project: {
_id: 0,
RTNEqual: { $eq: [X, Y] },
RTNPlus: { $sum: [X, Y] }
}}
)
Output:
{
"result": [
{
"RTNEqual": true,
"RTNPlus": NumberDecimal("200")
}
],
"ok": 1
}
Thanks to @Stennie. I did it that way:
function addUpDecimalNumbers(a, b) {
var results = db.DummyCollection.aggregate([
{ $limit: 1 }, // because there would be a result for every document in the collection
{ $project: {"Sum": {$sum: [a, b]}} }
]).toArray();
return results[0].Sum;
}
var first = NumberDecimal(1.5);
var second = NumberDecimal(2.5);
var result = addUpDecimalNumbers(first, second);
print(result);
Another workaround could be to do something like this
var X=new NumberDecimal("100");
var Y=new NumberDecimal("100");
var jsonY = tojson(Y)
var jsonX = tojson(X)
var RTNEqual= jsonY == jsonX;
print("Equal=>" + RTNEqual);