$type

如果所选字段的类型为指定类型,则 $type 运算符将检索文档。 $type 运算符可用于数据验证,并确保集合中所有文档的一致性。

Syntax

{
  <field>: { $type: <BSON type number> | <string alias> }
}

参数

参数 Description
field 要检查其类型的字段。
BSON type number 对应于 BSON 类型的数字(例如,1 表示双精度,2 表示字符串)。
string alias BSON 类型的字符串别名(例如“double”、“string”、“object”、“array”)。

常见 BSON 类型

类型 编号 别名 Description
Double 1 "double" 64 位浮点
String 2 "string" UTF-8 字符串
物体 3 "object" 嵌入的文档
Array 4 "array" Array
对象标识符 (ObjectId) 7 "objectId" 对象标识符 (ObjectId)
布尔 8 "bool" 布尔
日期 9 "date" 日期
Null 10 "null" 空值
32位整数 16 "int" 32位整数
时间戳 17 "timestamp" 时间戳
64 位整数 18 "long" 64 位整数

例子

请考虑商店集合中的这个示例文档。

{
    "_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:查找具有字符串类型名称的存储

若要查找名称为字符串类型的任何存储,请使用名称字段上的 $type 运算符运行查询。 然后,只提取 ID 和名称字段,并将结果集限制为一个文档。

db.stores.find({
    "name": {
        $type: "string"
    }
}, {
    "_id": 1,
    "name": 1
}).limit(1)

此查询返回以下结果:

[
    {
        "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
        "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort"
    }
]

示例 2:使用多个类型检查进行数据验证

此查询演示如何验证集合的文档结构中的基本字段是否具有所需的数据类型。

db.stores.find({
      "_id": "905d1939-e03a-413e-a9c4-221f74055aac",
      "name": {
          $type: "string"
      },
      "location": {
          $type: "object"
      },
      "staff.employeeCount.fullTime": {
          $type: ["int", "long"]
      },
      "promotionEvents": {
          $type: "array"
      }
  }, {
      "_id": 1,
      "name": 1,
      "location": 1,
      "staff": 1
  }
)

此查询返回以下结果。

[
    {
        "_id": "905d1939-e03a-413e-a9c4-221f74055aac",
        "name": "Trey Research | Home Office Depot - Lake Freeda",
        "location": {
            "lat": -48.9752,
            "lon": -141.6816
        },
        "staff": {
            "employeeCount": {
                "fullTime": 12
            }
        }
    }
]