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

XMLWordPrintableJSON

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

      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 - Query Team (Inactive)
            Reporter:
            Aaron Staple (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: