Learn the "why" behind slow queries and how to fix them in our 2-Part Webinar.
Register now >
Docs Menu
Docs Home
/ /

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

$type

$type selects documents where the value of the field is an instance of the specified BSON type(s). Querying by data type is useful for unstructured data where data types aren't predictable.

次の環境でホストされる配置には $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 の値が列挙された型のいずれかであるドキュメントに一致します。配列で指定される型は、数値または文字列のエイリアスのいずれかになります。

For an example, see Querying by Multiple Data Types.

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

Tip

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

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

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

$type 演算子は、BSON types に対応する数値に加えて、string エイリアスも受け入れます。

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

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

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 コレクションには名前と平均点が含まれ、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 : 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" }

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

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

戻る

$exists

項目一覧