MongoDB $typeオペレーター

MongoDB では、$typeオペレーターは、指定した型のフィールドを持つドキュメントをクエリするために使用されます。

MongoDB の $type オペレーターは、フィールドの BSON データ型をクエリするために使用されます。

これにより、1 つまたは複数の型を指定し、それらの型に一致するドキュメントを返すことができます。

以下に、$type オペレーターの詳細な説明と例を示します。

構文:

db.collection.find({ field: { $type: <type> } })
  • field:型を確認するフィールド。
  • type:指定する BSON 型。型の数値コードまたは型名の文字列を指定できます。

BSON タイプ

以下は、一般的な BSON 型と、対応する数値コードおよび文字列名です:

型コード型名
1double
2string
3object
4array
5binData
6undefined
7objectId
8bool
9date
10null
11regex
12dbPointer
13javascript
14symbol
15javascriptWithScope
16int
17timestamp
18long
19decimal
255minKey
127maxKey

例

フィールドの型が文字列であるドキュメントを検索する:

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 フィールドの型が倍精度浮動小数点数であるドキュメントを検索する:

例

db.myCollection.find({
    $and: [
        { details: { $type: "object" } },
        { score: { $type: "double" } }
    ]
})

または型コードを使用:

例

db.myCollection.find({
    $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 }
その他の拡張