今天给大家介绍一款分析MongoDB数据库表结构的软件 -- Varity.对于MongoDB这种Schema Free的数据库来说,用软件自带的查询collection中存储的数据情况很难一眼就看出具体的数据结构,Tomá Dvoák 作者写了一个Variety.js的脚本就很容易理解没个collection中的数据结构。作者将工具托管在github上,并且欢迎任何人来提供建议或者添加功能。以下Variety的特点翻译自作者的博客:

collection信息输出格式是ASCII的。

  • 可以很清晰看到每个key使用的是什么类型的数据格式

  • 可以看到没个key在这个collection的使用率是多少

  • 可以限制documents的查询数量

  • 可以限制查询documents的深度

  • 可以只分析documents的子集

  • 可以对查询结果排序

  • 可以保存查询结果

  • 可以以JSON格式输出

  • 工具简介易用,没用任何其他库依赖

Variety的下载地址 https://github.com/variety/variety。

使用方法:

mongo DATABASE_NAME --eval "var collection = 'COLL_NAME' " variety.js,比如我的DATABASE_NAME 是test, COLL_NAME是users,

我事先插入的数据是

db.users.insert({name: "Tom", bio: "A nice guy.", pets: ["monkey", "fish"], someWeirdLegacyKey: "I like Ike!"});
db.users.insert({name: "Dick", bio: "I swordfight.", birthday: new Date("1974/03/14")});
db.users.insert({name: "Harry", pets: "egret", birthday: new Date("1984/03/14")});
db.users.insert({name: "Shanker", bio: "a va?"});

正常的查询Users的回显是这样的

> db.users.find()
{ "_id" : ObjectId("56cfc28fbdae9b9a922a19cb"), "name" : "Tom", "bio" : "A nice guy", "pets" : [  "monkey",  "fish" ], "someWeirdLegacyKey" :                                                                                     "I like ike!" }
{ "_id" : ObjectId("56cfc2acbdae9b9a922a19cc"), "name" : "Dick", "bio" : "I swordfight." }
{ "_id" : ObjectId("56cfc2c6bdae9b9a922a19cd"), "name" : "Harry", "pets" : "egret" }
{ "_id" : ObjectId("56cfc2e0bdae9b9a922a19ce"), "name" : "Shanker", "bio" : "caca" }

用Variety查询结果是这样的

mongo test --eval "var collection = 'users'" variety.js
MongoDB shell version: 2.4.9
connecting to: test
Variety: A MongoDB Schema Analyzer
Version 1.5.0, released 14 May 2015
Using collection of "users"
Using query of { }
Using limit of 4
Using maxDepth of 99
Using sort of { "_id" : -1 }
Using outputFormat of "ascii"
Using persistResults of false
Using resultsDatabase of "varietyResults"
Using resultsCollection of "usersKeys"
Using resultsUser of null
Using resultsPass of null
Using plugins of [ ]
+--------------------------------------------------------------------+
| key                | types                | occurrences | percents |
| ------------------ | -------------------- | ----------- | -------- |
| _id                | ObjectId             |           4 |    100.0 |
| name               | String               |           4 |    100.0 |
| bio                | String               |           3 |     75.0 |
| pets               | String (1),Array (1) |           2 |     50.0 |
| someWeirdLegacyKey | String               |           1 |     25.0 |
+--------------------------------------------------------------------+

是不是格式很友好,很容易读懂了呢?

如果数据库用的不是默认端口,可以用--port参数:

mongo DATABASE_NAME --port 27111 --eval " var collection = 'COLL_NAME' " variety.js

如果db文件不在默认文件,可以用--dbpath参数:

mongo DATABASE_NAME --dbpath /path/to/database/folder --eval "var collection = 'COLL_NAME' " variety.js

如果需要对查询进行排序的话可以这样用:

mongo DATABASE_NAME --eval "var collection = 'COLL_NAME', sort = { date : -1 }" variety.js

如果需要JSON的输出格式的话可以这样用:

mongo DATABASE_NAME --eval "var collection = 'users', outputFormat = 'json' " variety.js

如果一个collection有10亿个数据,我们可以限制查询的数量,用limit来限定:

mongo DATABASE_NAME --eval "var collection ='users', limit = 1000 " variety.js

如果某个colletions嵌套的层数太多了,可以用maxDepth来限制查询:

db.users.insert({name:"Walter", someNestedObject:{a:{b:{c:{d:{e:1}}}}}});
[ibmcloud@bravo:~/variety04:05]$mongo test --eval "var collection = 'users' " variety.js
MongoDB shell version: 2.4.9
connecting to: test
Variety: A MongoDB Schema Analyzer
Version 1.5.0, released 14 May 2015
Using collection of "users"
Using query of { }
Using limit of 5
Using maxDepth of 99
Using sort of { "_id" : -1 }
Using outputFormat of "ascii"
Using persistResults of false
Using resultsDatabase of "varietyResults"
Using resultsCollection of "usersKeys"
Using resultsUser of null
Using resultsPass of null
Using plugins of [ ]
+----------------------------------------------------------------------------+
| key                        | types                | occurrences | percents |
| -------------------------- | -------------------- | ----------- | -------- |
| _id                        | ObjectId             |           5 |    100.0 |
| name                       | String               |           5 |    100.0 |
| bio                        | String               |           3 |     60.0 |
| pets                       | String (1),Array (1) |           2 |     40.0 |
| someNestedObject           | Object               |           1 |     20.0 |
| someNestedObject.a         | Object               |           1 |     20.0 |
| someNestedObject.a.b       | Object               |           1 |     20.0 |
| someNestedObject.a.b.c     | Object               |           1 |     20.0 |
| someNestedObject.a.b.c.d   | Object               |           1 |     20.0 |
| someNestedObject.a.b.c.d.e | Number               |           1 |     20.0 |
| someWeirdLegacyKey         | String               |           1 |     20.0 |
+----------------------------------------------------------------------------+
[ibmcloud@bravo:~/variety05:06]$mongo test --eval "var collection = 'users', maxDepth = 3" variety.js
MongoDB shell version: 2.4.9
connecting to: test
Variety: A MongoDB Schema Analyzer
Version 1.5.0, released 14 May 2015
Using collection of "users"
Using query of { }
Using limit of 5
Using maxDepth of 3
Using sort of { "_id" : -1 }
Using outputFormat of "ascii"
Using persistResults of false
Using resultsDatabase of "varietyResults"
Using resultsCollection of "usersKeys"
Using resultsUser of null
Using resultsPass of null
Using plugins of [ ]
+----------------------------------------------------------------------+
| key                  | types                | occurrences | percents |
| -------------------- | -------------------- | ----------- | -------- |
| _id                  | ObjectId             |           5 |    100.0 |
| name                 | String               |           5 |    100.0 |
| bio                  | String               |           3 |     60.0 |
| pets                 | String (1),Array (1) |           2 |     40.0 |
| someNestedObject     | Object               |           1 |     20.0 |
| someNestedObject.a   | Object               |           1 |     20.0 |
| someNestedObject.a.b | Object               |           1 |     20.0 |
| someWeirdLegacyKey   | String               |           1 |     20.0 |
+----------------------------------------------------------------------+

如果需要制定条件的查询,比如carddAbout为true的,可以这样:

mongo DATABASE_NAME --eval "var collection = 'COLL_NAME', query = {'caredAbout':true}" variety.js

需要注意的是,Variety在对数据结构进行分析的时候,实际是用MapReduce来做的,会进行全表扫描操作,所以如果是对线上库进行分析,那么建议最好使用一个不提供服务的备份库或者在业务低峰来做。避免给线上业务造成压力。

参考地址:

http://www.acetolyne.net/Projects7/node/48

https://github.com/variety/variety

http://www.mongoing.com/archives/2282

转载于:https://blog.51cto.com/shanker/1745258

MongoDB 表结构分析工具介绍 -- Variety相关推荐

  1. 根据数据库表结构生成Excel表设计——工具介绍

    根据数据库表结构生成Excel表设计 最近又接手了一些老系统,库表设计啥的都没有,十来个系统,每个系统都几十张表,一个个的补感觉太难了,合计了一下感觉还是搞个工具比较好. 在系统运维的过程中,很多时候 ...

  2. 彩虹表原理详解及工具介绍

    PS:这玩意偶前几天用了一下,确实强悍无比,在这个表面前,md5等公开的加密算法不堪一击啊.记得我之前的公司开发的游戏账号都用修改过的特有MD5加密算法,建议开发人员都这样搞,这样安全性就大大提高.如 ...

  3. Windows 安装 MongoDB 和 可视化工具Robo3T

    MongoDB 官网下载地址:https://www.mongodb.com/try/download/community MongoDB 是一款非常热门的 NoSQL 面向文档的数据库管理系统, 分 ...

  4. Mongodb网页管理工具,基于Spring Boot2.0,前端采用layerUI实现

    源码:https://github.com/a870439570/Mongodb-WeAdmin 项目介绍 Mongodb网页管理工具,基于Spring Boot2.0,前端采用layerUI实现. ...

  5. 七周三次课(11月29日) 10.11 Linux网络相关 10.12 firewalld和netfilter 10.13 netfilter5表5链介绍 10.14 iptables语法...

    2019独角兽企业重金招聘Python工程师标准>>> 10.11 Linux网络相关 ifconfig 查看网卡ip (yum install net-tools) 安装 -a   ...

  6. 七周三次课(1月24日) 10.11 Linux网络相关 10.12 firewalld和netfilter 10.13 netfilter5表5链介绍 10.14 iptables语法...

    七周三次课(1月24日) 10.11 Linux网络相关 10.12 firewalld和netfilter 10.13 netfilter5表5链介绍 10.14 iptables语法 ====== ...

  7. anemometer mysql_MySQL慢日志简介及Anemometer工具介绍 | | For DBA

    原标题:MySQL慢日志简介及Anemometer工具介绍 | | For DBA 作者:王航威 - fordba.com 来源:http://fordba.com/box-anemometer-vi ...

  8. MySQL数据库中导入导出方法以及工具介绍

    MySQL数据库中导入导出方法以及工具介绍 1.MySQLimport的语法介绍: mysqlimport位于mysql/bin目录中,是mysql的一个载入(或者说导入)数据的一个非常有效的工具.这 ...

  9. fiddler和wireshark工具介绍及对比 - [测试技术知识]

    2013-11-19 fiddler和wireshark工具介绍及对比 - [测试技术知识] 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://www.blogbus.co ...

最新文章

  1. splitcontainer如何设置两边一样打_墙洞加筋如何计算?
  2. nsTimer的简单用法
  3. 【CodeForces】960 F. Pathwalks 主席树+动态规划
  4. AI 玩微信跳一跳的正确姿势:跳一跳 Auto-Jump 算法详解
  5. OS- -操作系统概念
  6. mysql 并发_Mysql事务,并发问题,锁机制
  7. AttributeError: partially initialized module ‘aiohttp‘ has no attribute ‘ClientSession‘ (most...)
  8. 小技巧:Mac下快速锁屏
  9. 字符集,字体,编码,代码页,输入法
  10. DNN硬件加速器设计2 -- Survey of DNN Development Resouces and DNN Hardware(MIT)
  11. 接口自动化:淘宝的登录、搜索商品、确认订单、付款流程
  12. python query函数的用法_python help函数实例用法
  13. 从零构建 React 开发环境(一) —— hello world,麻雀虽小五脏俱全~
  14. 大数据技术如何有效阻击网络黑产?
  15. 《人机交互技术》结课作业:界面调研报告交互界面设计快速原型设计(华科软院)
  16. html代码 imgn,html代码大全
  17. 循环世界模型(Recurrent World Models)——真实世界建模的强化学习利器
  18. CSS中visibility 属性
  19. 用MOBA游戏的方式来评估候选人实力
  20. 没有链克口袋,但我一样玩游戏掘金

热门文章

  1. 利用jaxp对xml进行dom解析
  2. scrum 11.28
  3. linux之rsync远程同步文件
  4. 《jQuery Cookbook中文版》——1.9 根据当前上下文遍历DOM获得新的DOM元素集
  5. iOS8中添加的extensions总结(一)——今日扩展
  6. 小米虚高的估值泡沫要破了么?
  7. delphi listbox 使用
  8. MongoDB工具MagicMongoDBTool使用介绍(一) -- 简单MongoDB入门
  9. 电子书下载(强烈推荐):《大道至简——软件工程实践者的思想》
  10. 【免费毕设】ASP.NET报名管理信息系统(源代码+lunwen)