MongoDB 教程
1. MongoDB 教程 2. NoSQL 简介 3. MongoDB 简介 4. Windows 平台安装 MongoDB 5. Linux 平台安装 MongoDB 6. MongoDB 概念解析 7. MongoDB 连接 8. MongoDB 插入文档 9. MongoDB 更新文档 10. MongoDB 删除文档 11. MongoDB 查询文档 12. MongoDB 条件操作符 13. MongoDB $type 操作符 14. MongoDB Limit与Skip方法 15. MongoDB 排序 16. MongoDB 索引 17. MongoDB 聚合 18. MongoDB 复制(副本集) 19. MongoDB 分片 20. MongoDB 备份(mongodump)与恢复(mongorestore) 21. MongoDB 监控 22. MongoDB Java 23. MongoDB PHP 24. MongoDB 关系 25. MongoDB 数据库引用 26. MongoDB 覆盖索引查询 27. MongoDB 查询分析 28. MongoDB 原子操作 29. MongoDB 高级索引 30. MongoDB 索引限制 31. MongoDB ObjectId 32. MongoDB Map Reduce 33. MongoDB 全文检索 34. MongoDB 正则表达式 35. MongoDB 管理工具: Rockmongo 36. MongoDB GridFS 37. MongoDB 固定集合(Capped Collections) 38. MongoDB 自动增长

MongoDB $type 操作符

MongoDB $type 操作符


描述

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

$type操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。

MongoDB 中可以使用的类型如下表所示:

类型 数字 备注
Double 1  
String 2  
Object 3  
Array 4  
Binary data 5  
Undefined 6 已废弃。
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  

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

简单的集合"col":


>db.col.insert({

    title: 'PHP 教程', 

    description: 'PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。',

    by: '',

    url: 'http://www.55mianshi.com',

    tags: ['php'],

    likes: 200

})



>db.col.insert({title: 'Java 教程', 

    description: 'Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。',

    by: '',

    url: 'http://www.55mianshi.com',

    tags: ['java'],

    likes: 150

})



>db.col.insert({title: 'MongoDB 教程', 

    description: 'MongoDB 是一个 Nosql 数据库',

    by: '',

    url: 'http://www.55mianshi.com',

    tags: ['mongodb'],

    likes: 100

})

使用find()命令查看数据:


> db.col.find()

{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "", "url" : "http://www.55mianshi.com", "tags" : [ "php" ], "likes" : 200 }

{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "", "url" : "http://www.55mianshi.com", "tags" : [ "java" ], "likes" : 150 }

{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "", "url" : "http://www.55mianshi.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.55mianshi.com", "tags" : [ "php" ], "likes" : 200 }

{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "", "url" : "http://www.55mianshi.com", "tags" : [ "java" ], "likes" : 150 }

{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "", "url" : "http://www.55mianshi.com", "tags" : [ "mongodb" ], "likes" : 100 }