a description
In this section, we will continue our discussion of the conditional operator $type in MongoDB.
The $type operator is based on the BSON type to retrieve the matching data type in the collection and return the result.
The types that can be used in MongoDB are shown in the following table:
Type
Number
Notes
Double | 1 | |
String | 2 | |
Object | 3 | |
Array | 4 | |
Binary data | 5 | |
Undefined | 6 | Obsolete. |
Object id | 7 | |
Boolean | 8 | |
Date | 9 | |
Null | 10 | |
Regular Expression | 11 | |
JavaScript | 13 | |
Symbol | 14 | |
JavaScript (with scope) | 15 | |
32-bit integer | 16 | |
Timestamp | 17 | |
64-bit integer | 18 | |
Min key | 255 | Query with -1. |
Max key | 127 |
Two examples
If you want to get the data whose title is String in the "col" collection, you can use the following command:
> use runoob switched to db runoob > db.col.find({"title" : {$type : 2}}) { "_id" : ObjectId("593b87cbbcd6757fd2d30303"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强 有力的服务器端脚本语言。", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "php" ], "likes" : 200 } { "_id" : ObjectId("593b87dbbcd6757fd2d30304"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "java" ], "likes" : 150 } { "_id" : ObjectId("593b87e5bcd6757fd2d30305"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb" ], "likes" : 100 }