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: 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:
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:
Or the following command will also print the same result but here the integer value is used as an identifier:
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.
Or the integer value 4 is used to identify arrays as well:
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:
Or the integer value 1 can be used to get the same output:
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:
It is noticed that integer indicators can also be used for multiple data types as well:
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.