Definition定义
New in version 8.0.在版本8.0中新增。
$toUUIDConverts a string value to a UUID. If the input value is not a string,将字符串值转换为UUID。如果输入值不是字符串,则$toUUIDerrors.$toUUID错误。
Syntax语法
{
$toUUID: <expression>
}
The $toUUID expression is shorthand for the following $convert expression:$toUUID表达式是以下$convert表达式的简写:
{
$convert: {
input: <expression>,
to: {
type: "binData",
subtype: 4 // UUID
},
format: "uuid"
}
}Example示例
Create a 使用以下文档创建products collection with the following document:products集合:
db.products.insertOne(
{
name: "laptop",
price: 400,
UUID: "0e3b9063-8abd-4eb3-9f9f-f4c59fd30a60"
}
)
In the example document, the 在示例文档中,UUID field is a string. To convert the UUID field to a UUID value, run the following $toUUID operation:UUID字段是一个字符串。要将UUID字段转换为UUID值,请运行以下$toUUID操作:
db.products.aggregate( [
{
$project: {
name: 1,
price: 1,
UUID: {
$toUUID: "$UUID"
}
}
}
] )
Output:输出:
[
{
_id: ObjectId('669945ab610b080391a8e2f5'),
name: 'laptop',
price: 400,
UUID: UUID('0e3b9063-8abd-4eb3-9f9f-f4c59fd30a60')
}
]