$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: