liveBook · Manning

Facet operator

As explained in MongoDB 8.0 in Action, Third Edition

A facet operator groups matching search results by values or ranges in specified fields and returns counts for the groups.

The Atlas Search facet operator groups matching search results by values or ranges in specified fields and returns a count for each group. It is a search collector for producing metadata, rather than an operator that finds matches. Faceted counts can help users navigate a large result set by inspection outcome, sector, or other categories. section 11.5 section 11.5.1

Facet fields support dateFacet, numberFacet, and stringFacet types. Choose fields whose groups will help people refine results: a status field gives named categories, while a numeric field can supply ranges. section 11.5 section 11.5.1

Count categories after a business-name search

Search inspection records for business names matching “HOT DOG,” allowing a minor spelling error. The illustrated pipeline then uses the aggregation stage $facet to count matches separately by inspection result, inspection year, and sector. Its output includes 74 matches from 2015 and 8 from 2016. This demonstrates the value of faceted results, but $facet is an aggregation stage—not the Atlas Search facet collector. section 11.5.1 section 11.5

When adapting the example, distinguish the two APIs: use the Atlas Search facet collector for search-result metadata, or place an aggregation $facet stage after $search to run separate counting pipelines over matches. The illustrated $facet pipeline is not a copyable syntax example for the search collector. section 11.5 section 11.5.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines the collector and supported field types, then illustrates faceted counts with aggregation $facet rather than collector syntax.
Examples
Uses inspection-record searches for “HOT DOG” and “BUILDING,” grouping matches by status, year, sector, or ZIP code.
What it assumes
Assumes a searchable MongoDB Atlas collection and familiarity with aggregation pipelines; provides no search-collector setup walkthrough.
What you can do after
Evaluate/understand faceted search counts and distinguish an Atlas Search collector from an aggregation $facet stage.
How it connects
Connects facets to $search, $searchMeta, aggregation $facet, and fuzzy text search.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.