The $type operator works with BSON data types. Use it in find() queries to filter documents by field type, or in aggregation pipelines to inspect what type a value actually is.
01
BSON Types
string, number, array.
02
Query Filter
find() and $match.
03
Expression
Return type name.
04
Type Aliases
"number", "string".
05
Use Cases
Schema cleanup, QA.
06
vs $isNumber
Type vs boolean.
Fundamentals
Definition and Usage
MongoDB stores values in BSON format, and each value has a type such as string, double, int, array, object, or null. The $type operator lets you work with those types in two ways.
As a query operator, it filters documents where a field matches a specified type. As an aggregation expression, it returns the BSON type name of a value. This is especially useful when cleaning imported data, auditing schema inconsistencies, or debugging mixed-type fields.
💡
Beginner Tip
A field can hold 42 (number) or "42" (string) — they look similar but are different BSON types. $type helps you find and fix those mismatches.
Foundation
📝 Syntax
Query Syntax (find / $match)
Filter documents where a field is a specific BSON type:
Pair $type with $in and $cond to build data-quality flags directly in your pipeline.
Applications
🚀 Use Cases
Schema auditing — find fields stored as the wrong BSON type after imports.
Data cleanup — filter string numbers like "42" before converting them.
Null detection — locate explicit null values vs missing fields.
Pipeline validation — inspect types before applying math or array operators.
Migration checks — verify documents match expected types after schema changes.
🧠 How $type Works
1
MongoDB reads the field or expression
In queries, a field path is checked. In expressions, the input expression is evaluated first.
Input
2
BSON type is determined
MongoDB identifies the stored type: string, int, array, object, null, and so on.
Type
3
Filter or return the result
Query form keeps matching documents. Expression form returns the type name string.
Output
=
🔍
Type-aware data
Find mismatches, filter by type, and build safer aggregation pipelines.
Wrap Up
Conclusion
The $type operator is a versatile tool for working with BSON types in MongoDB. Use it in queries to filter documents by field type, and in aggregation expressions to inspect what type a value actually is.
Remember the two forms: query filter { field: { $type: "string" } } vs expression { $type: "$field" }. For boolean numeric checks, see $isNumber and $isArray. Next in the series: $week.
Use expression $type to audit schema inconsistencies
Prefer "number" when any numeric subtype is acceptable
Pass type arrays when multiple types are valid
Combine with $expr for type logic inside pipelines
❌ Don’t
Confuse query and expression syntax — they behave differently
Assume "42" matches { $type: "number" }
Use expression $type as a direct query filter without $expr
Forget that missing fields return "missing" in expressions
Replace $isNumber with $type when you only need true/false
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $type
Use these points when filtering or inspecting BSON types.
5
Core concepts
🔍01
BSON Types
Filter or inspect.
Purpose
📝02
Two Forms
Query vs expr.
Syntax
🔢03
"number"
All numeric types.
Alias
🛠04
Type Arrays
Multiple matches.
Pattern
⚠05
vs $isNumber
Name vs boolean.
Compare
❓ Frequently Asked Questions
$type checks BSON data types. In find() queries it filters documents where a field matches a given type. In aggregation expressions it returns the BSON type name of a value (e.g. "string", "double", "array").
Yes. As a query operator, use { field: { $type: "string" } } in find() or $match. As an aggregation expression, use { $type: "$field" } inside $project, $addFields, or $cond to return the type name.
The alias "number" matches any numeric BSON type: int, long, double, and decimal. It is useful when you want all numbers regardless of specific numeric subtype.
Yes. In queries you can pass an array: { value: { $type: ["string", "null"] } } returns documents where value is a string or null.
$type returns the BSON type name (or filters by type in queries) and works for any type. $isNumber returns true or false for numeric types only inside aggregation expressions.