[SERVER-47200] how to compare two NumberDecimal digits in javascript Created: 31/Mar/20  Updated: 27/Oct/23  Resolved: 31/Mar/20

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Question Priority: Major - P3
Reporter: winnie wu Assignee: Dmitry Agranat
Resolution: Community Answered Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Backwards Compatibility: Fully Compatible
Participants:
Linked BF Score: 15

 Description   

Hi, 

    I am using map-reduce and in the map function I am trying to find out all objects with its value = or > or < a given decimal value. those objects are all NumberDecimal  ones. note for some reason, I can't use aggregation operation.

    My question is:  how can I compare two NumberDecimal digits in Javascripts? Does Mongo provide a function like compareTo() that I can use?

     e.g   var X=new NumberDecimal("100");
    var Y=new NumberDecimal("100.0");

in JavaScript, X== Y is false.

Perhaps I should converse the two to BigDecimal type and then do comparation? As I know, JavaScript doesn't have a native Decimal Type, and I have to use a library which define BigDecimal.

Is it possible for to use this library in map-reduce operation? How should achieve this?

by the way, I found the a lib who defines the bigDecimal : https://github.com/royNiladri/js-big-decimal#readme

but it requires me to install it like this: npm install --save js-big-decimal and then introduce it like this: var bigDecimal = require('js-big-decimal');

but I think I can't install it in mongo server.

it also suggest for web , I can do like this:

<script src="node_modules/js-big-decimal/dist/web/js-big-decimal.min.js"></script>

but I don't think It is applicable for me.

Is there any good suggestion for me to achieve my goal? Note I need to compare the two digits by <, =, and >.

 

 



 Comments   
Comment by Dmitry Agranat [ 31/Mar/20 ]

Hi winnie.wu@quest.com,

Being able to compare different decimal128 values to each other has come up for testing the database as well. We use the bsonWoCompare(obj1, obj2) function that's available in mongo shell to do so. That same function is also available in the server-side JavaScript environment with $where and mapReduce. It will return

  • 1 if obj1 > obj2
  • 0 if obj1 = obj2
  • -1 if obj1 < obj2

Some examples:

> bsonWoCompare({_: NumberDecimal("100")}, {_: NumberDecimal("100.0")})
0
> bsonWoCompare({_: NumberDecimal("100.0")}, {_: NumberLong("100")})
0
> bsonWoCompare({_: NumberDecimal("99.999999")}, {_: NumberDecimal("99.999")})
1

Note that if you're using a recent version of MongoDB (4.0.4 or later), then you don't actually need to wrap the values in {_: <value>} objects as a result of the changes from SERVER-30773.

The SERVER project is for bugs and feature suggestions for the MongoDB server. As this ticket does not appear to be a bug, I will now close it. If you need further assistance troubleshooting, I encourage you to ask our community by posting on the MongoDB Community Forums or on Stack Overflow with the mongodb tag.

Regards,
Dima

Generated at Thu Feb 08 05:13:33 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.