The $toString operator converts any value to a string in MongoDB aggregation pipelines. Use it to build text labels with $concat, display ObjectIds, format numbers for export, and combine mixed-type fields.
01
Stringify
Any value to text.
02
Syntax
One expression.
03
$concat
Build labels.
04
ObjectId
ID to hex text.
05
Use Cases
Export, display.
06
vs $toObjectId
String ↔ ID.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $toString operator converts an expression to its string representation. Numbers become text like "42", ObjectIds become 24-character hex strings, and booleans become "true" or "false". This is essential when you need to combine values of different types using $concat or display them in reports.
$toString is shorthand for { $convert: { input: <expression>, to: "string" } }. It is the reverse of operators like $toInt and $toObjectId, which parse strings into typed values.
💡
Beginner Tip
$concat only works with strings. Wrap numeric or ObjectId fields in $toString before concatenating them with text labels.
Converting ObjectId group keys to strings makes output easier to read in reports and JSON exports.
Applications
🚀 Use Cases
Building labels — combine text and numbers with $concat for display strings.
ObjectId display — export IDs as hex strings for APIs and reports.
Data export — convert all field types to strings for CSV or JSON output.
Composite keys — build SKU codes, reference numbers, and identifiers from mixed fields.
Debugging — inspect typed values as readable strings in pipeline output.
🧠 How $toString Works
1
MongoDB reads the expression
The input resolves from a field path, literal, or nested expression of any BSON type.
Input
2
Value is converted to text
MongoDB applies type-specific string formatting (decimal for numbers, hex for ObjectIds, ISO for dates).
Convert
3
A string result is returned
The output can be used in $concat, displayed in reports, or exported as text.
Output
=
📝
Readable text
Use in labels, exports, display fields, and string operations.
Wrap Up
Conclusion
The $toString operator converts any BSON value to a string in MongoDB aggregation pipelines. It is essential for building text labels with $concat, displaying ObjectIds, and formatting data for export.
For the reverse conversion (string to ObjectId), use $toObjectId. For custom date formatting, use $dateToString. Next in the series: $toUpper.
Use $toString on ObjectIds for API and export formats
Use $dateToString when you need custom date formatting
Keep original typed fields when you still need numeric operations
Pair with $toObjectId for round-trip ID conversion
❌ Don’t
Pass numbers directly to $concat without $toString
Use $toString on dates when you need a specific format (use $dateToString)
Assume "true"/"false" strings are user-friendly (use $cond for labels)
Forget that null input returns null
Replace numeric fields when you still need to sort or calculate on them
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $toString
Use these points when converting values to strings in MongoDB.
5
Core concepts
📝01
To String
Any → text.
Purpose
📝02
{ $toString: x }
One argument.
Syntax
🔗03
$concat
Build labels.
Pairing
🛠04
ObjectId
ID to hex.
Usage
⚠05
vs $dateToString
Dates: use that.
Compare
❓ Frequently Asked Questions
$toString converts a value to a string inside an aggregation pipeline. It works on numbers, dates, ObjectIds, booleans, and other BSON types.
The syntax is { $toString: <expression> }. The expression can be a field reference like "$score", a literal, or any value you want to stringify.
Use $toString when building text labels with $concat, displaying ObjectIds as hex strings, formatting values for export, or combining mixed-type fields into a single string.
$toString converts any value to a string. $toObjectId converts a 24-character hex string to ObjectId. They are reverse operations for ID fields.
$toString returns null when the input is null. It does not throw an error for null input.