MongoDB $type Query Operator

In MongoDB, you can use the $type element query operator to filter a collection of documents based on BSON type. It returns just those documents that have a particular BSON type in a specific field.

You provide the field and the BSON type, and $type will return all documents that match.

Example

Suppose we insert the following documents into a collection called employees:

db.employees.insertMany([
    { _id: 1, name: "Sandy", remuneration: "Too Much!!!" },
    { _id: 2, name: "Sarah", remuneration: NumberInt(128000) },
    { _id: 3, name: "Fritz", remuneration: 25000 },
    { _id: 4, name: "Chris", remuneration: NumberDecimal("45000.75") },
    { _id: 5, name: "Beck", remuneration: "10% commission" },
    { _id: 6, name: "Peter", remuneration: "70K" },
    { _id: 7, name: "Homer", remuneration: null },
    ])

Each document contains an employee along with that employee’s remuneration.

However, while each document has a remuneration field, we can see that there’s no consistency across the documents with regards to the BSON type being used in this field.

We can use the $type query operator to return just those documents for which the remuneration field is of a certain BSON type.

Example:

db.employees.find( { remuneration: { $type: "double" } } )

Result:

{ "_id" : 3, "name" : "Fritz", "remuneration" : 25000 }

In this case, only one document has a BSON type of double in the remuneration field.

Let’s see which ones contain strings:

db.employees.find( { remuneration: { $type: "string" } } )

Result:

{ "_id" : 1, "name" : "Sandy", "remuneration" : "Too Much!!!" }
{ "_id" : 5, "name" : "Beck", "remuneration" : "10% commission" }
{ "_id" : 6, "name" : "Peter", "remuneration" : "70K" }

The number Alias

You can use the number alias as a convenient way to return documents that contain numeric types.

The number alias matches against the following BSON types:

  • double
  • 32-bit integer
  • 64-bit integer
  • decimal

Example:

db.employees.find( { remuneration: { $type: "number" } } )

Result:

{ "_id" : 2, "name" : "Sarah", "remuneration" : 128000 }
{ "_id" : 3, "name" : "Fritz", "remuneration" : 25000 }
{ "_id" : 4, "name" : "Chris", "remuneration" : NumberDecimal("45000.75") }

Check for Multiple Types

You can check for multiple types by providing an array of BSON types.

Example:

db.employees.find( { 
  remuneration: { $type: ["double", "int", "null"] } 
  } )

Result:

{ "_id" : 2, "name" : "Sarah", "remuneration" : 128000 }
{ "_id" : 3, "name" : "Fritz", "remuneration" : 25000 }
{ "_id" : 7, "name" : "Homer", "remuneration" : null }

Filter by Number

Each BSON type has a corresponding number and alias (as outlined in the MongoDB documentation).

The previous examples use the alias. You can alternatively use the number instead of the BSON type alias.

Example:

db.employees.find( { 
  remuneration: { $type: [1, 16, 10] } 
  } )

Result:

{ "_id" : 2, "name" : "Sarah", "remuneration" : 128000 }
{ "_id" : 3, "name" : "Fritz", "remuneration" : 25000 }
{ "_id" : 7, "name" : "Homer", "remuneration" : null }

In this case, I provided an array of numbers that correspond to double, int, and null (which returns the same result as the previous example).

Prior to MongoDB 3.2, only numbers were accepted with the $type query operator. String aliases have only been accepted since MongoDB 3.2.

Return a Field’s Type

There’s also a $type aggregation pipeline operator that allows you to get the BSON type of a field’s value.