Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-6120

Value::coerceToBool is inconsistent with javascript in some cases

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.2.0-rc0
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • None

      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.

            Assignee:
            aaron Aaron Staple
            Reporter:
            aaron Aaron Staple
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: