$type

如果所選取字位為指定的類型,運算子 $type 會擷取檔。 $type運算子對於數據驗證和確保集合中檔之間的一致性很有用。

語法

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

參數

參數 Description
field 要檢查類型的欄位。
BSON type number 對應至 BSON 類型的數字 (例如:1 代表雙精度浮點數,字串則為 2)。
string alias BSON 類型的字串別名 (例如:“double”、“string”、“object”、“array”)。

常見的 BSON 類型

類型 Number Alias Description
Double 1 "double" 64 位元浮點數
繩子 2 "string" UTF-8 字串
物體 3 "object" 內嵌檔
Array 4 "array" Array
ObjectId (物件識別碼) 7 "objectId" ObjectId (物件識別碼)
布林值 8 "bool" 布林值
Date 9 "date" Date
Null 10 "null" 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
            }
        }
    }
]