0Pricing
MongoDB Academy · Lesson

Facets and Compound Queries

Learners will combine multiple search clauses with the compound operator and compute faceted counts for category filters alongside search results.

What Are Search Facets?

Facets are aggregated counts of how many search results fall into each category of a field. You have seen facets on e-commerce sites: 'Brand: Nike (142), Adidas (89), Puma (45)' or 'Price: Under $50 (230), $50-$100 (185)'. In Atlas Search, facets are computed server-side alongside the search results in a single query using the $searchMeta stage or the facet collector within $search.

Configuring Fields for Faceting

To facet on a field, it must be indexed with the stringFacet (for string categories) or numberFacet/dateFacet (for range buckets) data type in the Atlas Search index mapping. You can add a facet type alongside a string type on the same field. Standard string-typed fields are not facetable—you must explicitly add the facet type to your index definition.

// Index mapping with facetable fields
{
  'mappings': {
    'dynamic': false,
    'fields': {
      'title': { 'type': 'string', 'analyzer': 'lucene.standard' },
      'category': [
        { 'type': 'string' },         // for filtering
        { 'type': 'stringFacet' }     // for facet counts
      ],
      'brand': { 'type': 'stringFacet' },
      'price': [
        { 'type': 'number' },
        { 'type': 'numberFacet' }
      ]
    }
  }
}

All lessons in this course

  1. Creating an Atlas Search Index
  2. Writing $search Queries: Text, Phrase, and Wildcard
  3. Autocomplete and Fuzzy Matching
  4. Facets and Compound Queries
← Back to MongoDB Academy