Visit complete MongoDB roadmap

← Back to Topics List


The $type operator is an element query operator in MongoDB that allows you to select documents based on data types of their fields. This can be useful when you want to perform operations only on those documents that have specific data types for certain fields.


The basic syntax for using the $type operator is:

  fieldName: {
    $type: dataType;

Here, fieldName is the name of the field whose data type you want to check, and dataType is the BSON data type or its corresponding alias.

BSON Data Types and Aliases

MongoDB supports various data types for fields, such as String, Number, Date, etc. Some of the common BSON data types and their corresponding aliases are:

  • Double: 1 or ‘double’
  • String: 2 or ‘string’
  • Object: 3 or ‘object’
  • Array: 4 or ‘array’
  • Binary: 5 or ‘binData’
  • ObjectId: 7 or ‘objectId’
  • Boolean: 8 or ‘bool’
  • Date: 9 or ‘date’
  • Null: 10 or ‘null’
  • Regex: 11 or ‘regex’
  • Int32: 16 or ‘int’
  • Int64: 18 or ‘long’
  • Decimal128: 19 or ‘decimal’

Refer to the MongoDB documentation for a comprehensive list of supported BSON data types and their aliases.


Suppose you have a collection named products with different fields like name, price, and discount. You want to find documents that have a price field of type Double. You can use the $type operator like this:

db.products.find({ price: { $type: 'double' } });

Or use the BSON data type instead of alias:

db.products.find({ price: { $type: 1 } });

Keep in mind that the $type operator will only match documents with the exact data type specified for the field. So, if the field has an integer value, using $type with Double will not match those documents.

In summary, the $type element operator is a useful query tool for selecting documents based on the data types of their fields in MongoDB. By understanding and utilizing the BSON data types and aliases, you can effectively filter documents in your queries based on specific fields’ data types.

Community is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Roadmaps Best Practices Guides Videos Store YouTube by Kamran Ahmed

Community created roadmaps, articles, resources and journeys to help you choose your path and grow in your career.

© · FAQs · Terms · Privacy


The leading DevOps resource for Kubernetes, cloud-native computing, and the latest in at-scale development, deployment, and management.