Type Conversion

<< Click to Display Table of Contents >>

Current:  Create Calculated Field > Mongo Calculated Field Functions List 

Type Conversion

Previous pageReturn to chapter overviewNext page

Functions

Syntax

Instructions

Examples

convert

{$convert:{

input: <expression>,

to: <type expression>,

onError: <expression>,

onNull: <expression> }

}

Converts a value to a specified type.

For example:

{ $convert: { input: "100",to: "int"} }

toBool

{

$toBool: <expression>

}

Converts a value to a boolean.

For example:

{$toBool: 1.99999}

toDate

{

  $toDate: <expression>

}

Converts a value to a date. If the value cannot be converted to a date, $toDate errors. If the value is null or missing, $toDate returns null.

For example:

{$toDate: "2018-03-03"}

toDecimal

{

  $toDecimal: <expression>

}

Converts a value to a decimal. If the value cannot be converted to a decimal, $toDecimal errors. If the value is null or missing, $toDecimal returns null.

For example:

{$toDecimal: 2.5}

toDouble

{

  $toDouble: <expression>

}

Converts a value to a double. If the value cannot be converted to an double, $toDouble errors. If the value is null or missing, $toDouble returns null.

For example:

{$toDouble: "5.5"}

toInt

{

  $toInt: <expression>

}

Converts a value to an integer. If the value cannot be converted to an integer, $toInt errors. If the value is null or missing, $toInt returns null.

For example:

{$toInt: 1.99999}

toLong

{

  $toLong: <expression>

}

Converts a value to a long. If the value cannot be converted to a long, $toLong errors. If the value is null or missing, $toLong returns null.

For example:

{$toLong: 1.99999}

toObjectId

{

  $toObjectId: <expression>

}

Converts a value to an ObjectId. If the value cannot be converted to an ObjectId, $toObjectId errors. If the value is null or missing, $toObjectId returns null.

For example:

{$toObjectId: "5ab9cbfa31c2ab715d42129e"}

toString

{

  $toString: <expression>

}

Converts a value to a string. If the value cannot be converted to a string, $toString errors. If the value is null or missing, $toString returns null.

For example:

{$toString: 2.5}