你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

$type

适用对象: MongoDB vCore

运算符 $type 返回表达式中指定的值的类型。

语法

运算符的 $type 语法为:

{ "$type": <expression> }

参数

参数 DESCRIPTION
expression 要检索其类型的指定值

例子

请查看 StoreData 数据库中 stores 集合的这个示例文档。

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

示例 1:获取 Double 值的类型

db.stores.aggregate([
{
    "$match": {
        "_id": "b0107631-9370-4acd-aafa-8ac3511e623d"
    }
},
{
    "$project": {
        "longitude": "$location.lon",
        "longitudeType": {
            "$type": "$location.lon"
        }
    }
}])

此查询返回以下结果:

{
    "_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
    "longitude": 72.8377,
    "longitudeType": "double"
}

示例 2:获取字符串值的类型

db.stores.aggregate([
{
    "$match": {
        "_id": "b0107631-9370-4acd-aafa-8ac3511e623d"
    }
},
{
    "$project": {
        "name": "$name",
        "nameType": {
            "$type": "$name"
        }
    }
}])

此查询返回以下结果:

{
    "_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
    "name": "Wide World Importers | Furniture Bargains - Roobport",
    "nameType": "string"
}

示例 3:获取 Int 值的类型

db.stores.aggregate([
{
    "$match": {
        "_id": "b0107631-9370-4acd-aafa-8ac3511e623d"
    }
},
{
    "$project": {
        "fullTimeStaff": "$staff.totalStaff.fullTime",
        "fullTimeStaffType": {
            "$type": "$staff.totalStaff.fullTime"
        }
    }
}])

此查询返回以下结果:

{
    "_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
    "fullTimeStaff": 3,
    "fullTimeStaffType": "int"
}