$type

$type演算子は、選択したフィールドが指定した型の場合にドキュメントを取得します。 $type演算子は、データの検証と、コレクション内のドキュメント間の一貫性の確保に役立ちます。

構文

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

パラメーター

パラメーター Description
field 型を確認するフィールド。
BSON type number BSON 型に対応する数値 (例: double の場合は 1、string の場合は 2)。
string alias BSON 型の文字列エイリアス (例: "double"、"string"、"object"、"array")。

一般的な BSON 型

タイプ Number エイリアス Description
Double 1 "double" 64 ビット浮動小数点
2 "string" UTF-8 文字列
Object 3 "object" 埋め込みドキュメント
Array 4 "array" Array
オブジェクト識別子 7 "objectId" オブジェクト識別子
ブール値 "bool" ブール値
日付 9 "date" 日付
Null 10 "null" Null 値
32 ビット整数 16 "int" 32 ビット整数
タイムスタンプ 17 "timestamp" タイムスタンプ
64 ビット整数 18 "long" 64 ビット整数

例示

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: 文字列型の名前を持つ店舗を検索する

名前が文字列型のストアを検索するには、name フィールドで $type 演算子を使用してクエリを実行します。 次に、ID フィールドと名前フィールドのみを投影し、結果セットから 1 つのドキュメントに結果を制限します。

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
            }
        }
    }
]