MongoDB $typeオペレーター
MongoDB では、$typeオペレーターは、指定した型のフィールドを持つドキュメントをクエリするために使用されます。
MongoDB の $type オペレーターは、フィールドの BSON データ型をクエリするために使用されます。
これにより、1 つまたは複数の型を指定し、それらの型に一致するドキュメントを返すことができます。
以下に、$type オペレーターの詳細な説明と例を示します。構文:
db.collection.find({ field: { $type: <type> } })
- field:型を確認するフィールド。
- type:指定する BSON 型。型の数値コードまたは型名の文字列を指定できます。
BSON タイプ
以下は、一般的な BSON 型と、対応する数値コードおよび文字列名です:
| 型コード | 型名 |
|---|---|
| 1 | double |
| 2 | string |
| 3 | object |
| 4 | array |
| 5 | binData |
| 6 | undefined |
| 7 | objectId |
| 8 | bool |
| 9 | date |
| 10 | null |
| 11 | regex |
| 12 | dbPointer |
| 13 | javascript |
| 14 | symbol |
| 15 | javascriptWithScope |
| 16 | int |
| 17 | timestamp |
| 18 | long |
| 19 | decimal |
| 255 | minKey |
| 127 | maxKey |
例
フィールドの型が文字列であるドキュメントを検索する:
db.myCollection.find({ fieldName: { $type: "string" } })
または型コードを使用:
db.myCollection.find({ fieldName: { $type: 2 } })
フィールドの型が数値であるドキュメントを検索する。例:age フィールドの型が整数であるドキュメントを検索する:
db.myCollection.find({ age: { $type: "int" } })
または型コードを使用:
db.myCollection.find({ age: { $type: 16 } })
フィールドの型がブール値であるドキュメントを検索する:
db.myCollection.find({ isActive: { $type: "bool" } })
または型コードを使用:
db.myCollection.find({ isActive: { $type: 8 } })
フィールドの型が日付であるドキュメントを検索する:
db.myCollection.find({ createdAt: { $type: "date" } })
または型コードを使用:
db.myCollection.find({ createdAt: { $type: 9 } })
フィールドの型が複数の型のいずれかであるドキュメントを検索する。例:value フィールドの型が文字列または整数であるドキュメントを検索する:
db.myCollection.find({ value: { $type: ["string", "int"] } })
または型コードを使用:
db.myCollection.find({ value: { $type: [2, 16] } })
details フィールドの型がオブジェクトであり、score フィールドの型が倍精度浮動小数点数であるドキュメントを検索する:
例
$and: [
{ details: { $type: "object" } },
{ score: { $type: "double" } }
]
})
または型コードを使用:
例
$and: [
{ details: { $type: 3 } },
{ score: { $type: 1 } }
]
})
$type オペレーターを使用すると、ドキュメント内の特定のフィールドのデータ型を正確にクエリでき、より細かい粒度のデータフィルタリングと管理が可能になります。
その他の例
使用するデータベース名は "サンプル"、コレクション名は "col" です。以下は挿入したデータです。
シンプルなコレクション "col":
>db.col.insert({
title: 'PHP 教程',
description: 'PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。',
by: 'サンプル',
url: 'http://www.example.com',
tags: ['php'],
likes: 200
})
>db.col.insert({title: 'Java 教程',
description: 'Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。',
by: 'サンプル',
url: 'http://www.example.com',
tags: ['java'],
likes: 150
})
>db.col.insert({title: 'MongoDB 教程',
description: 'MongoDB 是一个 Nosql 数据库',
by: 'サンプル',
url: 'http://www.example.com',
tags: ['mongodb'],
likes: 100
})
find() コマンドを使用してデータを確認する:
> db.col.find()
{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "サンプル", "url" : "http://www.example.com", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "サンプル", "url" : "http://www.example.com", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "サンプル", "url" : "http://www.example.com", "tags" : [ "mongodb" ], "likes" : 100 }
MongoDB オペレーター - $type の例
"col" コレクション内の title が String であるデータを取得するには、次のコマンドを使用できます:
db.col.find({"title" : {$type : 2}})
或
db.col.find({"title" : {$type : 'string'}})
出力結果は次のとおりです:
{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "サンプル", "url" : "http://www.example.com", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "サンプル", "url" : "http://www.example.com", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "サンプル", "url" : "http://www.example.com", "tags" : [ "mongodb" ], "likes" : 100 }
その他の拡張