2014年5月4日 星期日

MongoDB 學習筆記之二 - MongoDB 簡介

一、什麼是 MongoDB?

MongoDB 是一套 open source 的 NoSQL DB。MongoDB 本身是用 C++ 開發的,它提供了高效能 (high performace)、高可用度 (high availability) 以及自動擴充 (automatic scaling) 等特色。

如下圖所示,在 MongoDB 中,一筆資料就是一個「document」(文件),所謂的 document 是由多個 field-value pairs 所組成的,其結構類似於 JSON 物件。一個 field (欄位) 裡的 value (值)可以是其他的文件、陣列或者由文件組成的陣列。


圖一、MongoDB 中每一筆紀錄都是 JSON 格式的 document

MongoDB 以 document 作為基本的資料儲存單位,有下列幾項好處
  1. JSON document 可對應於許多物件導向程式語言的原生資料類型 (如:物件),這也是近年來 JSONORM (Object-Relational Mapping) 日漸風行的原因 。
  2. RDBMS 的 JOIN 操作其實是成本高昂的,但如果採用 嵌入式文件 (embedded document) 或陣列的方式,可減少 JOIN 操作的使用。
  3. 動態的 schema 支援流暢的多型 (ploymorphism)。
本文寫作時的 MongoDB 社群正式版(production release)為 2.6.0 (2014/04/08 發行)。目前支援的 OS 如下表所示:

WindowsLinuxMac OS XSolaris
32-bit
64-bit

您可以在 MognoDB 官網的「Downloads」網頁下載。不過要注意的是,32 位元的版本有一個限制,即最多只能儲存 2 GB 的資料。所以,一般說來,你應該使用 64 位元版本。32 位元版本僅適用在 replica set 的「arbiter」(裁決者) 角色,或者用做 sharding 的 mongos (但請不要用於正式環境),相關資訊請參考這個網頁

MongoDB 的授權許可(license):
  • Database Server 和 Tools:AGPL
  • Drivers:Apache License
  • MongoDB Enterprise (企業版):MongoDB Inc. 提供商用的 licenses

MongoDB 的 key features(摘錄自官網):
  • High Performance
    • Support for embedded data models reduces I/O activity on database system.
    • Indexes support faster queries and can include keys from embedded documents and arrays.
  • High Availability (replica set)
    A replica set is a group of MongoDB servers that maintain the same data set, providing redundancy and increasing data availability.
    • automatic failover.
    • data redundancy.
  • Automatic Scaling (sharding)
    • Automatic sharding distributes data across a cluster of machines.
    • Replica sets can provide eventually-consistent reads for low-latency high throughput deployments.

二、關於 MongoDB Inc.

www.mongodb.org」是 MongoDB 社群版的官方網站,「www.mongodb.com」則是 MongoDB 企業版的官方網站,這兩個網站基本上都是由 Mongo Inc. 這家公司在維護。2013 年 8 月 27 日以前,MongoDB Inc. 其實叫做「10gen Inc.」的,為了避免造成大家的困惑,所以直接以產品名稱作為公司的名稱,詳情請參考 Tech Crunch 網站上這篇報導:《10gen Is Now MongoDB To Reflect Focus On NoSQL Database》。

2007 年,10gen Inc. 成立於紐約。由 Dwight Merriman (前 DoubleClick 創辦人兼 CTO )、Kevin P. Ryan (前 DoubleClick CEO、Gilt Groupe 創辦人)、 Eliot Horowitz (前 DoubleClck 工程師、 ShopWiki 創辦人兼 CTO ) 三人,接受了來自 Flybridge Capital Partners、In-Q-Tel、Intel、New Enterprise Associates (NEA)、Red Hat、紅杉資本 (Sequoia Capital) 以及 Union Square Ventures 共約 8,100 萬美金的投資。

10gen 最初的目標是希望能建立一個完全基於 open source 元件的 PaaS 架構,但因為該公司始終找不到一套既有的 DB 平台,可以符合他們建造此雲端架構的「原則」。因此,這家公司開始發展一套文件導向的資料庫系統 —— MongoDB

在發現這套系統的潛力之後,10gen 決定放棄原來的雲端平台計畫,全力投入 MongoDB 的開發與維護。2009 年,10gen 將 MongoDB 公開釋出為 open source 專案。2010 年 8 月,10gen 設立了位於美國西岸加州 Palo Alto 的辦公室。2011 年,陸續在維吉尼亞州的 Reston、英國倫敦、愛爾蘭都柏林、西班牙巴塞隆納、澳洲雪梨等地設立據點。

2012 年 9 月,在 華爾街日報 一篇《The Next Big Thing 2012》的報導裡,10gen 評為頂尖軟體公司排行榜的第 9 名

2013 年 4 月,10gen 搬進了舊紐約時報大樓。並於同年 8 月 27 日 10gen 宣佈將更名為 MongoDB Inc.,讓大家能由公司名稱直接就聯想到其代表性產品。

2013 年 10 月,MongoDB Inc. 在新一輪融資中獲得了1.5 億美元的風險創投資金,其估值達到 12 億美元,成為紐約估值最高的新創公司!(詳情請參考 Bloomberg 的這篇報導:《MongoDB Now King of NYC Startups With $1.2 Billion Valuation》和 TechOrange 的這篇報導:《NoSQL 資料庫的春天:MongoDB 獲得 1.5 億美元融資,它背後的故事是什麼?》)

三、MongoDB vs. RDBMS

以下這張 MongoDB 與 RDBMS 的對照表摘錄自官網的《SQL to MongoDB Mapping Chart》:

RDBMSMongoDB
databasedatabase
tablecollection
rowdocument or BSON document
columnfield
indexindex
table joinsembedded documents and linking
primary key
Specify any unique column or column combination as primary key.
primary key
In MongoDB, the primary key is automatically set to the _id field.
aggregation (e.g. group by)aggregation pipeline
See the SQL to Aggregation Mapping Chart.

下圖是在「users」這個 collection 裡新增一筆 document 的範例,也說明了 MongoDB 中的 Collection 與 Document 之間的關係:

圖二、Collection 與 Document 之間的關係

下表則是 MongoDB 與 RDBMS 在 DDL 與 DML 的比較表:

DML / DDLRDBMSMongoDB
DDLCREATE TABLEdb.collection.insert()
db.createCollection()
ALTER TABLEdb.collection.update()
DROP TABLEdb.collection.drop()
CREATE INDEXdb.collection.ensureIndex()
DROP INDEXdb.collection.dropIndex()
db.collection.dropIndexes()
DMLINSERTdb.collection.insert()
db.collection.save()
UPDATEdb.collection.update()
db.collection.save()
DELETEdb.collection.remove()
SELECTdb.collection.find()
db.collection.findOne()

※ 關於「db.collection.save()」,請參考官方文件
  •  如果未指定 _id,則等同執行「db.collection.insert()」。
  •  如果有指定 _id,則等同執行 upsert(即 db.collection.update({ _id: "XXXX"}, {document}, { upsert : true}))。若該 _id 已存在,則更新該筆 document;若該 _id 不存在,則新增一筆 document。

四、MongoDB 的安裝

MongoDB 的安裝非常簡單,官網已經提供了各種作業系統的安裝說明(如下),所以這裡就不再贅述囉。
五、結論

MongoDB 因為獲得相當龐大的資金挹注,所以銀彈充足,目前改版速度相當快。以最近兩次改版僅間隔三個月(2014/01/10 才出 2.4.9 版、2014/04/08 便跳到 2.6.0 版),以及 2.6.0 版的 release notes 所揭露的新功能與改版幅度來看,現在 MongoDB 應該處於急速成長的階段。

雖然如此,但我仍覺得 MongoDB 是相當值得一試的產品!

1 則留言:

  1. online casino【WG】play the best roulette, blackjack
    roulette, blackjack, and more ✓ 【 온라인 카지노 벳무브 play online casino online roulette casino free】✓No Signup ✓ Free Spins ✓ Mobile Version ✓ Real Time Gaming.

    回覆刪除