Skip to main content

Select

Aliases: $select, $selectFields, $map

Passes along records with only the specified fields to the next stage in the pipeline. The specified fields can be existing fields from the input records or newly computed fields.

Stage as JSON

Format:

{
$select: [
<existing_field_name> or <new_field_spec>,
...
]
}

Example:

{
$select: [
"country", // return an existing field
"q1",
"q2",
{ name: "month", syntax: "getMonth(timestamp)" } // adding a new calculated field
]
}

Field selection is required somewhere in every pipeline (should I require this?). If an Aggregate stage exists in a pipeline, the calculation engine can automatically determine which fields it needs and the selection occurs automatically. Otherwise, use this Select stage to specify which fields are needed.

Select Fields

{
$select: [
"country",
"q1",
"q2",
{ name: "month", syntax: "month(timestamp)" } // adding a new calculated field
]
}

Note: Without field selection, every field in your dataset would be returned from every calculation. Aside from wasting performance, this would make dependency tracking almost useless, because it would think every field is being used everywhere (all tables, charts, etc). If you don't need a field in a visualization, it's a good practice to exclude it from the selection to stay performant and keep dependency tracking useful.