[SERVER-6120] Value::coerceToBool is inconsistent with javascript in some cases Created: 18/Jun/12  Updated: 11/Jul/16  Resolved: 03/Jul/12

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 2.2.0-rc0

Type: Improvement Priority: Major - P3
Reporter: Aaron Staple Assignee: Aaron Staple
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-6203 Aggregation operators should have wel... Backlog
Participants:

 Description   

Javascript treats empty strings and NaN as false, but Value::coerceToBool treats them as true.

Test

c = db.c;
c.drop();
 
// Save some values.                                                                              
[
 1,
 0,
 true,
 false,
 {},
 [],
 null,
 '',
 0/0
 ].forEach( function( x ) { c.save( { a:x } ); } );
 
// Return each value and its boolean coerced value.                                               
result = c.aggregate( { $project: { a:'$a', bool:{ $and:[ '$a' ] } } } ).result;
for( i in result ) {
    r = result[ i ];
    // Find the boolean coerced value in javascript and check that it matches the value from      
    // the server.                                                                                
    expectedBool = !!r.a;
    assert.eq( expectedBool, r.bool, 'Expected ' + expectedBool + ' for (' + r.a + ').' );
}
 

Observed behavior:

{ $and:[ '' ] }

evaluates to true.
Expected behavior:

{ $and:[ '' ] }

evaluates to false.



 Comments   
Comment by auto [ 03/Jul/12 ]

Author:

{u'date': u'2012-07-02T16:08:27-07:00', u'name': u'Aaron', u'email': u'aaron@10gen.com'}

Message: SERVER-6120 Clarify comments to indicate that Value::coerceToBool() is intended to be consistent with BSONElement::trueValue() and add a test.
Branch: master
https://github.com/mongodb/mongo/commit/2f86d853fc324cd50b4713fab50cda91803d19e5

Comment by auto [ 03/Jul/12 ]

Author:

{u'date': u'2012-07-02T16:05:27-07:00', u'email': u'aaron@10gen.com', u'name': u'Aaron'}

Message: SERVER-6120 Remove unused Value::coerceToBoolean().
Branch: master
https://github.com/mongodb/mongo/commit/3738f69346b146f7d81f0c1468003c505c467f8f

Comment by Eliot Horowitz (Inactive) [ 27/Jun/12 ]

Yeah - I think BSONElement::trueValue is actually what we want.

Comment by Aaron Staple [ 26/Jun/12 ]

Also now that I look at it again this comment was what made me think js behavior was desired:

        /*
          Coerce (cast) a value to a Boolean Value, using JSON rules.
 
          @returns the Boolean Value value
        */

But I can change to make it based on BSONElement::trueValue

Comment by Eliot Horowitz (Inactive) [ 26/Jun/12 ]

It should be BSONElement::trueValue

Comment by Aaron Staple [ 25/Jun/12 ]

I don't have strong opinions about it. You might want to discuss with Andy.

Comment by Mathias Stearn [ 22/Jun/12 ]

I'm not sure if we want JS semantics or BSONElement::trueValue or something else. Lets chat about this next thursday when I get back from MongoDC

Generated at Thu Feb 08 03:10:48 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.