Skip to main content
$count returns how many documents contribute a value for a field.

How It Works

  • By default, only documents that have the field are counted.
  • If missing is set, documents without the field also contribute to result.

Compatibility

Field TypeSupported
TEXTNo
U64/I64/F64Yes
DATEYes
BOOLYes
KEYWORDYes
FACETNo
Field must be FAST.

Arguments

ArgumentTypeRequiredDescription
fieldstringYesField to count values for.
missingnumberNoFallback value for missing fields.
await index.aggregate({
  aggregations: {
    price_count: { $count: { field: "price" } },
  },
});

Output

{ "price_count": { "value": 9 } }