Cheat Sheet: Type Conversion Operators
Conversion expressions, error handling, BSON type inspection, and migration examples.
$toString
Reference group: Conversion and type inspection
What it does
Converts supported values to strings; invalid conversions may error.
Syntax
{$toString: expression}
Use case
Export or integrate with string-based systems.
{ $project: { idText: { $toString: "$_id" } } }- Converts supported values to strings; invalid conversions may error.
- Typical use: Export or integrate with string-based systems.
$toInt
Reference group: Conversion and type inspection
What it does
Converts compatible values to 32-bit integer.
Syntax
{$toInt: expression}
Use case
Normalize numeric strings.
Important notes
Invalid strings or out-of-range values can fail.
{ $set: { quantityInt: { $toInt: "$quantityText" } } }- Converts compatible values to 32-bit integer.
- Typical use: Normalize numeric strings.
$toLong
Reference group: Conversion and type inspection
What it does
Converts compatible values to 64-bit integer.
Syntax
{$toLong: expression}
Use case
Counts exceeding 32-bit range.
{ $set: { viewsLong: { $toLong: "$viewsText" } } }- Converts compatible values to 64-bit integer.
- Typical use: Counts exceeding 32-bit range.
$toDouble
Reference group: Conversion and type inspection
What it does
Converts compatible values to double precision.
Syntax
{$toDouble: expression}
Use case
Normalize numeric strings where floating-point semantics are acceptable.
{ $set: { score: { $toDouble: "$scoreText" } } }- Converts compatible values to double precision.
- Typical use: Normalize numeric strings where floating-point semantics are acceptable.
$toDecimal
Reference group: Conversion and type inspection
What it does
Converts compatible values to BSON Decimal128.
Syntax
{$toDecimal: expression}
Use case
Financial calculations needing decimal semantics.
{ $set: { amount: { $toDecimal: "$amountText" } } }- Converts compatible values to BSON Decimal128.
- Typical use: Financial calculations needing decimal semantics.
$toBool
Reference group: Conversion and type inspection
What it does
Converts supported values to boolean.
Syntax
{$toBool: expression}
Use case
Normalize boolean-like BSON values.
Important notes
Text false may convert to true as a non-empty string; explicitly map textual flags.
{ $project: { flag: { $toBool: "$enabledValue" } } }false may convert to true as a non-empty string; explicitly map textual flags.- Converts supported values to boolean.
- Typical use: Normalize boolean-like BSON values.
$toDate
Reference group: Conversion and type inspection
What it does
Converts supported date strings, numbers, or ObjectIds to BSON Date.
Syntax
{$toDate: expression}
Use case
Normalize timestamps.
{ $set: { createdAtDate: { $toDate: "$createdAtText" } } }- Converts supported date strings, numbers, or ObjectIds to BSON Date.
- Typical use: Normalize timestamps.
$toObjectId
Reference group: Conversion and type inspection
What it does
Converts valid 24-character hexadecimal strings to ObjectId.
Syntax
{$toObjectId: expression}
Use case
Migrate string references.
Important notes
Invalid strings cause conversion errors.
{ $set: { customerObjectId: { $toObjectId: "$customerIdText" } } }- Converts valid 24-character hexadecimal strings to ObjectId.
- Typical use: Migrate string references.
$convert
Reference group: Conversion and type inspection
What it does
General conversion with explicit error/null behavior.
Syntax
{$convert: { input, to, onError?, onNull? }}
Use case
Process dirty imports without failing the pipeline.
{ $set: { safeQty: { $convert: { input: "$qtyText", to: "int", onError: null, onNull: 0 } } } }- General conversion with explicit error/null behavior.
- Typical use: Process dirty imports without failing the pipeline.
$type
Reference group: Conversion and type inspection
What it does
Returns BSON type name; aggregation can report missing for an absent field.
Syntax
{$type: expression}
Use case
Audit mixed-type data.
{ $project: { fieldType: { $type: "$price" } } }missing for an absent field.
When to use it
Audit mixed-type data.- Returns BSON type name; aggregation can report
missingfor an absent field. - Typical use: Audit mixed-type data.