$toBool - Amazon DocumentDB

$toBool

The $toBool operator in Amazon DocumentDB converts an expression to a boolean value.

Parameters

  • expression: The expression to be converted to a boolean value.

Note: Any string converts to true.

Example (MongoDB Shell)

The following example demonstrates using the $toBool operator to normalize device state values from different data types.

Create sample documents

db.deviceStates.insertMany([ { _id: 1, deviceId: "sensor-001", status: true }, { _id: 2, deviceId: "camera-002", status: 1 }, { _id: 3, deviceId: "thermostat-003", status: "active" }, { _id: 4, deviceId: "doorlock-004", status: 0 } ]);

Query example

db.deviceStates.aggregate([ { $project: { _id: 1, deviceId: 1, isActive: { $toBool: "$status" } } } ]);

Output

[ { "_id": 1, "deviceId": "sensor-001", "isActive": true }, { "_id": 2, "deviceId": "camera-002", "isActive": true }, { "_id": 3, "deviceId": "thermostat-003", "isActive": true }, { "_id": 4, "deviceId": "doorlock-004", "isActive": false } ]

Code examples

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

Node.js
const { MongoClient } = require('mongodb'); async function main() { 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('deviceStates'); const result = await collection.aggregate([ { $project: { _id: 1, deviceId: 1, isActive: { $toBool: '$status' } } } ]).toArray(); console.log(result); await client.close(); } main();
Python
from pymongo import MongoClient def main(): 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['deviceStates'] result = list(collection.aggregate([ { '$project': { '_id': 1, 'deviceId': 1, 'isActive': { '$toBool': '$status' } } } ])) print(result) client.close() if __name__ == '__main__': main()