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

Add builder for $facet 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

      {
        $facet: {
          facetName1: [<stage1>, <stage2>, <stage3>],
          facetName2: [<stage4>, <stage5>, …],
          ...
        }
      }
      

      Examples

      > db.example.insert([
          {
            _id: "product1",
            avgReview: 4.3,
            price: 21,
            category: "TVs",
            title: "Sony 42 inch HDTV",
            attributes: [
              {name: "type", value: "HD"},
              {name: "screen size", value: 42},
              {name: "manufacturer", value: "Sony"},
              ...
            ]
          }
          ...
      ])
      > db.example.aggregate([
      {$match: {category: "TVs"}},  // Match all relevant results.
      {$facet: {
        "Screen Sizes": [
           {$unwind: "$attributes"},
           {$match: {"attributes.name": "screen size"}},
           {$group: {
               _id: { /* Use $switch to compute buckets for screen sizes */ },
               count: {$sum: 1}
           }}
         ],
       
         "Manufacturer": [
           {$match: {"attributes.name": "manufacturer"}},
           {$group: {_id: "$attributes.value", count: {$sum: 1}}},
           {$sort: {count: -1}}
           {$limit: 5}
         ]
      }}
      ])
       
      // Output.
      {
        "Screen Size": [
          {_id: {min: MinKey, max: 32}, count: 32},
          {_id: {min: 32, max: 42}, count: 32},
          {_id: {min: 42, max: 50}, count: 33},
          {_id: {min: 50, max: MaxKey}, count: 33}
        ],
        "Manufacturer": [
          {_id: "Sony", count: 54},
          {_id: "Samsung", count: 45},
          {_id: "LG", count: 34},
          {_id: "Sharp", count: 22},
          {_id: "Vizio", count: 14},
        ]
      }   
      

      Behavior

      • names of sub-pipelines (keys in the document) must be unique.
      • This is a blocking stage.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved: