$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 aggregation operator returns the BSON data type of a specified field. This is useful for identifying the data type of field values during aggregation operations.

Parameters

  • expression: The field or expression whose type to return.

Example (MongoDB Shell)

The following example demonstrates using the $type operator to identify the data type of the price field for each product.

Create sample documents

db.inventory.insertMany([ { _id: 1, item: "Notebook", price: 15.99 }, { _id: 2, item: "Pen", price: "2.50" }, { _id: 3, item: "Eraser", price: 1 }, { _id: 4, item: "Ruler", price: null } ]);

Query example

db.inventory.aggregate([ { $project: { item: 1, price: 1, priceType: { $type: "$price" } } } ]);

Output

[ { _id: 1, item: 'Notebook', price: 15.99, priceType: 'double' }, { _id: 2, item: 'Pen', price: '2.50', priceType: 'string' }, { _id: 3, item: 'Eraser', price: 1, priceType: 'int' }, { _id: 4, item: 'Ruler', price: null, priceType: 'null' } ]

Code examples

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

Node.js
const { MongoClient } = require('mongodb'); async function example() { 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('inventory'); const result = await collection.aggregate([ { $project: { item: 1, price: 1, priceType: { $type: "$price" } } } ]).toArray(); console.log(result); await client.close(); } example();
Python
from pymongo import MongoClient def example(): 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['inventory'] result = list(collection.aggregate([ { '$project': { 'item': 1, 'price': 1, 'priceType': { '$type': '$price' } } } ])) print(result) client.close() example()