March 2019
Intermediate to advanced
394 pages
11h 4m
English
Introduced in MongoDB 4.0, type conversion operators allow us to convert a value to a specified type. The generic syntax of the command is as follows:
{ $convert: { input: <expression>, to: <type expression>, onError: <expression>, // Optional. onNull: <expression> // Optional. } }
In this syntax, input and to (the only mandatory arguments) can be any valid expression. In its simplest form, we could, for example, have the following:
$convert: { input: "true", to: "bool" }
This converts a string with the value true to the Boolean value true.
The onError phrase can again be any valid expression, and specifies the value that MongoDB will return if it encounters an error during conversion, including unsupported type ...
Read now
Unlock full access