Facet class

$facet aggregation stage

Stage description

Processes multiple aggregation pipelines within a single stage on the same set of input documents. Each sub-pipeline has its own field in the output document where its results are stored as an array of documents.

The $facet stage allows you to create multi-faceted aggregations which characterize data across multiple dimensions, or facets, within a single aggregation stage. Multi-faceted aggregations provide multiple filters and categorizations to guide data browsing and analysis. Retailers commonly use faceting to narrow search results by creating filters on product price, manufacturer, size, etc.

Input documents are passed to the $facet stage only once. $facet enables various aggregations on the same set of input documents, without needing to retrieve the input documents multiple times.

Example:

Dart code:

Facet({
  'categorizedByTags': [
    Unwind(Field('tags')),
    SortByCount(Field('tags'))
  ],
  'categorizedByPrice': [
    Match(where.exists('price').map['\$query']),
    Bucket(
      groupBy: Field('price'),
      boundaries: [0, 150, 200, 300, 400],
      defaultId: 'Other',
      output: {
        'count': Sum(1),
        'titles': Push(Field('title'))
      }
    )
  ],
  'categorizedByYears(Auto)': [
    BucketAuto(
      groupBy: Field('year'),
      buckets: 4
    )
  ]
}).build()

Equivalent aggreagtion stage:

{
 $facet: {
   "categorizedByTags": [
     { $unwind: "$tags" },
     { $sortByCount: "$tags" }
   ],
   "categorizedByPrice": [
     { $match: { price: { $exists: true } } },
     {
       $bucket: {
         groupBy: "$price",
         boundaries: [  0, 150, 200, 300, 400 ],
         default: "Other",
         output: {
           "count": { $sum: 1 },
           "titles": { $push: "$title" }
         }
       }
     }
   ],
   "categorizedByYears(Auto)": [
     {
       $bucketAuto: {
         groupBy: "$year",
         buckets: 4
       }
     }
   ]
 }
}

https://docs.mongodb.com/manual/reference/operator/aggregation/facet/

Inheritance

Constructors

Facet(Map<String, List<AggregationStage>> pipelines)
Creates $facet aggregation stage

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

build() Map<String, Object>
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited