MongoDB $typeOperator

In MongoDB,$typethe operator is used to query documents whose fields have a specified type.

MongoDB's $type operator is used to query the BSON data type of fields.

It allows you to specify one or more types and returns documents that match these types.

The following is a detailed introduction and examples of the $type operator.

Syntax:

db.collection.find({ field: { $type: <type> } })
  • field: the field whose type is to be checked.
  • type: the specified BSON type, which can be a 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 CodeType Name
1double
2string
3object
4array
5binData
6undefined
7objectId
8bool
9date
10null
11regex
12dbPointer
13javascript
14symbol
15javascriptWithScope
16int
17timestamp
18long
19decimal
255minKey
127maxKey

Example

Find documents whose field type is a string:

db.myCollection.find({ fieldName: { $type: "string" } })

Or use the type code:

db.myCollection.find({ fieldName: { $type: 2 } })

Find documents whose 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 whose field type is a boolean:

db.myCollection.find({ isActive: { $type: "bool" } })

Or use the type code:

db.myCollection.find({ isActive: { $type: 8 } })

Find documents whose field type is a date:

db.myCollection.find({ createdAt: { $type: "date" } })

Or use the type code:

db.myCollection.find({ createdAt: { $type: 9 } })

Find documents whose 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 specific fields in documents, thereby performing more fine-grained data filtering and management.


More Examples

The database we use is named "example" and our collection is named "col". The following is the data we inserted.

Simple collection "col":

>db.col.insert({
    title: 'PHP 教程', 
    description: 'PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。',
    by: 'Example',
    url: 'http://www.example.com',
    tags: ['php'],
    likes: 200
})

>db.col.insert({title: 'Java 教程', 
    description: 'Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。',
    by: 'Example',
    url: 'http://www.example.com',
    tags: ['java'],
    likes: 150
})

>db.col.insert({title: 'MongoDB 教程', 
    description: 'MongoDB 是一个 Nosql 数据库',
    by: 'Example',
    url: 'http://www.example.com',
    tags: ['mongodb'],
    likes: 100
})

Use the find() command to view the data:

> db.col.find()
{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "Example", "url" : "http://www.example.com", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "Example", "url" : "http://www.example.com", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "Example", "url" : "http://www.example.com", "tags" : [ "mongodb" ], "likes" : 100 }

MongoDB Operator - $type Example

If you want to get the data where title is a String in the "col" collection, you can use the following command:

db.col.find({"title" : {$type : 2}})
或
db.col.find({"title" : {$type : 'string'}})

The output result is:

{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "Example", "url" : "http://www.example.com", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "Example", "url" : "http://www.example.com", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "Example", "url" : "http://www.example.com", "tags" : [ "mongodb" ], "likes" : 100 }
Other Extensions