Definition
$toObjectConverts a string to an object. If the value cannot be converted,
$toObjecterrors. If the value isnullor missing,$toObjectreturns null.$toObjecthas the following syntax:{ $toObject: <expression> } $toObjecttakes any valid expression.$toObjectis a shorthand for the following$convertexpression:{ $convert: { input: <expression>, to: "object" } }
Behavior
Input Type Expectations
The following table describes the behavior of $toObject for
different input types:
Input Type | Behavior |
|---|---|
String | Returns a document corresponding to the content within the string. The string must contain characters that represent a valid JSON object. |
Null or Missing | Returns null. |
Parsing Rules
When converting a string to an object, $toObject:
Requires valid JSON syntax. Comments and trailing commas are not allowed.
Requires the top-level value to be an object. If the string does not represent an object,
$toObjecterrors.Does not interpret Extended JSON type wrappers such as
$oid,$date, orTimestamp(...). These remain strings or nested objects in the result.Preserves the last value when the object contains duplicate field names. Earlier values for the same field are discarded.
Numeric Type Mapping
$toObject converts numeric types based on their value and format:
Integers within the 32-bit signed range become
int.Integers outside the 32-bit range but within the 64-bit signed range become
long.Integers outside the 64-bit signed range become
double, which can result in loss of precision.Numbers with a decimal point or exponent notation become
double.
Examples
The following table shows examples of using $toObject to convert
strings to objects:
Example | Results |
|---|---|
| { a: 1, b: 2 } |
| { } |
| Error: Input doesn't match expected type 'object' |
| Error: Input doesn't represent valid JSON: Unexpected standalone value |
| Error: Input doesn't represent valid JSON: Illegal embedded null byte NoteThe string must contain characters that represent a valid object. |
| { name: 'fox00o' } |
| { a: 3, b: 2 } NoteThe last value for the same field is preserved. |
| { foo: null } |
| { foo: false } |
| { ['__proto__']: { foo: null } } |
| { foo: 'NaN' } |
| { foo: 123 } |
| { foo: Long('4294967296') } NoteThe number is outside the 32-bit signed range, so it is converted to a long. |
| { foo: 1.123123 } |
| { foo: 1200 } |
| { largePos: 18446744073709552000 } NoteThe number is outside the 64-bit signed range, so it is converted to a double with precision loss. |
| { largeNeg: -18446744073709552000 } NoteThe number is outside the 64-bit signed range, so it is converted to a double with precision loss. |
| null |
Convert String to Object
Create a collection with strings stored in a field:
db.jsonStrings.insertOne({ _id: 1, config: '{"feature": true, "threshold": 10}' })
The following aggregation converts the string in config to an
object:
db.jsonStrings.aggregate([ { $project: { _id: 0, parsedConfig: { $toObject: "$config" } } } ])
This operation returns a document where parsedConfig is a nested
document with a boolean and an integer value:
{ parsedConfig: { feature: true, threshold: 10 } }
Note
If the conversion operation encounters an error, the aggregation
operation stops and throws an error. To override this behavior, use
$convert instead.