$toString is a MongoDB aggregation expression that converts a value to its string form. Numbers become text like "42", ObjectIds become 24-character hex strings, and booleans become "true" / "false". Use it when you need text for $concat, exports, or display labels.
Remember
$toString = String() for aggregation
{ $toString: <expression> }
number → "42"
boolean → "true" / "false"
ObjectId → 24-char hex
null / missing → null
Pair with $concat for labels
Dates: prefer $dateToString for formats
If building text, wrap every non-string piece before $concat.
Keep the original typed field when you still need math or sorting.
Piece
Required?
Role
$toString
Yes
Convert value → string
Expression
Yes
Field, literal, or nested expression
$concat
No
Join strings into a display label
Pseudocode
Pseudocode
for each document:
v = evaluate(expression)
if v is null or missing:
result = null
else:
result = String(v) // type-specific text form
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while writing type-conversion expressions.
Topic
Detail
Type
Aggregation expression (not a find filter)
Syntax
{ $toString: <expression> }
Equivalent
{ $convert: { input: <expr>, to: "string" } }
Number
150 → "150"
ObjectId
→ 24-character hex string
Label pattern
$concat: ["Score: ", { $toString: "$score" }]
Null
null / missing → null
Try it
Live Preview
Watch $toString turn a value into text. Pick a sample input and run the conversion.
Simulates $toString the same way as the mongosh examples below.
We use a small products collection. First convert a number to text, then build a display label with $concat. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine two products. We save name, price, and stock as documents in a collection named products.
Same stored documents. Still using Widget and Gadget from Example 1.
Stringify numbers.$toString turns price and stock into text.
Join the pieces.$concat stitches name, currency text, and counts into one label.
Easy check. Widget’s label should read Widget — $29.99 (150 in stock).
Edge Cases & Pitfalls
Check these before using $toString in production pipelines.
concat
$concat needs strings
Passing a number or ObjectId directly to $concat fails. Wrap non-strings with $toString first.
dates
Prefer $dateToString for formats
$toString on a date gives ISO-8601. Use $dateToString when you need a custom pattern.
null
Null and missing become null
Missing fields return null, not an empty string. Guard with $ifNull if you need a default.
types
Keep typed fields for math
Do not overwrite numbers with strings if you still need $sum, $sort, or comparisons. Add a separate text field instead.
Analysis
Performance Notes
Approach
Cost
Best for
$toString alone
Per-document convert
Export / display fields
$toString + $concat
Still O(docs)
Labels and SKU-style strings
Stringify late
Fewer conversions
Pipelines that filter first
Type conversion is cheap compared with large sorts or joins. Filter early with $match when only some documents need a label.
Remember
Key Takeaways
Role: any value → string in aggregation.
Labels: wrap numbers/ObjectIds before $concat.
Dates: prefer $dateToString for custom formats.
Null: null / missing → null.
One line:$toString answers “what is this value as text?”—so you can concat, export, or display it safely.
Frequently Asked Questions
$toString converts a value to its string representation inside an aggregation pipeline. Numbers become decimal text, ObjectIds become 24-character hex strings, and booleans become "true" or "false".
Use { $toString: <expression> }. The expression can be a field path like "$price", a literal, or any nested expression you want to stringify.
Use it before $concat when mixing numbers or ObjectIds with text, when exporting IDs as hex strings, or when building display labels and SKU codes from mixed field types.
$toString gives a default string form for any type (dates become ISO-8601). Prefer $dateToString when you need a custom date format such as YYYY-MM-DD.
$toString returns null when the input is null or the field is missing. It does not throw an error for null input.
Yes for the idea. Think of $toString as MongoDB’s aggregation version of String()—run it in the pipeline, not as a standalone find() filter.