MongoDB表結(jié)構(gòu)分析工具介紹--Variety-創(chuàng)新互聯(lián)

今天給大家介紹一款分析MongoDB數(shù)據(jù)庫表結(jié)構(gòu)的軟件 -- Varity.對于MongoDB這種Schema Free的數(shù)據(jù)庫來說,用軟件自帶的查詢collection中存儲的數(shù)據(jù)情況很難一眼就看出具體的數(shù)據(jù)結(jié)構(gòu),Tomá Dvoák 作者寫了一個Variety.js的腳本就很容易理解沒個collection中的數(shù)據(jù)結(jié)構(gòu)。作者將工具托管在github上,并且歡迎任何人來提供建議或者添加功能。以下Variety的特點翻譯自作者的博客:

建網(wǎng)站原本是網(wǎng)站策劃師、網(wǎng)絡(luò)程序員、網(wǎng)頁設(shè)計師等,應(yīng)用各種網(wǎng)絡(luò)程序開發(fā)技術(shù)和網(wǎng)頁設(shè)計技術(shù)配合操作的協(xié)同工作。創(chuàng)新互聯(lián)專業(yè)提供成都做網(wǎng)站、成都網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站制作(企業(yè)站、成都響應(yīng)式網(wǎng)站建設(shè)、電商門戶網(wǎng)站)等服務(wù),從網(wǎng)站深度策劃、搜索引擎友好度優(yōu)化到用戶體驗的提升,我們力求做到極致!

collection信息輸出格式是ASCII的。

  • 可以很清晰看到每個key使用的是什么類型的數(shù)據(jù)格式

  • 可以看到?jīng)]個key在這個collection的使用率是多少

  • 可以限制documents的查詢數(shù)量

  • 可以限制查詢documents的深度

  • 可以只分析documents的子集

  • 可以對查詢結(jié)果排序

  • 可以保存查詢結(jié)果

  • 可以以JSON格式輸出

  • 工具簡介易用,沒用任何其他庫依賴

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

使用方法:

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

我事先插入的數(shù)據(jù)是

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查詢結(jié)果是這樣的

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 | +--------------------------------------------------------------------+

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

如果數(shù)據(jù)庫用的不是默認(rèn)端口,可以用--port參數(shù):

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

如果db文件不在默認(rèn)文件,可以用--dbpath參數(shù):

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

如果需要對查詢進(jìn)行排序的話可以這樣用:

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億個數(shù)據(jù),我們可以限制查詢的數(shù)量,用limit來限定:

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

如果某個colletions嵌套的層數(shù)太多了,可以用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在對數(shù)據(jù)結(jié)構(gòu)進(jìn)行分析的時候,實際是用MapReduce來做的,會進(jìn)行全表掃描操作,所以如果是對線上庫進(jìn)行分析,那么建議最好使用一個不提供服務(wù)的備份庫或者在業(yè)務(wù)低峰來做。避免給線上業(yè)務(wù)造成壓力。

參考地址:

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

https://github.com/variety/variety

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

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

本文名稱:MongoDB表結(jié)構(gòu)分析工具介紹--Variety-創(chuàng)新互聯(lián)
當(dāng)前URL:http://muchs.cn/article24/djigce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、關(guān)鍵詞優(yōu)化定制開發(fā)、全網(wǎng)營銷推廣做網(wǎng)站、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司