Docs Menu
Docs Home
/ /

$type(クエリ述語演算子)

$type

$type は、fieldが指定された BSON type のインスタンスであるドキュメントを選択します。データ型によるクエリは、データ型が予測できない非構造化データに便利です。

次の環境でホストされる配置には $type を使用できます。

  • MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです

  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

単一の BSON type の $type 式の構文は以下のとおりです。

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

BSON type の番号または エイリアスのいずれかを指定できます。

$type式では BSON type の配列も受け入れることが可能です。構文は以下のとおりです。

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

上記のクエリは、field の値が列挙された型のいずれかであるドキュメントに一致します。配列で指定される型は、数値または文字列のエイリアスのいずれかになります。

例については、複数のデータ型によるクエリを参照してください。

利用可能なタイプ 」では、BSON types とそれに対応する数値および string のエイリアスについて説明します。

Tip

$typeは、 fieldの BSON 型が$typeに渡された BSON 型と一致するドキュメントを返します。

fieldが配列であるドキュメントの場合、 $typeは少なくとも 1 つの配列要素が$typeに渡された型と一致するドキュメントを返します。

$type: "array" のクエリでは、フィールド自体が配列であるドキュメントが返されます。

$type 演算子は、BSON types に対応する数値に加えて、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.ratingBSONstring であるドキュメントが返されます。

このクエリは、BSON type の番号とともに タイプを指定します。

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型のエイリアスを持つ 型を指定します。

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型の doubleint、または 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 であるドキュメントが返されます。最初のクエリでは数値エイリアスが使用され、2 番目のクエリでは 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

項目一覧