$type - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$type

The $type operator is used to check the data type of a field in a document. It can used when type-specific operations or validations are needed. The $type operator returns the BSON type of the evaluated expression. The returned type is a string, which corresponds to the type of the field or expression.

Planner version 2.0 added index support for $type.

Parameters

  • expression : The expression to evaluate.

Example (MongoDB Shell)

Create sample documents

db.documents.insertMany([ { _id: 1, name: "John", age: 30, email: "john@example.com" }, { _id: 2, name: "Jane", age: "25", email: 123456 }, { _id: 3, name: 123, age: true, email: null } ]);

Query example

db.documents.find({ $or: [ { age: { $type: "number" } }, { email: { $type: "string" } }, { name: { $type: "string" } } ] })

Output

[ { "_id": 1, "name": "John", "age": 30, "email": "john@example.com" }, { "_id": 2, "name": "Jane", "age": "25", "email": 123456 } ]

This query will return the documents where the age field is of type "number", the email field is of type "string", and the name field is of type "string".

Code examples

To view a code example for using the $type command, choose the tab for the language that you want to use:

Node.js
const { MongoClient } = require('mongodb'); async function findByType() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('documents'); const results = await collection.find({ $or: [ { age: { $type: 'number' } }, { email: { $type: 'string' } }, { name: { $type: 'string' } } ] }).toArray(); console.log(results); client.close(); } findByType();
Python
from pymongo import MongoClient def find_by_type(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] collection = db['documents'] results = list(collection.find({ '$or': [ {'age': {'$type': 'number'}}, {'email': {'$type': 'string'}}, {'name': {'$type': 'string'}} ] })) print(results) client.close() find_by_type()