On this page本页内容
The following table lists the input types that can be converted to a boolean:
| Input Type | Behavior |
|---|---|
| Boolean | No-op. Returns the boolean value. |
| Double | Returns true if not zero.
Return false if zero. |
| Decimal | Returns true if not zero.
Return false if zero. |
| Integer | Returns true if not zero.
Return false if zero. |
| Long | Returns true if not zero.
Return false if zero. |
| ObjectId | Returns true. |
| String | Returns true. |
| Date | Returns true. |
The following table lists some conversion to boolean examples:
{$toBool: false} |
false |
{$toBool: 1.99999} |
true |
{$toBool: NumberDecimal("5")} |
true |
{$toBool: NumberDecimal("0")} |
false |
{$toBool: 100} |
true |
{$toBool: ISODate("2018-03-26T04:38:28.044Z")} |
true |
{$toBool: "false"} |
true |
{$toBool: ""} |
true |
{$toBool: null} |
null |
Create a collection orders with the following documents:
The following aggregation operation on the orders collection converts the shipped to a boolean value before finding the unshipped orders:
The operation returns the following document:
Note
If the conversion operation encounters an error, the aggregation operation stops and throws an error. To override this behavior, use $convert instead.