MongoDB

How to use $type operator in MongoDB

MongoDB falls under the NoSQL type of databases and provides extensive support for multiple operator classes. The element query operators’ class in MongoDB consists of two operators; $type and $exists. The latter one is used to check the existence of a field in a document, whereas $type filters the documents based on fields that contain BSON type of data. The $type operator is quite useful when the collection is complex and prediction of the datatypes of any field is impossible. The BSON type in MongoDB works for all well-known data types Double, String, Array, Object, Boolean, and so on.

In this descriptive guide, a detailed overview and usage of the $type operator will be provided.

Firstly, start with the basic working of $type operator:

How $type works

As described above, the $type operator works on the BSON type in MongoDB, and it offers two identifiers for each BSON type; one is “integer” and the other is “string“. For instance, to locate a Double data type, one can use integer value “1” and a string “double” to locate the Double data type in the specified field. The syntax of the $type operator is given below:

{field: {$type: [<BSON-TYPE1>, <BSON-TYPE2>, <BSON-TYPE3>....]}}

field: Name of the field in a collection of MongoDB database

BSON-TYPE(s): This indicates the category of BSON type you want to search for in a specific field i.e., String, Array, Double etc.

Prerequisites

Before getting into details, you must have the following prerequisites present on your system to get started with the $type operator in MongoDB.

Mongo Shell: You must be logged in to Mongo shell to execute MongoDB commands:

MongoDB database: In this article, a database named “linuxhint” will be used:

Collection: MongoDB operates on collections of a database; we have associated the “score” collection to the “linuxhint” database:

Documents: The data inserted in a collection of MongoDB database

How $type works in MongoDB

While in this section, it is assumed that you have the database, a collection, and a set of documents inside that collection.

In our case, firstly we will show the documents present in the “authors” collection: for this, use the following command in the mongo shell:

> db.authors.find().pretty()

Text Description automatically generated

Example 1: Basic use of $type operator

The $type operator has a long list of supported BSON types that can be used to refine the result of a query.

Using $type to locate a string : Referring to the string type; the following commands will print all the documents that have string values in “cat” field:

> db.authors.find({cat: {$type: "string"}}).pretty()

Text Description automatically generated

Or the following command will also print the same result but here the integer value is used as an identifier:

> db.authors.find({cat: {$type: 2}}).pretty()

Text Description automatically generated

Using $type to locate array : The commands mentioned below will print the documents that have arrays type in “cat” field:

Both commands display the document in which the “cat” field has an array value.

> db.authors.find({cat: {$type: "array"}}).pretty()

Text Description automatically generated

Or the integer value 4 is used to identify arrays as well:

> db.authors.find({cat: {$type: 4}}).pretty()

Text Description automatically generated

Using $type to locate a Double : You can locate field that contains Double values as well; the command mentioned below will look for Double values in a “cat” field:

> db.authors.find({cat: {$type: "double"}}).pretty()

Text Description automatically generated

Or the integer value 1 can be used to get the same output:

> db.authors.find({cat: {$type: 1}}).pretty()

Text Description automatically generated

It is observed that only that document is displayed that contains the “Double” value in the “cat” field.

Example 2: Use of $type to match multiple data types

In the above example, use of $type is described to retrieve a field by passing only a single data type at a time. The $type command can be used for multiple datatypes of a field as well:

For instance, the command state below will look for array and double datatype in “cat” field:

> db.authors.find({cat: {$type: ["array","double"]}}).pretty()

Text Description automatically generated with medium confidence

It is noticed that integer indicators can also be used for multiple data types as well:

> db.authors.find({cat: {$type: [4,1]}}).pretty()

Text Description automatically generated

Conclusion

The query operators in MongoDB have a key role in retrieving the relevant content from any collection of a database. The $type operator in MongoDB is used to get those documents that match the BSON type specified in the command. This guide specifically targets the $type operator and we have explained its usage with the help of examples. $type plays a key role in identifying the datatype of a field in a complex set of documents. The Mongo users may follow this guide to check the data types of the data inserted inside documents of MongoDB.

About the author

Adnan Shabbir