$type 선택한 필드가 지정된 형식인 경우 연산자는 문서를 검색합니다. $type 연산자는 데이터 유효성 검사 및 컬렉션의 문서 간에 일관성을 보장하는 데 유용합니다.
문법
{
<field>: { $type: <BSON type number> | <string alias> }
}
매개 변수
| 매개 변수 | Description |
|---|---|
field |
형식을 확인할 필드입니다. |
BSON type number |
BSON 형식에 해당하는 숫자입니다(예: double의 경우 1, 문자열의 경우 2). |
string alias |
BSON 형식에 대한 문자열 별칭입니다(예: "double", "string", "object", "array"). |
일반적인 BSON 형식
| 유형 | Number | Alias | Description |
|---|---|---|---|
| Double | 1 | "double" | 64비트 부동 소수점 |
| String | 2 | "string" | UTF-8 문자열 |
| Object | 3 | "object" | 포함된 문서 |
| Array | 4 | "array" | Array |
| ObjectId (오브젝트아이디) | 7 | "objectId" | ObjectId (오브젝트아이디) |
| 불리언 (Boolean) | 8 | "bool" | 불리언 (Boolean) |
| 날짜 | 9 | "date" | 날짜 |
| 없음 | 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
}
}
}
]