정의
호환성
다음 환경에서 호스팅되는 배포에 $type 사용할 수 있습니다.
MongoDB Atlas: 클라우드에서의 MongoDB 배포를 위한 완전 관리형 서비스
MongoDB Enterprise: MongoDB의 구독 기반 자체 관리 버전
MongoDB Community: MongoDB의 소스 사용 가능 무료 자체 관리 버전
구문
단일 BSON 유형에 대한 $type 표현식의 구문은 다음과 같습니다.
{ field: { $type: <BSON type> } }
BSON types번호 또는 별칭을 지정할 수 있습니다.
$type 표현식은 BSON 유형의 배열을 받아들일 수도 있으며 구문은 다음과 같습니다.
{ field: { $type: [ <BSON type1> , <BSON type2>, ... ] } }
위의 쿼리는 field 값이 나열된 유형 중 하나에 해당하는 문서와 일치합니다. 배열에 지정된 유형은 숫자 또는 문자열 별칭일 수 있습니다.
For an example, see Querying by Multiple Data Types.
사용 가능한 유형에서는 BSON types와 해당 숫자 및 문자열 별칭을 설명합니다.
팁
$isNumber- 인수가 숫자인지 확인합니다.$type (Aggregation)- 인수의 BSON types를 반환합니다.
행동
$type은 field의 BSON 유형이 $type에 전달된 BSON 유형과 일치하는 문서를 반환합니다.
배열
field가 배열인 문서의 경우 $type은 하나 이상의 배열 요소가 $type에 전달된 유형과 일치하는 문서를 반환합니다.
$type: "array" 에 대한 쿼리는 필드 자체가 배열인 문서를 반환합니다.
사용 가능한 유형
The $type 연산자는 BSON 유형에 해당하는 숫자 외에도 BSON 유형에 대한 string 별칭을 허용합니다.
$type(은)는 다음 BSON 유형과 일치하는 number 별칭을 지원합니다.
예를 보려면 예시를 참조하세요.
MinKey 및 MaxKey
MinKey and MaxKey are used in comparison operations and exist primarily for internal use. For all possible BSON element values, MinKey is always the smallest value, and MaxKey is always the greatest value.
$type을 사용하여 minKey 또는 maxKey를 쿼리하면 특수 MinKey 또는 MaxKey 값과 일치하는 필드만 반환됩니다.
For example, the following data collection has these documents with MinKey and MaxKey:
db.data.insertMany( [ { _id : 1, x : { "$minKey" : 1 } }, { _id : 2, y : { "$maxKey" : 1 } } ] )
다음 쿼리는 _id: 1이 포함된 문서를 반환합니다.
db.data.find( { x: { $type: "minKey" } } )
다음 쿼리는 _id: 2이 포함된 문서를 반환합니다.
db.data.find( { y: { $type: "maxKey" } } )
예시
데이터 유형별 쿼리
The addressBook contains addresses and ZIP codes, where zipCode has string, int, double, and long values:
db.addressBook.insertMany( [ { _id : 1, address : "2030 Martian Way", zipCode : "90698345" }, { _id : 2, address : "156 Lunar Place", zipCode : 43339374 }, { _id : 3, address : "2324 Pluto Place", zipCode : Long(3921412) }, { _id : 4, address : "55 Saturn Ring" , zipCode : Int32(88602117) }, { _id : 5, address : "104 Venus Drive", zipCode : ["834847278", "1893289032"] } ] )
The following queries return documents where zipCode is the BSON type string or is an array with an element of the specified type:
db.addressBook.find( { zipCode : { $type : 2 } } ); db.addressBook.find( { zipCode : { $type : "string" } } );
The queries return:
{ _id : 1, address : "2030 Martian Way", zipCode : "90698345" } { _id : 5, address : "104 Venus Drive", zipCode : [ "834847278", "1893289032" ] }
The following queries return documents where zipCode is the BSON type double or is an array with an element of the specified type:
db.addressBook.find( { zipCode : { $type : 1 } } ); db.addressBook.find( { zipCode : { $type : "double" } } );
이러한 쿼리가 반환됩니다.
{ _id : 2, address : "156 Lunar Place", zipCode : 43339374 }
The following query uses the number alias to return documents where zipCode is the BSON type double, int, or long or is an array with an element of the specified type:
db.addressBook.find( { zipCode : { $type : "number" } } )
이러한 쿼리가 반환됩니다.
{ _id : 2, address : "156 Lunar Place", zipCode : 43339374 } { _id : 3, address : "2324 Pluto Place", zipCode : Long(3921412) } { _id : 4, address : "55 Saturn Ring", zipCode : 88602117 }
여러 데이터 유형으로 쿼리하기
grades collection에는 이름과 평균이 포함되어 있습니다. 이 때 classAverage 에는 string, int 및 double 값이 있습니다.
db.grades.insertMany( [ { _id : 1, name : "Alice King" , classAverage : 87.333333333333333 }, { _id : 2, name : "Bob Jenkins", classAverage : "83.52" }, { _id : 3, name : "Cathy Hart", classAverage: "94.06" }, { _id : 4, name : "Drew Williams" , classAverage : Int32("93") } ] )
The following queries return documents where classAverage is the BSON type string or double or is an array with an element of the specified type. The first query uses numeric aliases and the second query uses string aliases.
db.grades.find( { classAverage : { $type : [ 2 , 1 ] } } ); db.grades.find( { classAverage : { $type : [ "string" , "double" ] } } );
이러한 쿼리가 반환됩니다.
{ _id : 1, name : "Alice King", classAverage : 87.33333333333333 } { _id : 2, name : "Bob Jenkins", classAverage : "83.52" } { _id : 3, name : "Cathy Hart", classAverage : "94.06" }
MinKey 및 MaxKey로 쿼리하기
restaurants collection은 낙제 등급인 모든 성적에 대해 minKey 을(를) 사용합니다.
db.restaurants.insertOne( [ { _id: 1, address: { building: "230", coord: [ -73.996089, 40.675018 ], street: "Huntington St", zipcode: "11231" }, borough: "Brooklyn", cuisine: "Bakery", grades: [ { date : new Date(1393804800000), grade : "C", score : 15 }, { date : new Date(1378857600000), grade : "C", score : 16 }, { date : new Date(1358985600000), grade : MinKey(), score : 30 }, { date : new Date(1322006400000), grade : "C", score : 15 } ], name : "Dirty Dan's Donuts", restaurant_id : "30075445" } ] )
그리고 가장 높은 합격 점수인 모든 학년에 대한 maxKey은(는) 다음과 같습니다.
db.restaurants.insertOne( [ { _id : 2, address : { building : "1166", coord : [ -73.955184, 40.738589 ], street : "Manhattan Ave", zipcode : "11222" }, borough: "Brooklyn", cuisine: "Bakery", grades: [ { date : new Date(1393804800000), grade : MaxKey(), score : 2 }, { date : new Date(1378857600000), grade : "B", score : 6 }, { date : new Date(1358985600000), grade : MaxKey(), score : 3 }, { date : new Date(1322006400000), grade : "B", score : 5 } ], name : "Dainty Daisey's Donuts", restaurant_id : "30075449" } ] )
다음 쿼리는 grades.grade 필드에 minKey 이(가) 포함되거나 지정된 유형의 요소가 포함된 배열인 레스토랑을 반환합니다.
db.restaurants.find( { "grades.grade" : { $type : "minKey" } } )
다음과 같은 결과를 반환합니다.
{ _id : 1, address : { building : "230", coord : [ -73.996089, 40.675018 ], street : "Huntington St", zipcode : "11231" }, borough : "Brooklyn", cuisine : "Bakery", grades : [ { date : ISODate("2014-03-03T00:00:00Z"), grade : "C", score : 15 }, { date : ISODate("2013-09-11T00:00:00Z"), grade : "C", score : 16 }, { date : ISODate("2013-01-24T00:00:00Z"), grade : { "$minKey" : 1 }, score : 30 }, { date : ISODate("2011-11-23T00:00:00Z"), grade : "C", score : 15 } ], name : "Dirty Dan's Donuts", restaurant_id : "30075445" }
다음 쿼리는 grades.grade 필드에 maxKey 이(가) 포함되거나 지정된 유형의 요소가 포함된 배열인 레스토랑을 반환합니다.
db.restaurants.find( { "grades.grade" : { $type : "maxKey" } } )
다음과 같은 결과를 반환합니다.
{ _id : 2, address : { building : "1166", coord : [ -73.955184, 40.738589 ], street : "Manhattan Ave", zipcode : "11222" }, borough : "Brooklyn", cuisine : "Bakery", grades : [ { date : ISODate("2014-03-03T00:00:00Z"), grade : { "$maxKey" : 1 }, score : 2 }, { date : ISODate("2013-09-11T00:00:00Z"), grade : "B", score : 6 }, { date : ISODate("2013-01-24T00:00:00Z"), grade : { "$maxKey" : 1 }, score : 3 }, { date : ISODate("2011-11-23T00:00:00Z"), grade : "B", score : 5 } ], name : "Dainty Daisey's Donuts", restaurant_id : "30075449" }
배열 유형별 쿼리
sensorReading라는 이름의 컬렉션에 다음 문서가 포함되어 있습니다.
db.sensorReading.insertMany( [ { _id : 1, readings : [ 25, 23, [ "Warn: High Temp!", 55 ], [ "ERROR: SYSTEM SHUTDOWN!", 66 ] ] }, { _id : 2, readings : [ 25, 25, 24, 23 ] }, { _id : 3, readings : [ 22, 24, [] ] }, { _id : 4, readings : [] }, { _id : 5, readings : 24 } ] )
The following query returns any document where the readings field is an array:
db.sensorReading.find( { readings : { $type: "array" } } )
이 쿼리는 다음 문서를 반환합니다.
{ _id : 1, readings : [ 25, 23, [ "Warn: High Temp!", 55 ], [ "ERROR: SYSTEM SHUTDOWN!", 66 ] ] }, { _id : 2, readings : [ 25, 25, 24, 23 ] }, { _id : 3, readings : [ 22, 24, [] ] }, { _id : 4, readings : [] }
_id : 1, _id : 2, _id : 3 및 _id : 4 가 있는 문서에서 readings 필드는 배열입니다.