12 Topics
Chapter•24 Minutes

Cheat Sheet: Type Conversion Operators

Conversion expressions, error handling, BSON type inspection, and migration examples.

$toString

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$toString: practical example
{ $project: { idText: { $toString: "$_id" } } }
💡 Explanation Converts supported values to strings; invalid conversions may error. When to use it Export or integrate with string-based systems.
Key Takeaways
  • Converts supported values to strings; invalid conversions may error.
  • Typical use: Export or integrate with string-based systems.

$toInt

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$toInt: practical example
{ $set: { quantityInt: { $toInt: "$quantityText" } } }
💡 Explanation Converts compatible values to 32-bit integer. When to use it Normalize numeric strings. Caution Invalid strings or out-of-range values can fail.
Key Takeaways
  • Converts compatible values to 32-bit integer.
  • Typical use: Normalize numeric strings.

$toLong

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$toLong: practical example
{ $set: { viewsLong: { $toLong: "$viewsText" } } }
💡 Explanation Converts compatible values to 64-bit integer. When to use it Counts exceeding 32-bit range.
Key Takeaways
  • Converts compatible values to 64-bit integer.
  • Typical use: Counts exceeding 32-bit range.

$toDouble

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$toDouble: practical example
{ $set: { score: { $toDouble: "$scoreText" } } }
💡 Explanation Converts compatible values to double precision. When to use it Normalize numeric strings where floating-point semantics are acceptable.
Key Takeaways
  • Converts compatible values to double precision.
  • Typical use: Normalize numeric strings where floating-point semantics are acceptable.

$toDecimal

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$toDecimal: practical example
{ $set: { amount: { $toDecimal: "$amountText" } } }
💡 Explanation Converts compatible values to BSON Decimal128. When to use it Financial calculations needing decimal semantics.
Key Takeaways
  • Converts compatible values to BSON Decimal128.
  • Typical use: Financial calculations needing decimal semantics.

$toBool

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$toBool: practical example
{ $project: { flag: { $toBool: "$enabledValue" } } }
💡 Explanation Converts supported values to boolean. When to use it Normalize boolean-like BSON values. Caution Text false may convert to true as a non-empty string; explicitly map textual flags.
Key Takeaways
  • Converts supported values to boolean.
  • Typical use: Normalize boolean-like BSON values.

$toDate

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$toDate: practical example
{ $set: { createdAtDate: { $toDate: "$createdAtText" } } }
💡 Explanation Converts supported date strings, numbers, or ObjectIds to BSON Date. When to use it Normalize timestamps.
Key Takeaways
  • Converts supported date strings, numbers, or ObjectIds to BSON Date.
  • Typical use: Normalize timestamps.

$toObjectId

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$toObjectId: practical example
{ $set: { customerObjectId: { $toObjectId: "$customerIdText" } } }
💡 Explanation Converts valid 24-character hexadecimal strings to ObjectId. When to use it Migrate string references. Caution Invalid strings cause conversion errors.
Key Takeaways
  • Converts valid 24-character hexadecimal strings to ObjectId.
  • Typical use: Migrate string references.

$convert

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$convert: practical example
{ $set: { safeQty: { $convert: { input: "$qtyText", to: "int", onError: null, onNull: 0 } } } }
💡 Explanation General conversion with explicit error/null behavior. When to use it Process dirty imports without failing the pipeline.
Key Takeaways
  • General conversion with explicit error/null behavior.
  • Typical use: Process dirty imports without failing the pipeline.

$type

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$type: practical example
{ $project: { fieldType: { $type: "$price" } } }
💡 Explanation Returns BSON type name; aggregation can report missing for an absent field. When to use it Audit mixed-type data.
Key Takeaways
  • Returns BSON type name; aggregation can report missing for an absent field.
  • Typical use: Audit mixed-type data.