進階使用Nodejs-淺談
NoSQL(MongoDB)
MiCloud Team Benson
什麼是NoSQL
Big User
什麼是NoSQL
Big Data
什麼是NoSQL
五大特性
● Not Only SQL
● 水平擴充資料庫容量
● No Schema
● 資料遲早一致
● 新技術成熟度不足,版本風險
NoSQL的種類與特性
NoSQL的種類與特性
About Mongo DB
●
●
●
●

Open Source
Document database
Written in C++
Bson
mongoDB VS SQL
Database == Database
Collections == Table
Document == Row
實作Nodejs and MongoDB
安裝Mongodb
範例程式
http://goo.gl/SbvZPx
程式實作Create document
● npm install mongodb
var doc1 = {'hello':'doc1'};
var doc2 = {'hello':'doc2'};
var lotsOfDocs = [{'hello':'doc3'}, {'hello':'doc4'}];
collection.insert(doc1);
collection.insert(doc2, {w:1}, function(err, result) {});
collection.insert(lotsOfDocs, {w:1}, function(err, result) {});
程式實作Query document
//查詢全部
collection.find().toArray(function(err, items) {})
//除了2以外
var stream = collection.find({mykey:{$ne:2}}).stream();
stream.on("data", function(item) {});
stream.on("end", function() {});
//單一筆
collection.findOne({mykey:1}, function(err, item) {});
程式實作update document
collection.update({mykey:1}, {$set:{fieldtoupdate:2}}, {w:1}, function
(err, result) {});
collection.update({mykey:2}, {$push:{docs:{doc2:1}}}, {w:1}, function
(err, result) {});
程式實作Delete Document
//刪除單一筆
collection.remove({mykey:1});
//刪除單一筆並傳回結果
collection.remove({mykey:2}, {w:1}, function(err, result) {});
//刪除全部
collection.remove();
Map-Reduce
什麼是 Map
● Function
● 平行獨立,不影響原 Collection
MAP

Collection

[{key: value},
{key: value},
{key: value}]
Map-Reduce
Map-Reduce
什麼是Reduce
Map-Reduce
NoSQL知名案例
●
●
●
●

Facebook (Cassandra , HBase)
Twitter (Cassandra, HBase)
Yahoo(HBase)
力可科技(Cassandra)
其他資源
● MongoHQ http://www.mongohq.com/home
● mongoDB
http://www.mongodb.org/
謝謝大家

進階使用Nodejs 淺談no sql(mongodb)