Learn the "why" behind slow queries and how to fix them in our 2-Part Webinar.
Register now >
Docs Menu
Docs Home
/ /

$toObject (expression operator)

$toObject

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

$toObject has the following syntax:

{
$toObject: <expression>
}

$toObject takes any valid expression.

$toObject is a shorthand for the following $convert expression:

{ $convert: { input: <expression>, to: "object" } }

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.

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, $toObject errors.

  • Does not interpret Extended JSON type wrappers such as $oid, $date, or Timestamp(...). 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.

$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.

The following table shows examples of using $toObject to convert strings to objects:

Example
Results

$toObject: "{\"a\": 1, \"b\": 2}"

{ a: 1, b: 2 }

$toObject: "{}"

{ }

$toObject: "[]"

Error: Input doesn't match expected type 'object'

$toObject: "123"

Error: Input doesn't represent valid JSON: Unexpected standalone value

$toObject: "{\"nam\\u0000e\": \"foo\"}"

Error: Input doesn't represent valid JSON: Illegal embedded null byte

Note

The string must contain characters that represent a valid object.

$toObject: "{\"name\": \"fo\\u0000o\"}"

{ name: 'fox00o' }

$toObject: "{\"a\": 1, \"b\": 2, \"a\": 3}"

{ a: 3, b: 2 }

Note

The last value for the same field is preserved.

$toObject: "{\"foo\": null}"

{ foo: null }

$toObject: "{\"foo\": false}"

{ foo: false }

$toObject: "{\"__proto__\": {\"foo\": null}}"

{ ['__proto__']: { foo: null } }

$toObject: "{\"foo\": \"NaN\"}"

{ foo: 'NaN' }

$toObject: "{\"foo\": 123}"

{ foo: 123 }

$toObject: "{\"foo\": 4294967296}"

{ foo: Long('4294967296') }

Note

The number is outside the 32-bit signed range, so it is converted to a long.

$toObject: "{\"foo\": 1.123123}"

{ foo: 1.123123 }

$toObject: "{\"foo\": 1.2e+3}"

{ foo: 1200 }

$toObject: "{\"largePos\": 18446744073709551615}"

{ largePos: 18446744073709552000 }

Note

The number is outside the 64-bit signed range, so it is converted to a double with precision loss.

$toObject: "{\"largeNeg\": -18446744073709551615}"

{ largeNeg: -18446744073709552000 }

Note

The number is outside the 64-bit signed range, so it is converted to a double with precision loss.

$toObject: null

null

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.

Back

$toLong

On this page