select Operator
The select
operator transforms and projects document fields.
Syntax
| select { <field_mappings> }
Description
This operator transforms documents by projecting specific fields, computing new values, and restructuring the document shape. It serves as the primary mechanism for data transformation within processing pipelines.
Field Mappings
Basic Field Mapping
field: expression
Maps a field to the result of an expression.
Shorthand Field Mapping
field
Shorthand for field: field
- includes the field as-is.
Spread All Fields
...*
Includes all fields from the source document.
Spread Expression
...expression
Spreads all fields from the result of an expression.
Field Exclusion
-field
Excludes a field from the output.
Examples
Basic Projection
| select { id: id, name: name, age: age }
| select { id, name, age } // Shorthand
| select { id, name, computed_age: age + 1 }
Field Transformation
| select {
id,
temp_celsius: temperature,
temp_fahrenheit: temperature * 1.8 + 32,
is_hot: temperature > 25
}
Spread Operations
| select { id, ...* } // Include id plus all other fields
| select { ...*, alert: true } // All fields plus alert
| select { ...user, user_id: user.id } // Spread user object plus user_id
Field Exclusion
| select { ...*, -internal_field } // All fields except internal_field
| select { id, name, -password, -secret } // Include id, name, exclude sensitive fields
Complex Transformations
| select {
id,
location: { lat: latitude, lng: longitude },
status: if(active, "online", "offline"),
metadata: { ...config, timestamp: now() }
}
Array and Object Operations
| select {
id,
first_item: items[0],
item_count: items.length,
user_info: { ...user, display_name: user.first_name + " " + user.last_name }
}
Conditional Field Inclusion
| select {
id,
...(include_details ? { details: full_details } : {}),
...(is_admin ? { admin_data: sensitive_data } : {})
}
Advanced Patterns
Nested Object Restructuring
| select {
id,
contact: {
email: user.email,
phone: user.phone,
address: { ...user.address }
},
preferences: { ...user.preferences }
}
Array Transformations
| select {
id,
tags: tags.map(tag => tag.name),
scores: scores.filter(score => score > 0),
total: items.reduce((sum, item) => sum + item.value, 0)
}
Computed Fields with Functions
| select {
id,
name,
name_length: length(name),
upper_name: to_upper(name),
created_date: format_date(timestamp, "YYYY-MM-DD")
}
Performance Considerations
- Field exclusion (
-field
) works efficiently with large documents - Spread operations (
...*
) can be expensive with large objects - Complex expressions evaluate for each document
- Consider multiple SELECT operators for complex transformations