[SERVER-1672] (new NumberLong(3754)==new NumberLong(3754)) evals to false Created: 24/Aug/10  Updated: 08/Jan/24  Resolved: 20/May/16

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

Type: Bug Priority: Trivial - P5
Reporter: Brian C. Dilley Assignee: DO NOT USE - Backlog - Platform Team
Resolution: Won't Fix Votes: 3
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu Linux - mongo client console


Issue Links:
Depends
Duplicate
is duplicated by SERVER-6010 Shell considers ObjectID("4fce7c96b29... Closed
Related
is related to SERVER-2667 Cannot compare different instances of... Closed
is related to SERVER-21861 Better Timestamp object comparison in... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Participants:

 Description   

> (new NumberLong(3754)==new NumberLong(3754))
false
>

I ran into this issue when creating a $where function that compared numbers.



 Comments   
Comment by Mira Carey [ 20/May/16 ]

Javascript as an ecosystem has made a decision not to support operator overloading, so however much we'd like to make our types operate as native types, we just can't provide those semantics.

It might be worth considering an arithmetic library solution that could be extended with our types, but that wouldn't be the asked for functionality in this ticket.

Comment by Stephen Nelson [ 30/Apr/12 ]

Workaround: use String(NumberLong(..)) to compare string representations.

Comment by Brian C. Dilley [ 17/Sep/11 ]

How is this a "feature request". We're unable to compare equality for numbers of type NumberLong! There must be a way to define an equals implementation for objects in V8... in fact, does it just not use whatever the underlying c++ object uses for equality (ie: == operator)?

Comment by Guanqun Lu [ 17/Sep/11 ]

@Antoine I agree that if it's hard to implement it in v8, then we just ignore this feature request...

Comment by Antoine Girbal [ 17/Sep/11 ]

Looking at v8 sources, it doesnt seem that there is a way to override the equals behavior.
When 2 objects are compared, they are only equal if they are the exact same object.
In v8:
i::Handle<i::Object> obj = Utils::OpenHandle(this);
i::Handle<i::Object> other = Utils::OpenHandle(*that);
if (obj->IsJSObject() && other->IsJSObject())

{ return *obj == *other; }

One way to compare would be:
NumberLong(3754).toNumber() == NumberLong(3754).toNumber()
or
NumberLong(3754).valueOf() == NumberLong(3754).valueOf()
We could modify the v8 source to enable overriding this behavior, but that would make updating v8 painful.

Comment by Moses Nakamura [ 12/Jun/11 ]

I think it's more important that (new NumberLong(3754)===new NumberLong(3754)) evaluates to true, rather than with the == equality operator. The == operator should eval both of the NumberLong(3754) to native js 3754 numbers before comparing equality, while === should keep them in NumberLong format and see if they are identical.

Comment by Guanqun Lu [ 10/Jan/11 ]

v8 doesn't have as many documents as spidermonkey does. not sure how to do this...

btw: I'm ok with the test case being commented out.
https://github.com/guanqun/mongo/commit/730a5d9501b73fc2352138a349799d51fb6c551d

Comment by Eliot Horowitz (Inactive) [ 08/Jan/11 ]

The only problem with merging this is we need to support v8 at the same time.
Anyone want to take a stab at that?
if not we can comment out the test for now.

Comment by Guanqun Lu [ 08/Jan/11 ]

Thanks to Brian's directions, I've made up a simple patch for this and add three test cases for it.
https://github.com/guanqun/mongo/commit/4db464aa29729325651cd0d41e7de015ce5033ea
though, it's only for spidermonkey.

Comment by Eliot Horowitz (Inactive) [ 26/Aug/10 ]

Hmm.. missed that.
We need to be able to do in spidermonkey and v8.
Will see if there is v8 option as well

Comment by Brian C. Dilley [ 26/Aug/10 ]

Although I haven't looked at the mongo code at all - i have embedded SpiderMonkey in various applications and game engines - usually you use an JSEqualityOp callback on the JSExtendedClass:

https://developer.mozilla.org/en/JSExtendedClass

Comment by Eliot Horowitz (Inactive) [ 25/Aug/10 ]

This is nearly impossible because of javascript semantics.

Unless someone else knows how to do this - I think is not going to be doable.

Generated at Thu Feb 08 02:57:42 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.