SlideShare ist ein Scribd-Unternehmen logo
1 von 22
พิสิษฐ์ มรรคไพสิฐ
5514552616
เกี่ยวกับ MongoDB
• พัฒนาโดยบริษัท 10gen
• เป็น Document-oriented Database
• มี feature มากมายเหมือน Relational
Database
• Scalable (Replication, Sharding)
• สนับสนุน Map-Reduce
2
JSON Document
• ประกอบด้วยหลาย Database
• แต่ละ Database ประกอบด้วยหลาย Collections
(เทียบได้กับ Table)
• ข้อมูลจะถูกเรียกว่า Document (เทียบได้กับ
Row)
• JSON document (key, value) เช่น{
“name” : “som-tam”,
“price” : 30,
“ingredients” : [“papaya”, “tomato”, “chili”]
}
3
BSON Encoding
• ผู้ใช้มอง document อยู่ในรูปแบบของ JSON
• ข้อมูลจริงจะเก็บในรูป BSON (Binary JSON)
• BSON - http://bsonspec.org
{_id: ObjectId(XXXXXXXXXXXX), hello: “world”}
x27x00x00x00x07_idx00XXXXXXXXXXXX
x02hellox00x06x00x00x00worldx00x00
4
เปรียบเทียบกับ Relational DB
Server
Database
Table
Row
Column
• มี Join
• Atomicity ระดับ
Transaction
Server
Database
Collection
Document
(JSON)
Field
• ไม่มี Join
• Atomicity ระดับ
Relational DB
MongoDB
5
เริ่มต้นใช้งาน
• DBMS ของ MongoDB
– mongod รันเป็น daemon process
• Mongo Shell
– mongo ส่งคำาสั่งไปยัง mongod โดยใช้ภาษา
JavaScript
• MongoDB API (http://api.mongodb.org/)
– pymongo สำาหรับภาษา Python
6
ตัวอย่างคำาสั่ง
> db.people.insert( {name:’Wachira Poothong’,
email:’wp@zzz.com’, sex:’male’} )
> db.people.insert( {name:’Pisit Makpaisit’, age:25,
interesting:[‘Parallel Computing’, ‘Programming’]} )
Schemal
ess!
7
ตัวอย่างคำาสั่ง
> db.people.find()
{ "_id" : ObjectId("51206cee15407f12ea3db21b"), "name"
: "Wachira Poothong", "email" : "wp@zzz.com", "sex" :
"male" }
{ "_id" : ObjectId("51206d2a15407f12ea3db21c"), "name"
: "Pisit Makpaisit", "age" : 25, "interesting" : [ "Parallel
Computing", "Programming" ] }
db.people.insert( {name:’Wachira Poothong’,
email:’wp@zzz.com’, sex:’male’} )
> db.people.find( {name:’Pisit Makpaisit’} )
{ "_id" : ObjectId("51206d2a15407f12ea3db21c"), "name"
: "Pisit Makpaisit", "age" : 25, "interesting" : [ "Parallel
Computing", "Programming" ] } 8
ตัวอย่างคำาสั่ง
> db.people.find( {age:{$gt:15}} )
{ "_id" : ObjectId("51206d2a15407f12ea3db21c"), "name"
: "Pisit Makpaisit", "age" : 25, "interesting" : [ "Parallel
Computing", "Programming" ] }
> db.people.find( {email:{$exist:true}} )
{ "_id" : ObjectId("51206cee15407f12ea3db21b"), "name"
: "Wachira Poothong", "email" : "wp@zzz.com", "sex" :
"male" }
9
ตัวอย่างคำาสั่ง
> db.people.update( {name:’Wachira Poothong’}, {age:25} )
> db.people.findOne( )
{ "_id" : ObjectId("51206cee15407f12ea3db21b"),
"name" : "Wachira Poothong", “age" : 25, "email" :
"wp@zzz.com", "sex" : "male" }
> db.people.remove( {name:’Wachira Poothong’} )
> db.people.remove()
10
CRUD
Operator SQL MongoDB
Create INSERT INSERT
Read SELECT FIND
Update UPDATE UPDATE
Delete DELETE REMOVE
11
การออกแบบ Schema
• เนื่องจากไม่มี Join เหมือน RDBMS
• Pre-join หรือ Embedded
• Application-Driven Schema - ออกแบบโดยดู
จากการใช้งานข้อมูลของโปรแกรม
12
ตัวอย่างเว็บเพจหน้าแสดงข้อมูล
DVD• RDBMS เก็บข้อมูลเป็น table ดังนี้
– DVD (dvd_id, name, volume, price, distributor_id)
– Distributor (distributor_id, name, address)
– Comment (comment_id, dvd_id, author, body, date)
• MongoDB เก็บเป็น document เดียวกัน
– { name: ‘Digimon Xros Wars’, volume: 6, price: 169,
distributor: ‘TIGA’, comments: [
{author: ‘เกรียน’, body: ‘ที่ 1’, date: ‘2012-12-21
13:34:42’},
{author: ‘น้องแป้ง’, body: ‘สนุกจุงเบย’, date: ‘2012-
12-22 10:12:07’}] 13
ทำายังไงเมื่อไม่มี Join
• Join เอง ในระดับ Application
• Pre-join หรือ Embedded โดยพิจารณาจาก
– ความถี่ในการใช้งานร่วมกัน
– ขนาดของ document
• Embedded ทำาให้ atomicity ระดับ document
เพียงพอ
14
Replication
• เก็บข้อมูลซ้ำ้ากันในหลายที่
• เพิ่ม availability
• Back up ข้อมูล
• Read ข้อมูลได้เร็วขึ้น
15
http://blog.codecentric.de/en/2012/12/mongodb-the-sixth-sense/
Replica Sets
• เป็น cluster ของ mongod (daemon)
• ข้อมูลจะถูกเขียนลงไปที่ตัว primary (มีเพียงตัว
เดียว)
• ตัวอื่นจะเรียกว่า secondary และคัดลอกข้อมูล
มาจาก primary
• Automated failover – ถ้า primary ตาย จะ
เลือกตัวใหม่มาเป็น primary อัตโนมัติ
16
Sharding
• แบ่งกลุ่มของ document ไป
ยังเครื่องต่างๆ
• แบ่ง Collection ออกเป็น
chunks
• ใช้ shard key เป็นหลักใน
การแบ่ง
• เมื่อมี shard หนึ่งมี chunks
มากเมื่อเทียบกับ shard อื่น
จะทำาการ redistribute 17
http://blog.codecentric.de/en/2012/12/mongodb-the-sixth-sense/
เมื่อไหร่จึงทำา Sharding
• เมื่อข้อมูลมากจน Storage ของเครื่องเดียวเก็บ
ไม่พอ
• เพิ่มประสิทธิภาพด้วยการเพิ่มข้อมูลที่ทำางานบน
RAM
• มีการเขียนข้อมูลจำานวนมาก (ต้องการให้เขียน
ได้เร็ว)
18
Map-Reduce
• { _id: ObjectId("50a8240b927d5d8b5891743c"),
cust_id: "abc123",
ord_date: new Date("Oct 04, 2012"),
status: 'A',
price: 250,
items: [ { sku: "mmm", qty: 5, price: 2.5 },
{ sku: "nnn", qty: 5, price: 2.5 } ] }
• อยากรู้ยอดรวมการสั่งสินค้าของลูกค้าแต่ละคน
19
Map-Reduce
• มี 2 Operator หลัก คือ Map และ Reduce
• Map - จับคู่ระหว่าง Key-Value
var mapFunction = function() {
emit(this.cust_id, this.price);
};
• Reduce - รวบรวมค่า Value ของแต่ละ Key
จนเหลืออันเดียว
var reduceFunction = function(keyCustId, valuesPrices)
{
return Array.sum(valuesPrices); 20
Map-Reduce
• orders เป็นชื่อ collection
• เรียกด้วยฟังก์ชัน mapReduce ( )
• นำาผลลัพธ์ไปเก็บที่ collection ชื่อ
map_reduce_example
db.orders.mapReduce(
mapFunction,
reduceFunction,
{ out: "map_reduce_example" }
)
21
เอกสารอ้างอิง
• http://horicky.blogspot.com/2012/04/mongodb-
architecture.html
• http://www.slideshare.net/mdirolf/inside-
mongodb-the-internals-of-an-opensource-
database
• http://docs.mongodb.org/manual/replication/
• http://docs.mongodb.org/manual/sharding/
• http://docs.mongodb.org/manual/applications/ma
p-reduce/ 22

Weitere ähnliche Inhalte

Empfohlen

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Empfohlen (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

Basic MongoDB