$type (BSON Type)

Last updated: 2026-10-09

$type (BSON Type)

$type matches documents where a field value is of a given BSON type. `{ age: { $type: 'int' } }` matches documents where age is a 32-bit integer. Use type aliases (string, object, array, date, double) or numeric codes (1 double, 2 string). The string '21' and the number 21 are different types — a common confusion.

CategoryElement & Evaluation
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ <field>: { $type: <BSON type> } }

⚙️ Parameters

fieldThe document field whose type to test.
BSON typeA type alias or code, e.g. 'int', 'string', 'date', 'array'.

Returns:Cursor of matching documents

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $type: users whose age is an integer
// int means a 32-bit integer
db.users.find({ age: { $type: 'int' } })

// Multiple types: int or double
db.users.find({ age: { $type: ['int', 'double'] } })

Output:

// Returns users whose age is int; the second query matches int or double

📌 Notes

📚 Related Tutorials

❓ FAQ

QWhat are the common $type aliases?
Adouble, string, object, array, binData, objectId, bool, date, null, regex, int, long, decimal, timestamp, etc.
QHow do I query a date-typed field?
A{ updatedAt: { $type: 'date' } }.
QCan $type match null?
AYes, 'null' matches documents whose field value is null (the field must exist).
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏