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

$first and $last accumulators accept null, missing and undefined

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • Query

      Many accumulators ignore null-ish values, but $first/$last do not.

      Observed behavior: If the first or last entry for a field is undefined, the $first / $last accumulator becomes undefined (the field is dropped).
      Expected behavior: Potentially the first / last non undefined field could be chosen.

      Test:

      c = db.c;
      c.drop();
      
      c.save( {} );
      c.save( { a:1 } );
      c.save( {} );
      
      // The 'a' field of the first document is undefined, so no 'z' result.
      printjson( c.aggregate( { $group:{ _id:0, z:{ $first:'$a' } } } ) );
      // The 'a' field of the last document is undefined, so no 'z' result.
      printjson( c.aggregate( { $group:{ _id:0, z:{ $last:'$a' } } } ) );
      // The 'z' result is [1], the undefined values of the first and last documents are excluded.
      printjson( c.aggregate( { $group:{ _id:0, z:{ $push:'$a' } } } ) );
      

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            aaron Aaron Staple
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: