$type

Belirtilen türde olan bir alan seçildiğinde $type operatörü belgeleri alır. $type işleci, veri doğrulama ve koleksiyondaki belgeler arasında tutarlılık sağlama konusunda yararlıdır.

Sözdizimi

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

Parametreler

Parametre Description
field Türü denetlenecek alan.
BSON type number BSON türüne karşılık gelen bir sayı (örneğin, çift için 1, dize için 2).
string alias BSON türü için bir dize diğer adı (örneğin, "double", "string", "object", "array").

Yaygın BSON Türleri

Türü Sayı Diğer ad Description
Double 1 "double" 64 bit kayan nokta
String 2 "string" UTF-8 dizesi
Nesne 3 "object" Eklenmiş belge
Array 4 "array" Array
Nesne Kimliği 7 "objectId" Nesne Kimliği
Boolean 8 "bool" Boolean
Date 9 "date" Date
Null 10 "null" Null değer
32 bit tamsayı 16 "int" 32 bit tamsayı
Zaman Damgası 17 "timestamp" Zaman Damgası
64 bit tamsayı 18 "long" 64 bit tamsayı

Örnekler

Stores koleksiyonundaki bu örnek belgeyi göz önünde bulundurun.

{
    "_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
                }
            ]
        }
    ]
}

Örnek 1: Dize türündeki adlara sahip depoları bulma

Adı dize türünde olan herhangi bir depoyu bulmak için ad alanındaki $type işlecini kullanarak bir sorgu çalıştırın. Ardından, yalnızca kimlik ve ad alanlarını yansıtın ve sonuçları sonuç kümesinden bir belgeyle sınırlayın.

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

Bu sorgu aşağıdaki sonucu döndürür:

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

Örnek 2: Birden çok tür denetimi kullanarak veri doğrulama

Bu sorgu, koleksiyonun belge yapısındaki temel alanların istenen veri türlerine sahip olduğunu doğrulamayı gösterir.

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

Bu sorgu aşağıdaki sonucu döndürür.

[
    {
        "_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
            }
        }
    }
]