Docs Menu
Docs Home
/
MongoDB マニュアル
/ / / /

$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 に対応する数値に加えて、文字列エイリアスも受け入れます。[1]

タイプ
番号
エイリアス
ノート
Double
1
"double"
文字列
2
"string"
オブジェクト
3
"object"
配列
4
"array"
バイナリ データ
5
"binData"
未定義
6
"undefined"
非推奨。
ObjectId
7
"objectId"
ブール値
8
"bool"
日付
9
"date"
null
10
"null"
正規表現
11
"regex"
DBポインタ
12
"dbPointer"
非推奨。
JavaScript
13
"javascript"
シンボル
14
"symbol"
非推奨。
32 ビット整数
16
"int"
タイムスタンプ
17
"timestamp"
64 ビット整数
18
"long"
Decimal128
19
"decimal"
Min key
-1
"minKey"
Max key
127
"maxKey"

$typeは、次のBSONタイプに一致するnumberエイリアスをサポートしています。

例については、「例 」を参照してください。

[1] クエリフィルター $type: 0$exists:false の同義語として使用できなくなりました。null または欠落しているフィールドをクエリするには、「null または欠落しているフィールドのクエリ」を参照してください。

Tip

以下も参照してください。

MinKeyMaxKeyは比較操作で使用され、主に内部使用のために存在します。 すべての可能なBSON要素値に関しては、 MinKeyは常に最小値であり、 MaxKeyは常に最大値です。

$typeを使用してminKeyまたはmaxKeyをクエリすると、 MinKeyまたはMaxKeyの特別な値と一致するフィールドのみが返されます。

data コレクションに MinKeyMaxKey を持つ 2 つのドキュメントがあるとします。

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

addressBookには住所と郵便番号が含まれ、zipCode には stringintdoublelong の値があります。

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 : NumberLong(3921412) },
{ _id : 4, address : "55 Saturn Ring" , zipCode : NumberInt(88602117) },
{ _id : 5, address : "104 Venus Drive", zipCode : ["834847278", "1893289032"] }
] )

次のクエリでは、zipCodeBSON type の string である、または指定された型の要素を含む配列であるドキュメントがすべて返されます。

db.addressBook.find( { zipCode : { $type : 2 } } );
db.addressBook.find( { zipCode : { $type : "string" } } );

これらのクエリでは以下が返されます。

{ _id : 1, address : "2030 Martian Way", zipCode : "90698345" }
{ _id : 5, address : "104 Venus Drive", zipCode : [ "834847278", "1893289032" ] }

次のクエリでは、zipCodeBSON type の double である、または指定された型の要素を含む配列であるドキュメントがすべて返されます。

db.addressBook.find( { zipCode : { $type : 1 } } );
db.addressBook.find( { zipCode : { $type : "double" } } );

これらのクエリでは以下が返されます。

{ _id : 2, address : "156 Lunar Place", zipCode : 43339374 }

次のクエリでは、number エイリアスを使用して、zipCodeBSON type の doubleintlong のいずれかである、または指定された型の要素を含む配列であるドキュメントが返されます。

db.addressBook.find( { zipCode : { $type : "number" } } )

これらのクエリでは以下が返されます。

{ _id : 2, address : "156 Lunar Place", zipCode : 43339374 }
{ _id : 3, address : "2324 Pluto Place", zipCode : NumberLong(3921412) }
{ _id : 4, address : "55 Saturn Ring", zipCode : 88602117 }

grades コレクションには名前と平均点が含まれ、classAverage には stringintdouble の値があります。

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 : NumberInt("93") }
] )

次のクエリでは、classAverageBSON type の string または double である、または指定された型の要素を含む配列であるドキュメントがすべて返されます。最初のクエリでは数値エイリアスが使用され、2 番目のクエリでは文字列エイリアスが使用されます。

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

restaurants コレクションでは、不合格の評点には 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 }
] )

次のクエリでは、readings フィールドが配列(空でも空でなくても) のドキュメントがすべて返されます。

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 フィールドは配列です。

戻る

$exists