MongoDB $type Operator
In MongoDB, the $type operator is used to query documents that have fields of a specified type.
MongoDB's $type operator is used to query the BSON data type of a field.
It allows you to specify one or more types and return documents that match those types.
The following is a detailed introduction and examples of the $type operator.
Syntax:
db.collection.find({ field: { $type: } })
- field: The field whose type is to be checked.
- type: The specified BSON type, which can be the numeric code of the type or a string of the type name.
BSON types
The following are common BSON types and their corresponding numeric codes and string names:
| Type code | Type name |
|---|---|
| 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 |
Example
Find documents where the field type is string:
db.myCollection.find({ fieldName: { $type: "string" } })
Or use the type code:
db.myCollection.find({ fieldName: { $type: 2 } })
Find documents where the field type is a number, for example, find documents where the age field type is an integer:
db.myCollection.find({ age: { $type: "int" } })
Or use the type code:
db.myCollection.find({ age: { $type: 16 } })
Find documents where the field type is a boolean:
db.myCollection.find({ isActive: { $type: "bool" } })
Or use the type code:
db.myCollection.find({ isActive: { $type: 8 } })
Find documents where the field type is a date:
db.myCollection.find({ createdAt: { $type: "date" } })
Or use the type code:
db.myCollection.find({ createdAt: { $type: 9 } })
Find documents where the field type is one of multiple types, for example, find documents where the value field type is a string or an integer:
db.myCollection.find({ value: { $type: ["string", "int"] } })
Or use the type code:
db.myCollection.find({ value: { $type: [2, 16] } })
Find documents where the details field type is an object and the score field type is a double-precision floating-point number:
Example
db.
myCollection
.
find
(
{
$and
:
[
{
details
:
{
$type
:
"object"
}
}
,
{
score
:
{
$type
:
"double"
}
}
]
}
)
Or use the type code:
Example
db.
myCollection
.
find
(
{
$and
:
[
{
details
:
{
$type
:
3
}
}
,
{
score
:
{
$type
:
1
}
}
]
}
)
By using the $type operator, you can precisely query the data type of a specific field in a document, enabling more fine-grained data filtering and management.
More examples
The database we use is named "runoob" and our collection is named "col"; the following is the data we inserted.
A simple collection "col":
>db.col.insert({
title: 'PHP 教程',
description: 'PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。',
by: 'Runoob Tutorial',
url: 'http://www.runoob.com',
tags: ['php'],
likes: 200
})
>db.col.insert({title: 'Java 教程',
description: 'Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。',
by: 'Runoob Tutorial',
url: 'http://www.runoob.com',
tags: ['java'],
likes: 150
})
>db.col.insert({title: 'MongoDB 教程',
description: 'MongoDB 是一个 Nosql 数据库',
by: 'Runoob Tutorial',
url: 'http://www.runoob.com',
tags: ['mongodb'],
likes: 100
})
Use the find() command to view the data:
> db.col.find()
{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "Runoob Tutorial", "url" : "http://www.runoob.com", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "Runoob Tutorial", "url" : "http://www.runoob.com", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "Runoob Tutorial", "url" : "http://www.runoob.com", "tags" : [ "mongodb" ], "likes" : 100 }
MongoDB Operator - $type Example
If you want to obtain data from the "col" collection where title is a String, you can use the following command:
db.col.find({"title" : {$type : 2}})
或
db.col.find({"title" : {$type : 'string'}})
The output is:
{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "Runoob Tutorial", "url" : "http://www.runoob.com", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "Runoob Tutorial", "url" : "http://www.runoob.com", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "Runoob Tutorial", "url" : "http://www.runoob.com", "tags" : [ "mongodb" ], "likes" : 100 }