Database Manual / Reference / Query Language / Expressions

$toBool (expression operator)(表达式运算符)

Definition定义

$toBool

Converts a value to a boolean.将值转换为布尔值。

$toBool has the following syntax:具有以下语法:

{
$toBool: <expression>
}

The $toBool takes any valid expression.$toBool接受任何有效表达式。

The $toBool is a shorthand for the following $convert expression:$toBool是以下$convert表达式的简写:

{ $convert: { input: <expression>, to: "bool" } }

Behavior行为

The following table lists the input types that can be converted to a boolean:下表列出了可以转换为布尔值的输入类型:

Input Type输入类型Behavior行为
Array数组Returns true返回true
Binary dataReturns true返回true
Boolean布尔值No-op. Returns the boolean value.否。返回布尔值。
CodeReturns true返回true
DateReturns true返回true
DecimalReturns true if not zero如果不为零,则返回true
Return false if zero如果为零,则返回false
Double双精度浮点数Returns true if not zero如果不为零,则返回true
Return false if zero如果为零,则返回false
Integer整数Returns true if not zero如果不为零,则返回true
Return false if zero如果为零,则返回false
JavaScriptReturns true返回true
Long长整数Returns true if not zero如果不为零,则返回true
Return false if zero如果为零,则返回false
MaxKeyReturns true返回true
MinKeyReturns true返回true
NullReturns null返回null
ObjectReturns true返回true
ObjectIdReturns true返回true
Regular expressionReturns true返回true
String字符串Returns true返回true
TimestampReturns true返回true

To learn more about data types in MongoDB, see BSON Types.要了解有关MongoDB中数据类型的更多信息,请参阅BSON类型

The following table lists some conversion to boolean examples:下表列出了一些转换为布尔值的示例:

Example示例Results结果
{$toBool: false}false
{$toBool: 1.99999}true
{$toBool: Decimal128("5")}true
{$toBool: Decimal128("0")}false
{$toBool: 100}true
{$toBool: ISODate("2018-03-26T04:38:28.044Z")}true
{$toBool: "false"}true
{$toBool: ""}true
{$toBool: null}null

Example示例

Create a collection orders with the following documents:使用以下文档创建集合orders

db.orders.insertMany( [
{ _id: 1, item: "apple", qty: 5, shipped: true },
{ _id: 2, item: "pie", qty: 10, shipped: 0 },
{ _id: 3, item: "ice cream", shipped: 1 },
{ _id: 4, item: "almonds", qty: 2, shipped: "true" },
{ _id: 5, item: "pecans", shipped: "false" }, // Note: All strings convert to true
{ _id: 6, item: "nougat", shipped: "" } // Note: All strings convert to true
] )

The following aggregation operation on the orders collection converts the shipped to a boolean value before finding the unshipped orders:在查找未发货的订单之前,orders集合上的以下聚合操作会将shipped转换为布尔值:

// Define stage to add convertedShippedFlag field with the converted shipped value定义阶段以添加已转换发货标志字段和已转换发货值
// Because all strings convert to true, include specific handling for "false" and ""因为所有字符串都转换为true,所以包括对"false"和""的特定处理

shippedConversionStage = {
$addFields: {
convertedShippedFlag: {
$switch: {
branches: [
{ case: { $eq: [ "$shipped", "false" ] }, then: false } ,
{ case: { $eq: [ "$shipped", "" ] }, then: false }
],
default: { $toBool: "$shipped" }
}
}
}
};

// Define stage to filter documents and pass only the unshipped orders定义阶段以筛选文档并仅传递未发货的订单

unshippedMatchStage = {
$match: { "convertedShippedFlag": false }
};

db.orders.aggregate( [
shippedConversionStage,
unshippedMatchStage
] )

The operation returns the following document:该操作返回以下文档:

{ "_id" : 2, "item" : "pie", "qty" : 10, "shipped" : 0, "convertedShippedFlag" : false }
{ "_id" : 5, "item" : "pecans", "shipped" : "false", "convertedShippedFlag" : false }
{ "_id" : 6, "item" : "nougat", "shipped" : "", "convertedShippedFlag" : false }

Note

If the conversion operation encounters an error, the aggregation operation stops and throws an error. To override this behavior, use $convert instead.如果转换操作遇到错误,聚合操作将停止并抛出错误。要覆盖此行为,请改用$converter