Docs Menu
Docs Home
/ /

$type (쿼리 자 연산자)

$type

$type값이 지정된 field BSON types의 인스턴스 인 문서를 선택합니다. 데이터 유형 쿼리는 데이터 유형을 예측할 수 없는 구조화되지 않은 데이터에 유용합니다.

다음 환경에서 호스팅되는 배포에 $type 사용할 수 있습니다.

  • MongoDB Atlas: 클라우드에서의 MongoDB 배포를 위한 완전 관리형 서비스

단일 BSON 유형에 대한 $type 표현식의 구문은 다음과 같습니다.

{ field: { $type: <BSON type> } }

BSON types번호 또는 별칭을 지정할 수 있습니다.

$type 표현식은 BSON 유형의 배열을 받아들일 수도 있으며 구문은 다음과 같습니다.

{ field: { $type: [ <BSON type1> , <BSON type2>, ... ] } }

위의 쿼리는 field 값이 나열된 유형 중 하나에 해당하는 문서와 일치합니다. 배열에 지정된 유형은 숫자 또는 문자열 별칭일 수 있습니다.

예시 는 여러 데이터 유형으로 쿼리하기를 참조하세요.

사용 가능한 유형에서는 BSON types와 해당 숫자 및 문자열 별칭을 설명합니다.

$typefield의 BSON 유형이 $type에 전달된 BSON 유형과 일치하는 문서를 반환합니다.

field가 배열인 문서의 경우 $type은 하나 이상의 배열 요소가 $type에 전달된 유형과 일치하는 문서를 반환합니다.

$type: "array" 에 대한 쿼리는 필드 자체가 배열인 문서를 반환합니다.

The $type 연산자는 BSON 유형에 해당하는 숫자 외에도 BSON 유형에 대한 string 별칭을 허용합니다.

$type(은)는 다음 BSON 유형과 일치하는 number 별칭을 지원합니다.

예를 보려면 예시를 참조하세요.

MinKeyMaxKey 는 비교 작업에 사용되며 주로 내부용으로 존재합니다. 모든 가능한 BSON 요소 값에 대해 MinKey 은 항상 가장 작은 값이고 MaxKey 는 항상 가장 큰 값입니다.

$type을 사용하여 minKey 또는 maxKey를 쿼리하면 특수 MinKey 또는 MaxKey 값과 일치하는 필드만 반환됩니다.

예시 들어 다음 data 컬렉션 MinKeyMaxKey가 포함된 문서가 있습니다.

db.data.insertMany( [
{ _id: 1, x: MinKey() },
{ _id: 2, y: MaxKey() }
] )

다음 쿼리는 _id: 1이 포함된 문서를 반환합니다.

db.data.find( { x: { $type: "minKey" } } )
[
{
_id: 1,
x: MinKey()
}
]

다음 쿼리는 _id: 2이 포함된 문서를 반환합니다.

db.data.find( { y: { $type: "maxKey" } } )
[
{
_id: 2,
y: MaxKey()
}
]

이 페이지의 예시에서는 sample_mflix 샘플 데이터 세트의 데이터를 사용합니다. 이 데이터 세트를 자체 관리형 MongoDB 배포서버에 로드하는 방법에 대한 자세한 내용은 샘플 데이터 세트 로드를 참조하세요. 샘플 데이터베이스를 수정한 경우 이 페이지의 예시를 실행 하려면 데이터베이스를 제거하고 다시 만들어야 할 수 있습니다.

movies 컬렉션 imdb.rating 필드 에 IMDb 평점을 저장합니다. 대부분의 문서는 imdb.ratingdouble로 저장 하지만 일부 문서는 빈 string ("")로 저장 .

다음 쿼리는 2013 에서 문서를 반환하며, 여기서 imdb.ratingBSON 유형 string입니다.

이 쿼리 BSON 유형의 번호로 유형을 지정합니다.

db.movies.find(
{ "imdb.rating": { $type: 2 }, year: 2013 },
{ _id: 0, title: 1, year: 1, "imdb.rating": 1 }
)
[
{ title: 'Coming to Terms', year: 2013, imdb: { rating: '' } },
{ title: 'Absent Minded', year: 2013, imdb: { rating: '' } }
]

이 쿼리 BSON type의 별칭을 사용하여 유형을 지정합니다.

db.movies.find(
{ "imdb.rating": { $type: "string" }, year: 2013 },
{ _id: 0, title: 1, year: 1, "imdb.rating": 1 }
)
[
{ title: 'Coming to Terms', year: 2013, imdb: { rating: '' } },
{ title: 'Absent Minded', year: 2013, imdb: { rating: '' } }
]

다음 쿼리 number 별칭을 사용하여 imdb.ratingBSON 유형 double, int 또는 long인 문서를 반환합니다.

db.movies.find(
{ "imdb.rating": { $type: "number" }, runtime: { $gt: 1000 } },
{ _id: 0, title: 1, runtime: 1, "imdb.rating": 1 }
)
[
{ runtime: 1256, title: 'Centennial', imdb: { rating: 8.5 } },
{ runtime: 1140, title: 'Baseball', imdb: { rating: 9.1 } }
]

다음 쿼리는 imdb.ratingBSON 유형 string 또는 double인 문서를 반환합니다. 첫 번째 쿼리 숫자 별칭을 사용하고 두 번째 쿼리 string 별칭을 사용합니다.

db.movies.find(
{ "imdb.rating": { $type: [ 2, 1 ] },
runtime: { $gt: 1000 } },
{ _id: 0, title: 1, runtime: 1, "imdb.rating": 1 }
)
[
{ runtime: 1256, title: 'Centennial', imdb: { rating: 8.5 } },
{ runtime: 1140, title: 'Baseball', imdb: { rating: 9.1 } }
]
db.movies.find(
{ "imdb.rating": { $type: [ "string", "double" ] },
runtime: { $gt: 1000 } },
{ _id: 0, title: 1, runtime: 1, "imdb.rating": 1 }
)
[
{ runtime: 1256, title: 'Centennial', imdb: { rating: 8.5 } },
{ runtime: 1140, title: 'Baseball', imdb: { rating: 9.1 } }
]

movies 컬렉션 genres 필드 에 영화 장르를 배열 로 저장합니다. 다음 쿼리 genres 필드 가 배열 문서를 반환합니다.

db.movies.find(
{ genres: { $type: "array" }, runtime: { $gt: 1000 } },
{ _id: 0, title: 1, genres: 1 }
)
[
{
title: 'Centennial',
genres: [ 'Action', 'Adventure', 'Drama' ]
},
{
title: 'Baseball',
genres: [ 'Documentary', 'History', 'Sport' ]
}
]

다음 예제에서는 낙제 등급인 모든 성적에 대해 minKey 을(를) 사용하는 restaurants 컬렉션 사용합니다.

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 : "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:00.000Z'),
grade: 'C',
score: 15
},
{
date: ISODate('2013-09-11T00:00:00.000Z'),
grade: 'C',
score: 16
},
{
date: ISODate('2013-01-24T00:00:00.000Z'),
grade: MinKey(),
score: 30
},
{
date: ISODate('2011-11-23T00:00:00.000Z'),
grade: 'C',
score: 15
}
],
name: ...,
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:00.000Z'),
grade: MaxKey(),
score: 2
},
{
date: ISODate('2013-09-11T00:00:00.000Z'),
grade: 'B',
score: 6
},
{
date: ISODate('2013-01-24T00:00:00.000Z'),
grade: MaxKey(),
score: 3
},
{
date: ISODate('2011-11-23T00:00:00.000Z'),
grade: 'B',
score: 5
}
],
name: ...,
restaurant_id: '30075449'
}
]

돌아가기

$exists

이 페이지의 내용