$type
The $type operator is used to check the data type of a field in a document. It can used when type-specific operations or validations are needed. The $type operator returns the BSON type of the evaluated expression. The returned type is a string, which corresponds to the type of the field or expression.
Planner version 2.0 added index support for $type.
Parameters
-
expression: The expression to evaluate.
Example (MongoDB Shell)
Create sample documents
db.documents.insertMany([ { _id: 1, name: "John", age: 30, email: "john@example.com" }, { _id: 2, name: "Jane", age: "25", email: 123456 }, { _id: 3, name: 123, age: true, email: null } ]);
Query example
db.documents.find({ $or: [ { age: { $type: "number" } }, { email: { $type: "string" } }, { name: { $type: "string" } } ] })
Output
[
{ "_id": 1, "name": "John", "age": 30, "email": "john@example.com" },
{ "_id": 2, "name": "Jane", "age": "25", "email": 123456 }
]
This query will return the documents where the age field is of type "number", the email field is of type "string", and the name field is of type "string".
Code examples
To view a code example for using the $type command, choose the tab for the language that you want to use: