Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2232

Add builder for $sortByCount aggregation stage

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.4.0
    • Component/s: Builders
    • Labels:
      None
    • Sprint:
      JVM Sprint 35

      Description

      Syntax

      { $sortByCount: <arbitrary non-object expression, see 'Behavior' for why> }
      

      Examples

      > db.example.insert([
        {_id: 0, x: 1},
        {_id: 1, x: 2},
        {_id: 2, x: 1},
        {_id: 3, x: 0}
      ]);
      > db.example.aggregate([{$sortByCount: "$x"}])
      {_id: 1, count: 2}
      {_id: 0, count: 1}
      {_id: 2, count: 1}
       
      // Example #2
      > db.example.drop();
      > db.example.insert([
        {_id: 0, x: 1.4},
        {_id: 1, x: 2.3},
        {_id: 2, x: 1.1},
        {_id: 3, x: 0.5}
      ]);
      // Note this is an expression, so does not count as an "object" for the sake of erroring.
      > db.example.aggregate([{$sortByCount: {$floor: "$x"}}])
      {_id: 1, count: 2}
      {_id: 0, count: 1}
      {_id: 2, count: 1}
       
      // Example #3
      > db.example.aggregate([{$sortByCount: {field1: "$x", field2: "$y"}}])
      Error!
      

      Behavior

      • This stage is syntactic sugar for the following:

        {
          $group: {
            _id: <expression>,
            count: {$sum: 1}
          }
        },
        {$sort: {count: -1}}
        

      • We restrict the expression to not be an object so that we might be able to add further arguments to $sortByCount in the future.

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: