自强学堂
自强学堂:学习、分享、让你更强!
MongoDB 教程HTMLCSSJAVASCRIPTJQUERYSQLPHPBOOTSTRAPANGULARXML
 

MongoDB条件操作符 – $type

描述

在本章节中,我们将继续讨论MongoDB中条件操作符 $type。

$type操作符是基于BSON类型来检索集合中匹配的结果。

MongoDB中可以使用的类型:

类型描述类型值
Double1
String2
Object3
Array4
Binary data5
Object id7
Boolean8
Date9
Null10
Regular expression11
JavaScript code13
Symbol14
JavaScript code with scope15
32-bit integer16
Timestamp17
64-bit integer18
Min key255
Max key127

我们使用的数据库名称为"myinfo" 我们的集合名称为"testtable",以下为我们插入的数据。

简单的集合"testtable":

mongodb-sample-table


MongoDB 操作符 - $type 实例

如果想获取 "testtable" 集合包含在 "extra" 中的"friends"为BSON类型的对象,你可以使用以下命令:

> db.testtable.find({"extra.friends" : {$type : 3}})

mongodb-type-operator


更多实例

查询所有name字段是字符类型的数据:

db.users.find({name: {$type: 2}});

查询所有age字段是整型的数据:

db.users.find({age: {$type: 16}});