$toString
New from version 4.0
The $toString operator in Amazon DocumentDB is used to convert a value of any type (except for null) to a string representation. This can be useful when you need to perform string operations on values that are not originally in string format.
Parameters
-
expression: The expression to convert to a string.
Example (MongoDB Shell)
The following example demonstrates how to use the $toString operator to convert numeric values to strings.
Create sample documents
db.numbers.insertMany([ { "_id": 1, "value": 42 }, { "_id": 2, "value": 3.14 } ]);
Query example
db.numbers.aggregate([ { $project: { _id: 1, valueAsString: { $toString: "$value" } }} ]);
Output
{ "_id": 1, "valueAsString": "42" }
{ "_id": 2, "valueAsString": "3.14" }
Code examples
To view a code example for using the $toString command, choose the tab for the language that you want to use: