$type
The $type operator returns a string that identifies the BSON type of the input value.
📌 Syntax
{ "$type": <expression> }
Returns values such as "string", "int", "double", "date", "bool", "object", "array", etc.
✅ Base Example – Detect Type
📥 Input Document
{ "price": 99.99 }
📌 Expression
{ "$type": "$price" }
📤 Output
"double"
✅ Type of Nested Field
📥 Input Document
{ "metadata": { "tags": ["new", "hot"] } }
📌 Expression
{ "$type": "$metadata.tags" }
📤 Output
"array"
🧱 Ecommerce Example – Validate Price Type Before Conversion
📌 Pipeline
[
{
"$project": {
"product": "$name",
"priceType": { "$type": "$price" },
"price": 1
}
}
]
📥 Input Documents
[
{ "name": "Phone", "price": "699" },
{ "name": "Laptop", "price": 1499.99 }
]
📤 Output
[
{ "product": "Phone", "priceType": "string", "price": "699" },
{ "product": "Laptop", "priceType": "double", "price": 1499.99 }
]
🔧 Common Use Cases
- Debug complex fields before transformation
- Conditional conversion or validation
- Auditing unknown schemas
🔗 Related Operators
$convert,$ifNull,$cond,$literal
🧠Notes
- Useful when working with inconsistent or dynamic data sources
- Helps avoid runtime conversion errors