SlideShare a Scribd company logo
1 of 17
Download to read offline
50177004-Big Data Analytics-Thinnaphat Borirak
สําหรับการเรียนรู้ ออกแบบ และจัดการฐานข้อมูล NoSQL ด้วย MongoDB
ทินภัทร บริรักษ์
https://www.mongodb.com
https://www.tutorialspoint.com/mongodb/index.htm
เนือหาเกียวกับการสอน
MongoDB เป็นฐานข้อมูลเอกสาร (Document Database) โอเพ่นซอร์ส และฐานข้อมูล NoSQL ชันนํา
MongoDB ถูกพัฒนาด้วยภาษา C++ ซึงเนือหาในเอกสารฉบับนีจะช่วยให้คุณเข้าใจแนวคิดของ MongoDB ทีจําเป็นใน
การสร้างและปรับใช้ฐานข้อมูลทีมีประสิทธิภาพ อีกทังยังเหมาะกับข้อมูลขนาดใหญ่ ทีไม่ซับซ้อน การทํางานทีไม่หนัก
มาก สามารถทํางานกับระบบทีเป็นการทํางานแบบเรียลไทม์ (Real Time)
โดยเนือหานีออกแบบมาสําหรับผู้เชียวชาญด้านซอฟต์แวร์ทีต้องการเรียนรู้ฐานข้อมูล MongoDB ในขันตอนที
ง่ายและสะดวก ช่วยให้คุณเข้าใจแนวคิดของ MongoDB เป็นอย่างดี และหลังจากจบคุณจะมีความเชียวชาญใน
ระดับกลางซึงคุณสามารถพัฒนาตัวเองไปสู่ระดับความเชียวชาญทีสูงขึนได้
ข้อกําหนดเบืองต้น
ก่อนดําเนินการสอนนี คุณควรมีความเข้าใจพืนฐานเกียวกับฐานข้อมูล โปรแกรมแก้ไขข้อความ และการทํางานของ
โปรแกรม ฯลฯ เนืองจากเรากําลังพัฒนาฐานข้อมูลประสิทธิภาพสูง ดังนันจะเป็นการดีหากคุณมีความเข้าใจเกียวกับ
แนวคิดพืนฐานของฐานข้อมูล (RDBMS).
Background: ฐานข้อมูลเชิงสัมพันธ์ (Relational Database)
 เป็นฐานข้อมูลทีใช้ภาษา Structure Query Language: SQL ดําเนินการกับข้อมูล Insert, Update, Delete, Search
 มีรูปแบบการจัดเก็บข้อมูลแบบตาราง
 กําหนดโครงสร้างการจัดเก็บข้อมูลทีชัดเจน (Structure Data)
 มีการเชือมโยงความสัมพันธ์กับข้อมูลในตารางอืนๆ
 การปรับขนาด ในกรณีเมือฐานข้อมูลมีการจัดเก็บในปริมาณมาก และอนาคตมีการปรับขนาดฐานข้อมูลให้ใหญ่ขึน
ฐานข้อมูลเชิงสัมพันธ์ จะเป็นการปรับขนาดในแนวตัง (Vertical-Scaling) โดยติดตังอุปกรณ์เพิมเติม เช่น CPU,
RAM, Storage ให้มีประสิทธิภาพสูงขึน ส่งผลให้มีค่าใช้จ่ายทีสูงขึนตามไปด้วย อีกทังยังส่งผลกระทบจากการงด
ให้บริการชัวคราวขณะติดตังอุปกรณ์เหล่านัน
ปัจจุบันได้มีการพัฒนาทีหลากหลาย มีเทคโนโลยีฐานข้อมูลต่างๆ มากมาย คราวนีก็จะมาดูในส่วนของ
เทคโนโลยีทาสนับสนุนการใช้งานฐานข้อมูล NoSQL ในหลายรูปแบบ เช่น HBase Column structure, Redis
Key/Values เป็นต้น ในเอกสารชุดนี เราจะพูดถึง MongoDB เนืองจากเป็นฐานข้อมูล NoSQL ทีได้รับความนิยมและ
รองรับมากทีสุดในอันดับที 5 ซึงปัจจุบันมีฐานข้อมูล NoSQL อยู่ 4 ประเภท ได้แก่
1. Document Databases
ฐานข้อมูลเอกสาร มักใช้วิธีเชิงเอกสารในการจัดเก็บข้อมูล แนวคิดหลักอยู่ทีเบืองหลัง โดยอาศัยการจัดการ
กับข้อมูลทังหมดเป็นเอนทิตีเดียว เลยจําเป็นต้องจัดเก็บในรูปแบบเอกสาร และเอกสารทังหมดนันสามารถจัดเก็บไว้
ด้วยกันได้ภายในคอลเลกชัน อีกทังเอกสารนีสามารถจัดเก็บข้อมูลเป็นเอกสารย่อยๆ ได้ ซึงโดยทัวไปแล้วใน RDBMS
จะจัดเก็บเป็นชุดข้อความทีทําการเข้ารหัสไว้ หรือเก็บอยู่ภายในตารางทีแยกออกจากกัน โดยเอกสารแต่ละฉบับ
สามารถเข้าถึงได้ด้วยรหัสเฉพาะทีเรียกว่า Unique Key
2
50177004-Big Data Analytics-Thinnaphat Borirak
2. Key-Value Database
ในหมวดหมู่ฐานข้อมูล NoSQL การจัดเก็บ Key-Value เป็นวิธีทีง่ายทีสุด ฐานข้อมูลเหล่านีจัดเก็บข้อมูลใน
ลักษณะทีไม่มีแบบแผน โดย Key จะสามารถชีไปทีข้อมูลประเภทใดก็ได้ เช่น Object, String หรือข้อมูลประเภทอืนๆ
ข้อได้เปรียบหลักของฐานข้อมูลเหล่านีคือง่ายต่อการติดตังและเพิมข้อมูลเข้าไป สามารถดึงข้อมูลจากฐานข้อมูลเหล่านี
ได้ด้วยจากการค้นหาของค่าคีย์ แต่ข้อเสียเปรียบหลักคือเราไม่สามารถหาองค์ประกอบใดๆ ตามค่าทีเก็บไว้ได้ ซึงเรา
จําเป็นทีจะต้องค้นหาข้อมูลตามชือของคีย์เสมอ
3. Column Store Database
ฐานข้อมูลประเภทนีเก็บข้อมูลในคอลัมน์ภายในคีย์สเปซ (Key Space) คีย์สเปซ ถูกกําหนดด้วย Name,
Value and Timestamp ทีไม่ซํากันเสมอ ซึงค่อนข้างคล้ายกับฐานข้อมูลในแบบที 2
4. Graph Store Database
ฐานข้อมูลประเภทนีได้รับการออกแบบมาสําหรับข้อมูลทีสามารถแสดงเป็นข้อมูลกราฟได้ง่าย ซึงหมายความ
ว่าข้อมูลจะเชือมโยงกับความสัมพันธ์ของข้อมูลจํานวนไม่แน่นอน เช่น ความสัมพันธ์ในครอบครัวและสังคม เป็นต้น
MongoDB Overview
MongoDB เป็นฐานข้อมูลเชิงเอกสาร (Document Store) ข้ามแพลตฟอร์มทีให้ประสิทธิภาพสูง ความพร้อมใช้งานสูง
และปรับขนาดได้ง่าย MongoDB works on concept of collection and document.
ภาพแสดงการเปรียบเทียบของโครงสร้างฐานข้อมูล RDBMS กับ MongoDB
- Database เป็นส่วนทีใช้เก็บ Collection หรือชุดข้อมูล
- Collection หรือชุดข้อมูลเทียบได้กับตารางข้อมูลเชิงสัมพันธ์
- Document เอกสารทีจัดเก็บข้อมูลของคู่คีย์ (Key) และค่า (Value)
ตารางแสดงความสัมพันธ์ของคําศัพท์ RDBMS กับ MongoDB
RDBMS MongoDB
Database Database
Table Collection
Tuple / Row Document or BSON Document
Column Field
Table Join Embedded Documents and Linking
Primary Key Primary Key (Default key _id provided by MongoDB itself)
Database Server and Client
Mysqld / Oracle mongod
Mysql / sqlplus mongo
3
50177004-Big Data Analytics-Thinnaphat Borirak
การจัดเก็บข้อมูลแบบ JSON ใน MongoDB
1. เริมเอกสารด้วยการสร้างเครืองหมายปีกกาเปิด-ปิด { }
2. ข้อมูลแต่ละ Field คันด้วยเครืองหมายคอมม่า (,)
3. สามารถสร้างเอกสารย่อยในเอกสารหลักได้โดยการใส่ข้อมูลในเครืองหมายปีกกาเปิด-ปิด ตามตัวอย่างดังนี
{
_id:”1234567890t995a”,
name: ”Abusayard TK Limon”,
email: “abusa@example.com”,
course: {
course_name: “MongoDB”
}
}
*** ในฐานข้อมูล MongoDB จะทําการแปลงเอกสาร JSON โดยเข้ารหัสให้อยู่ในรูปแบบ Binary หรือทีเรียกว่า Binary
JSON: BSON เพือขยายความสามารถให้ JSON มีชนิดข้อมูลเพิมมากขึน มีการเข้ารหัส-ถอดรหัสทีด้วยภาษาที
แตกต่างกัน
Example: Shows the document structure of a blog site which is simply a comma separated key value pair.
{
_id: ObjectId(1ab23cd4567e)
title: “MongoDB Overview”,
description: “MongoDB is NoSQL Database”,
by: “Top”,
email: “thinnaphat.bo@gmail.com”,
tags: [“mongodb”, “database”, “NoSQL”],
likes: 100,
comments:
[
{
user:”adam”,
message: “Adam_first comment”,
dateCreated: new Date(2023,1,20,2,15),
like: 0
},
{
user:”Tony”,
message: “Tony_first comment”,
dateCreated: new Date(2023,1,21,2,15),
like: 3
}
]
}
NOTE
_id คือ เลขฐานสิบหกขนาด 12 ไบต์ซึงรับประกันความเป็นเอกลักษณ์ของเอกสารทุกฉบับ โดยสามารถระบุ _id ขณะ
ใส่เอกสารได้ หากไม่ได้ทําการระบุ โปรแกรมของ MongoDB จะให้รหัสเฉพาะสําหรับทุกเอกสารเอง ซึงจะมีทังหมด 12
4
50177004-Big Data Analytics-Thinnaphat Borirak
ไบต์ - 4 ไบต์แรก สําหรับการระบุเวลาปัจจุบัน 3 ไบต์ถัดไปสําหรับรหัสเครือง 2 ไบต์ถัดไปสําหรับรหัสกระบวนการ
ของเซิร์ฟเวอร์ MongoDB และ 3 ไบต์ทีเหลือเป็นค่าทีเพิมขึนแบบ incremental Value
MongoDB Advantages
Any Relational Database has a typical schema design that shows number of tables and the relationship
between these tables. While in MongoDB there is no concept of relationship
Northwind Database
MongoDB Database
ข้อดีของ MongoDB Over RDBMS
 Schema less: MongoDB เป็นฐานข้อมูลเอกสารทีคอลเลกชันหนึงเก็บเอกสารทีแตกต่างกัน จํานวนฟิลด์ เนือหา
และขนาดของเอกสารอาจแตกต่างกันไปในแต่ละเอกสาร
 โครงสร้างของวัตถุชินเดียวมีความชัดเจน
 ไม่มีการรวมทีซับซ้อน
 ความสามารถในการค้นหาเชิงลึก MongoDB รองรับการสืบค้นแบบไดนามิกบนเอกสารโดยใช้ภาษาการสืบค้นตาม
เอกสาร ซึงเกือบจะมีประสิทธิภาพเทียบเท่ากับ SQL
 การปรับแต่ง MongoDB ปรับขนาดได้ง่าย
 ไม่จําเป็นต้อง Conversion หรือ mapping of application objects to database objects
 ใช้หน่วยความจําภายในสําหรับจัดเก็บชุดการทํางาน ทําให้เข้าถึงข้อมูลได้เร็วขึน
5
50177004-Big Data Analytics-Thinnaphat Borirak
Why should use MongoDB
 Document Oriented Storage : Data is stored in the form of JSON style documents
 Index on any attribute
 Replication & High Availability
 Auto-Sharing
 Rich Queries
 Fast In-Place Updates
 Professional Support By MongoDB
Where should use MongoDB?
 Big Data
 Content Management and Delivery
 Mobile and Social Infrastructure
 User Data Management
 Data Hub (ศูนย์กลางข้อมูลคือศูนย์กลางของการแลกเปลียนข้อมูลทีได้รับการสนับสนุนจากวิทยาศาสตร์ข้อมูล
วิศวกรรมข้อมูล และเทคโนโลยีคลังข้อมูล เพือโต้ตอบกับปลายทาง เช่น แอปพลิเคชันและอัลกอริทึม)
ตัวอย่างการนําข้อมูลไปใช้ในภาคอุตสาหกรรมรถยนต์
โดยทัวไปแล้วการจัดการกลุ่มยานพาหนะ ก็มีข้อมูลต่างๆทีเกียวข้องมากมาย ขึนอยู่กับวิธีทีบริษัทฯ นัน
จัดการและใช้ข้อมูล อาทิเช่น กระบวนการเพือรวบรวมข้อมูล กระบวนการในการนําข้อมูลไปวิเคราะห์ และใช้ประโยชน์
จากข้อมูลให้เกิดประโยชน์สูงสุด อย่าง Optimum Automotive ซึงได้พัฒนา DATA HUB ทีไม่เหมือนใครโดยใช้อัลกอริทึม
อัจฉริยะ (Smart Algorithms) ในการออกแบบและพัฒนาให้เกิดความก้าวหน้าทางด้านอุตสาหกรรมยานยนต์ดังนี
ทีมา: https://www.optimum-automotive.com/?-data-hub_fleet_managment-
Assignment: ยกตัวอย่างพร้อมเหตุผลประกอบในการนําข้อมูลมาใช้เพือให้เกิดประโยชน์
Assignment: คุณลองเข้าไปดูรายละเอียดของการใช้งานฐานข้อมูลในรูปแบบต่างๆ ในปีทีผ่านมา Most Popular
Databases In The World (2023) แล้วตอบคําถามว่าฐานข้อมูลทีบริษัทฯ หรือหน่วยงานทีสังกัดอยู่นัน ใช้ระบบ
ฐานข้อมูลใด เพราะเหตุใด มีแนวโน้มทีจะปรับเปลียนหรือไม่ อย่างไร
6
50177004-Big Data Analytics-Thinnaphat Borirak
Workshop: การติดตัง MongoDB
MongoDB (https://www.mongodb.com/)
หากต้องการติดตัง MongoDB ก่อนอืนให้ดาวน์โหลด MongoDB รุ่นล่าสุด โดยสามารถเข้าไป Download โปรแกรมเพือ
ใช้ในการจัดการได้ที https://www.mongodb.com/try/download/community ซึงผมจะติดตังโปรแกรมบน
ระบบปฏิบัติการ Windows 64 bit และควรตรวจสอบให้แน่ใจว่า MongoDB เวอร์ชันทีใช้สามารถรันได้ผ่าน
ระบบปฏิบัติการของเราด้วยครับ
* สามารถพิมพ์คําสังทีใช้ในการตรวจสอบระบบปฏิบัติการได้ดังนี C:>wmic os get osarchitecture
Link Download Program:
https://drive.google.com/file/d/1AuSykawONILZ8e_2JzGYhgn3imDSoZXT/view?usp=sharing
สําหรับบุคคลทีต้องการติดตังบนระบบปฏิบัติการอืน อย่างเช่น Ubuntu ก็สามารถเข้าไปทีลิงค์นีได้
https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/
เครืองมือพืนฐานในการใช้งาน
- MongoDB Community เป็น MongoDB Server รุ่น Open Source
- MongoDB Compass เป็นเครืองมือในการจัดเก็บข้อมูลในรูปแบบ GUI ในการอํานวยความสะดวกในการวิเคราะห์
ข้อมูล การดําเนินการเก็บข้อมูล (CRUD) รวมทังสืบค้นและจัดการ Index เป็นต้น
7
50177004-Big Data Analytics-Thinnaphat Borirak
ขันตอนการติดตังโปรแกรม
8
50177004-Big Data Analytics-Thinnaphat Borirak
ให้ลองทดสอบการ Connect ผ่าน Browser เพือเข้าใช้งาน mongodb://localhost:27017
เอกสารประกอบการศึกษาหลัก: https://www.mongodb.com/docs/
ทําความเข้าใจกับ MongoDB
จากการอภิปรายในบทความแนะนํา เราพบว่าข้อมูลใน MongoDB มี Schema ทียืดหยุ่น ใน MongoDB เราไม่
จําเป็นต้องประกาศคอลัมน์ใหม่ของ Collection ก่อนทีจะแทรกข้อมูลในฐานข้อมูล เหมือนกับแนวคิด RDBMS ทัวไป
ดังนันจึงมีข้อควรพิจารณาพืนฐานบางประการทีเราต้องจําไว้ในขณะทีออกแบบ Schema ใน MongoDB
1. ต้องออกแบบ schema ตามความต้องการของผู้พัฒนา
2. หากเราใช้ Object ตังแต่ 2 Object ขึนไป จําเป็นต้องรวมเป็น Object เดียว
3. เวลาเขียนข้อมูลสามารถทําการรวมข้อมูลตอนทีเขียนพร้อมกันได้
4. สามารถเพิมประสิทธิภาพ Schema สําหรับการค้นหาข้อมูลทีบ่อยทีสุดได้
5. สามารถทําหน้าทีรวมข้อมูลในเรืองของ Aggregation Functions ได้
9
50177004-Big Data Analytics-Thinnaphat Borirak
What are the Documents in MongoDB
Document เป็นหน่วยพืนฐานของข้อมูลใน MongoDB ซึงโดยทัวไปเทียบเท่ากับแถวตามหลักการออกแบบ
แนวคิดของระบบ RDBMS เปรียบได้ว่า Document คือ หัวใจหลักของ MongoDB นันเอง โดย Document ในทีนีก็ คือ
ชุดคีย์ทีเรียงลําดับกันซึงมีข้อมูลหรือค่าทีเกียวข้องกับการแสดงข้อมูลของ Document เหล่านัน ไม่ว่าจะเป็น Map,
Hash หรือ Dictionary ในกรณีของ JavaScript เอกสารจะแสดงถึงข้อมูลทีเป็น Object เช่นเดียวกับรายละเอียดการใช้
งานทีสําคัญของ MongoDB
Data Types of MongoDB - นันสามารถรองรับประเภทข้อมูลทีหลากหลายได้ในค่าของ Document ซึง MongoDB มัก
ถูกมองว่าเป็นข้อมูลทีเหมือน JSON ทีเป็น JavaScript Object โดย JSON นันเป็นการแสดงข้อมูลอย่างง่าย ข้อดีของ
JSON คือ ช่วยให้เข้าใจในเรืองของการวิเคราะห์ในแต่ละเฟสและง่ายต่อการจํา นอกจากนี JSON ยังยอมรับค่าเฉพาะ
ข้อมูลทีเป็นประเภท null, Boolean, Number, String, Array และ Object ตามรายการด้านล่างคือประเภทข้อมูลทีมีอยู่
ใน MongoDB
String This is most commonly used datatype to store the data. String in MongoDB must be UTF-8 valid.
Integer This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your
server.
Boolean This type is used to store a Boolean (true/ false) value.
Double This type is used to store floating point values.
Min/Max keys This type is used to compare a value against the lowest and highest BSON elements.
Arrays This type is used to store arrays or list or multiple values into one key.
Date This datatype is used to store the current date or time in UNIX time format. You can specify your
own datetime by creating object of Date and passing day, month, year into it.
Timestamp This can be handy for recording when a document has been modified or added.
Object This datatype is used for embedded documents.
Object ID This datatype is used to store the document’s ID.
Null This type is used to store a Null value.
Symbol This datatype is used identically to a string however, it's generally reserved for languages that use
a specific symbol type.
Binary data This datatype is used to store Binary data.
Code This datatype is used to store JavaScript code into document.
Regular expression This datatype is used to store regular expression
การสร้างแบบจําลองข้อมูล MongoDB
ให้คิดซะว่าข้อมูลใน MongoDB มี schema.documents ทียืดหยุ่นในคอลเล็กชันเดียวกันไม่จําเป็นต้องมีชุดฟิลด์หรือ
โครงสร้างชุดเดียวกัน และฟิลด์ทัวไปในเอกสารของคอลเล็กชันอาจมีข้อมูลประเภทต่างๆ กัน
ข้อควรพิจารณาบางประการขณะออกแบบสคีมา (Designing Schema) ใน MongoDB
 ออกแบบสคีมาตามความต้องการของผู้ใช้
 รวม Object เป็นเอกสารเดียวกันถ้าหากต้องการใช้ร่วมกัน แต่ถ้าไม่ควรให้แยกออกจากกัน
 ทําสําเนาข้อมูลให้สมําเสมอ ให้คิดว่าพืนทีในการจัดเก็บเทียบกับเวลาทีใช้ในการคํานวณไม่ได้
 ปรับสคีมาให้เหมาะสมสําหรับกรณีการใช้งานส่วนทีใช้บ่อยทีสุด
10
50177004-Big Data Analytics-Thinnaphat Borirak
Example: สมมติว่าลูกค้าต้องการการออกแบบฐานข้อมูลสําหรับเว็บไซต์บล็อกของเขา และให้ดูความแตกต่างระหว่าง
RDBMS และการออกแบบสคีมาของ MongoDB โดยเว็บไซต์นันจะมีข้อกําหนดดังต่อไปนี
 ทุกโพสต์จะมีชือ คําอธิบาย และ URL ทีไม่ซํากัน
 ทุกโพสต์สามารถมีหนึงแท็กหรือมากกว่า
 ทุกโพสต์มีชือผู้เผยแพร่และจํานวนไลค์ทังหมด
 ทุกโพสต์จะมีความคิดเห็นทีผู้ใช้ให้ไว้พร้อมกับชือ ข้อความ ข้อมูลเวลา และยอดไลค์
 ในแต่ละโพสต์อาจมีความคิดเห็นเป็นศูนย์หรือมากกว่าก็ได้
ในการออกแบบ Schema-RDBMS สําหรับข้อกําหนดข้างต้นจะมีตารางอย่างน้อย 3 ตาราง ดังนี
ในขณะทีการออกแบบ Schema ของ MongoDB จะมีหนึงโพสต์ทีเป็น Collection และมีโครงสร้างดังต่อไปนี
{ _id: POST_ID
title: TITLE_OF_POST,
description: POST_DESCRIPTION,
by: POST_BY,
url: URL_OF_POST,
tags: [TAG1, TAG2, TAG3],
likes: TOTAL_LIKES,
comments: [ {
user:'COMMENT_BY',
message: TEXT,
dateCreated: DATE_TIME,
like: LIKES
},
{
user:'COMMENT_BY',
message: TEXT,
dateCreated: DATE_TIME,
like: LIKES
} ]
}
ดังนันในขณะทีแสดงข้อมูลใน RDBMS จะต้องทําการ Join สามตาราง ส่วนข้อมูลใน MongoDB จะแสดงเพียงแค่
Collection เดียวเท่านัน
ข้อเสนอแนะ ให้เขียนคําสัง SQL ในการสร้างฐานข้อมูลเพือใช้ในการจัดเก็บข้อมูลสําหรับเว็บไซต์บล็อกของลูกค้า
ตามตัวอย่างทีกําหนดให้ พร้อมทดสอบการเพิมชุดข้อมูลการ Post และ Comment ลงในฐานข้อมูล จากนันเขียนคําสัง
SQL ในการแสดงรายการทีได้จากการจัดเก็บข้อมูลของเว็บบล็อกนัน
11
50177004-Big Data Analytics-Thinnaphat Borirak
Workshop: Create Database
The use Command
MongoDB use DATABASE_NAME is used to create database. The command will create a new database, if it
doesn't exist otherwise it will return the existing database.
Syntax:
Basic syntax of use DATABASE statement is as follows:
use DATABASE_NAME
Example:
If you want to create a database with name <mydb>, then use DATABASE statement would be as follows:
>use mydb
switched to db mydb
To check your currently selected database use the command db
>db
mydb
If you want to check your databases list, then use the command show dbs.
>show dbs
admin 40.00 KiB
config 60.00 KiB
local 72.00 KiB
Your created database (mydb) is not present in list. To display database you need to insert atleast one
document into it.
>db.movie.insert({"name":"Avatar"})
>show dbs
admin 40.00 KiB
config 108.00 KiB
local 72.00 KiB
mydb 40.00 KiB
* In mongodb default database is test. If you didn't create any database then collections will be stored in test
database.
Example: Try writing a command to add two more movie titles.
>
>
12
50177004-Big Data Analytics-Thinnaphat Borirak
Workshop: Drop Database
The dropDatabase () Method
MongoDB db.dropDatabase () command is used to drop a existing database.
Syntax:
Basic syn tax of dropDatabase () command is as follows:
db.dropDatabase()
This will delete the selected database. If you have not selected any database, then it will delete default 'test'
database
Example:
First, check the list available databases by using the command show dbs
>show dbs
admin 40.00 KiB
config 108.00 KiB
local 72.00 KiB
mydb 40.00 KiB
If you want to delete new database <mydb>, then dropDatabase() command would be as follows:
>use mydb
switched to db mydb
>db.dropDatabase()
{ ok: 1, dropped: 'mydb' }
Now check list of databases
>show dbs
admin 40.00 KiB
config 108.00 KiB
local 72.00 KiB
13
50177004-Big Data Analytics-Thinnaphat Borirak
Workshop: Create Collection
The createCollection() Method
MongoDB db.createCollection(name, options) is used to create collection.
Syntax:
Basic syntax of createCollection() command is as follows
db.createCollection(name, options)
In the command, name is name of collection to be created. Options is a document and used to specify
configuration of collection
Parameter Type Description
Name String Name of the collection to be created
Options Document (Optional) Specify options about memory size and indexing
Options parameter is optional, so you need to specify only name of the collection. Following is the list of options
you can use:
Field Type Description
capped Boolean (Optional) If true, enables a capped collection. Capped collection is a collection
fixed size collection that automatically overwrites its oldest entries when it reaches
its maximum size. If you specify true, you need to specify size parameter also.
autoIndexId Boolean (Optional) If true, automatically create index on _id field.s Default value is false.
size number (Optional) Specifies a maximum size in bytes for a capped collection. If capped is
true, then you need to specify this field also.
max number (Optional) Specifies the maximum number of documents allowed in the capped
collection.
Examples:
Basic syntax of createCollection() method without options is as follows
>use mydb
'switched to db mydb'
>db.createCollection("mycollection")
{ ok: 1 }
You can check the created collection by using the command show collections
>show collections
mycollection
14
50177004-Big Data Analytics-Thinnaphat Borirak
Following example shows the syntax of createCollection() method with few important options:
>db.createCollection("mycollection_option",{capped:true,autoIndexId:true,size:6142800,max:10000})
{ ok: 1 }
In MongoDB you don't need to create collection. MongoDB creates collection automatically, when you insert
some document.
>db.mycollection_50177004.insert({"name":"Big Data Analytics"})
>show collections
mycollection
mycollection_50177004
mycollection_option
Note: Rename Collection >db.collection_name.renameCollection("collection_New_name")
Workshop: Drop Collection
The drop() Method
MongoDB's db.collection.drop() is used to drop a collection from the database.
Syntax:
Basic syntax of drop() command is as follows
db.COLLECTION_NAME.drop()
Example:
First, check the available collections into your database mydb
> use mydb
'already on db mydb'
>show collections
mycollection
mycollection_50177004
mycollection_option
Now drop the collection with the name mycollection
> db.mycollection.drop()
true
drop() method will return true, if the selected collection is dropped successfully otherwise it will return false
Again check the list of collections into database
> show collections
mycollection_50177004
15
50177004-Big Data Analytics-Thinnaphat Borirak
mycollection_option
Example: drop the collection with the name mycollection_50177004 and mycollection_option
>
>
Now check the list of collections into database
>
Now check list of databases
>
Why is there no database named mydb?
Workshop: Insert Document
The insert() Method
To insert data into MongoDB collection, you need to use MongoDB's insert() or save()method.
Syntax
Basic syntax of insert() command is as follows:
>db.COLLECTION_NAME.insert(document)
Example
>db.mycollection.insert({
title: 'MongoDB Insert() Method',
description: 'MongoDB is NoSQL Database',
by: 'Abusayard TK Limon',
url: 'https://www.mongodb.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
Example
>db.post.insert( [
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'Abusayard TK Limon',
url: 'https://www.mongodb.com',
16
50177004-Big Data Analytics-Thinnaphat Borirak
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
title: 'NoSQL Database',
description: 'NoSQL database does not have tables',
by: 'Thinnaphat',
email: 'thinnaphat.bo@gmail.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 30,
comments: [
{
user:'Adam',
message: 'Adam first comment',
dateCreated: new Date(2023,01,28,2,35),
like: 0
} ]
}
] )
Note: InsertOne - db.Collection_name.insertOne( <Document> )
InsertMany - db.Collection_name.insertMany( [ <Document> ] )
Workshop: Basic Query Document
The find() Method
To query data from MongoDB collection, you need to use MongoDB's find() method.
Syntax
Basic syntax of find() method is as follows
>db.COLLECTION_NAME.find()
find() method will display all the documents in a non structured way.
Example
>db.post.find()
>db.post.find({"title":"MongoDB Overview"})
The pretty() Method
To display the results in a formatted way, you can use pretty() method.
Syntax:
>db.mycolection.find().pretty()
17
50177004-Big Data Analytics-Thinnaphat Borirak
Example
>db.mycollection.find().pretty( {
title: 'MongoDB Insert() Method',
description: 'MongoDB is NoSQL Database',
by: 'Abusayard TK Limon',
url: 'https://www.mongodb.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
} )
Assignment: ให้ทําการออกแบบฐานข้อมูลเพือใช้ในการจัดเก็บข้อมูลของสภาพภูมิอากาศ โดยใช้ตัวอย่างคําสังในการ
สร้าง Schema ทีให้ จากนันลองเพิมข้อมูล พร้อมทําความเข้าใจหลักการทํางานนันว่ามีรูปแบบอย่างไร
https://www.mongodb.com/docs/manual/core/timeseries/timeseries-procedures/

More Related Content

Similar to Introduction-Management NoSQL with MongoDB.pdf

Data processing
Data processingData processing
Data processingchukiat008
 
บทที่ 2 (21 02-56) okรันเลขแล้ว(แก้เหมายเลขหน้าด้วย ตั้งแต่หน้า 9 เป็นต้นไป )...
บทที่ 2 (21 02-56) okรันเลขแล้ว(แก้เหมายเลขหน้าด้วย ตั้งแต่หน้า 9 เป็นต้นไป )...บทที่ 2 (21 02-56) okรันเลขแล้ว(แก้เหมายเลขหน้าด้วย ตั้งแต่หน้า 9 เป็นต้นไป )...
บทที่ 2 (21 02-56) okรันเลขแล้ว(แก้เหมายเลขหน้าด้วย ตั้งแต่หน้า 9 เป็นต้นไป )...Sarawut Panchon
 
Database basic new
Database basic newDatabase basic new
Database basic newQoo Kratai
 
บทความ Big Data จากบล็อก thanachart.org
บทความ Big Data จากบล็อก thanachart.orgบทความ Big Data จากบล็อก thanachart.org
บทความ Big Data จากบล็อก thanachart.orgIMC Institute
 
การบริการสารสนเทศบนเว็บด้วย OSS : Joomla Workshop
การบริการสารสนเทศบนเว็บด้วย OSS : Joomla Workshopการบริการสารสนเทศบนเว็บด้วย OSS : Joomla Workshop
การบริการสารสนเทศบนเว็บด้วย OSS : Joomla WorkshopSatapon Yosakonkun
 
นุ๊ก
นุ๊กนุ๊ก
นุ๊กsirinet
 
หมวย
หมวยหมวย
หมวยsirinet
 
ดรีม
ดรีมดรีม
ดรีมsirinet
 
หมวย
หมวยหมวย
หมวยsirinet
 
หวิว
หวิวหวิว
หวิวViewMik
 
รออกแบบเว็บไซต์
รออกแบบเว็บไซต์รออกแบบเว็บไซต์
รออกแบบเว็บไซต์sirinet
 
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์แบบเสนอโครงร่างโครงงานคอมพิวเตอร์
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์Supaporn Pakdeemee
 
งานคอม หน่วยที่1
งานคอม หน่วยที่1งานคอม หน่วยที่1
งานคอม หน่วยที่1Ruttikan Munkhan
 
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์ ครูสมร
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์ ครูสมรแบบเสนอโครงร่างโครงงานคอมพิวเตอร์ ครูสมร
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์ ครูสมรอภิวัฒน์ ปานกลาง
 

Similar to Introduction-Management NoSQL with MongoDB.pdf (20)

Data processing
Data processingData processing
Data processing
 
บทที่ 2 (21 02-56) okรันเลขแล้ว(แก้เหมายเลขหน้าด้วย ตั้งแต่หน้า 9 เป็นต้นไป )...
บทที่ 2 (21 02-56) okรันเลขแล้ว(แก้เหมายเลขหน้าด้วย ตั้งแต่หน้า 9 เป็นต้นไป )...บทที่ 2 (21 02-56) okรันเลขแล้ว(แก้เหมายเลขหน้าด้วย ตั้งแต่หน้า 9 เป็นต้นไป )...
บทที่ 2 (21 02-56) okรันเลขแล้ว(แก้เหมายเลขหน้าด้วย ตั้งแต่หน้า 9 เป็นต้นไป )...
 
Database basic new
Database basic newDatabase basic new
Database basic new
 
บทความ Big Data จากบล็อก thanachart.org
บทความ Big Data จากบล็อก thanachart.orgบทความ Big Data จากบล็อก thanachart.org
บทความ Big Data จากบล็อก thanachart.org
 
Yuu
YuuYuu
Yuu
 
Ten
TenTen
Ten
 
New
NewNew
New
 
การบริการสารสนเทศบนเว็บด้วย OSS : Joomla Workshop
การบริการสารสนเทศบนเว็บด้วย OSS : Joomla Workshopการบริการสารสนเทศบนเว็บด้วย OSS : Joomla Workshop
การบริการสารสนเทศบนเว็บด้วย OSS : Joomla Workshop
 
Google bigtableappengine
Google bigtableappengineGoogle bigtableappengine
Google bigtableappengine
 
นุ๊ก
นุ๊กนุ๊ก
นุ๊ก
 
โบ
โบโบ
โบ
 
Best
BestBest
Best
 
หมวย
หมวยหมวย
หมวย
 
ดรีม
ดรีมดรีม
ดรีม
 
หมวย
หมวยหมวย
หมวย
 
หวิว
หวิวหวิว
หวิว
 
รออกแบบเว็บไซต์
รออกแบบเว็บไซต์รออกแบบเว็บไซต์
รออกแบบเว็บไซต์
 
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์แบบเสนอโครงร่างโครงงานคอมพิวเตอร์
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์
 
งานคอม หน่วยที่1
งานคอม หน่วยที่1งานคอม หน่วยที่1
งานคอม หน่วยที่1
 
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์ ครูสมร
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์ ครูสมรแบบเสนอโครงร่างโครงงานคอมพิวเตอร์ ครูสมร
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์ ครูสมร
 

More from NETsolutions Asia: NSA – Thailand, Sripatum University: SPU

More from NETsolutions Asia: NSA – Thailand, Sripatum University: SPU (12)

Managing Big Data with Apache Hadoop.pdf
Managing Big Data with Apache Hadoop.pdfManaging Big Data with Apache Hadoop.pdf
Managing Big Data with Apache Hadoop.pdf
 
Les12 creating views
Les12 creating viewsLes12 creating views
Les12 creating views
 
Les09 Manipulating Data
Les09 Manipulating DataLes09 Manipulating Data
Les09 Manipulating Data
 
Les04 Displaying Data From Multiple Table
Les04 Displaying Data From Multiple TableLes04 Displaying Data From Multiple Table
Les04 Displaying Data From Multiple Table
 
Les11 Including Constraints
Les11 Including ConstraintsLes11 Including Constraints
Les11 Including Constraints
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
 
Les02 Restricting And Sorting Data
Les02 Restricting And Sorting DataLes02 Restricting And Sorting Data
Les02 Restricting And Sorting Data
 
Les00 Intoduction
Les00 IntoductionLes00 Intoduction
Les00 Intoduction
 
Les06 Subqueries
Les06 SubqueriesLes06 Subqueries
Les06 Subqueries
 
Les05 Aggregating Data Using Group Function
Les05 Aggregating Data Using Group FunctionLes05 Aggregating Data Using Group Function
Les05 Aggregating Data Using Group Function
 
Les01 Writing Basic Sql Statements
Les01 Writing Basic Sql StatementsLes01 Writing Basic Sql Statements
Les01 Writing Basic Sql Statements
 
Les03 Single Row Function
Les03 Single Row FunctionLes03 Single Row Function
Les03 Single Row Function
 

Introduction-Management NoSQL with MongoDB.pdf

  • 1. 50177004-Big Data Analytics-Thinnaphat Borirak สําหรับการเรียนรู้ ออกแบบ และจัดการฐานข้อมูล NoSQL ด้วย MongoDB ทินภัทร บริรักษ์ https://www.mongodb.com https://www.tutorialspoint.com/mongodb/index.htm เนือหาเกียวกับการสอน MongoDB เป็นฐานข้อมูลเอกสาร (Document Database) โอเพ่นซอร์ส และฐานข้อมูล NoSQL ชันนํา MongoDB ถูกพัฒนาด้วยภาษา C++ ซึงเนือหาในเอกสารฉบับนีจะช่วยให้คุณเข้าใจแนวคิดของ MongoDB ทีจําเป็นใน การสร้างและปรับใช้ฐานข้อมูลทีมีประสิทธิภาพ อีกทังยังเหมาะกับข้อมูลขนาดใหญ่ ทีไม่ซับซ้อน การทํางานทีไม่หนัก มาก สามารถทํางานกับระบบทีเป็นการทํางานแบบเรียลไทม์ (Real Time) โดยเนือหานีออกแบบมาสําหรับผู้เชียวชาญด้านซอฟต์แวร์ทีต้องการเรียนรู้ฐานข้อมูล MongoDB ในขันตอนที ง่ายและสะดวก ช่วยให้คุณเข้าใจแนวคิดของ MongoDB เป็นอย่างดี และหลังจากจบคุณจะมีความเชียวชาญใน ระดับกลางซึงคุณสามารถพัฒนาตัวเองไปสู่ระดับความเชียวชาญทีสูงขึนได้ ข้อกําหนดเบืองต้น ก่อนดําเนินการสอนนี คุณควรมีความเข้าใจพืนฐานเกียวกับฐานข้อมูล โปรแกรมแก้ไขข้อความ และการทํางานของ โปรแกรม ฯลฯ เนืองจากเรากําลังพัฒนาฐานข้อมูลประสิทธิภาพสูง ดังนันจะเป็นการดีหากคุณมีความเข้าใจเกียวกับ แนวคิดพืนฐานของฐานข้อมูล (RDBMS). Background: ฐานข้อมูลเชิงสัมพันธ์ (Relational Database)  เป็นฐานข้อมูลทีใช้ภาษา Structure Query Language: SQL ดําเนินการกับข้อมูล Insert, Update, Delete, Search  มีรูปแบบการจัดเก็บข้อมูลแบบตาราง  กําหนดโครงสร้างการจัดเก็บข้อมูลทีชัดเจน (Structure Data)  มีการเชือมโยงความสัมพันธ์กับข้อมูลในตารางอืนๆ  การปรับขนาด ในกรณีเมือฐานข้อมูลมีการจัดเก็บในปริมาณมาก และอนาคตมีการปรับขนาดฐานข้อมูลให้ใหญ่ขึน ฐานข้อมูลเชิงสัมพันธ์ จะเป็นการปรับขนาดในแนวตัง (Vertical-Scaling) โดยติดตังอุปกรณ์เพิมเติม เช่น CPU, RAM, Storage ให้มีประสิทธิภาพสูงขึน ส่งผลให้มีค่าใช้จ่ายทีสูงขึนตามไปด้วย อีกทังยังส่งผลกระทบจากการงด ให้บริการชัวคราวขณะติดตังอุปกรณ์เหล่านัน ปัจจุบันได้มีการพัฒนาทีหลากหลาย มีเทคโนโลยีฐานข้อมูลต่างๆ มากมาย คราวนีก็จะมาดูในส่วนของ เทคโนโลยีทาสนับสนุนการใช้งานฐานข้อมูล NoSQL ในหลายรูปแบบ เช่น HBase Column structure, Redis Key/Values เป็นต้น ในเอกสารชุดนี เราจะพูดถึง MongoDB เนืองจากเป็นฐานข้อมูล NoSQL ทีได้รับความนิยมและ รองรับมากทีสุดในอันดับที 5 ซึงปัจจุบันมีฐานข้อมูล NoSQL อยู่ 4 ประเภท ได้แก่ 1. Document Databases ฐานข้อมูลเอกสาร มักใช้วิธีเชิงเอกสารในการจัดเก็บข้อมูล แนวคิดหลักอยู่ทีเบืองหลัง โดยอาศัยการจัดการ กับข้อมูลทังหมดเป็นเอนทิตีเดียว เลยจําเป็นต้องจัดเก็บในรูปแบบเอกสาร และเอกสารทังหมดนันสามารถจัดเก็บไว้ ด้วยกันได้ภายในคอลเลกชัน อีกทังเอกสารนีสามารถจัดเก็บข้อมูลเป็นเอกสารย่อยๆ ได้ ซึงโดยทัวไปแล้วใน RDBMS จะจัดเก็บเป็นชุดข้อความทีทําการเข้ารหัสไว้ หรือเก็บอยู่ภายในตารางทีแยกออกจากกัน โดยเอกสารแต่ละฉบับ สามารถเข้าถึงได้ด้วยรหัสเฉพาะทีเรียกว่า Unique Key
  • 2. 2 50177004-Big Data Analytics-Thinnaphat Borirak 2. Key-Value Database ในหมวดหมู่ฐานข้อมูล NoSQL การจัดเก็บ Key-Value เป็นวิธีทีง่ายทีสุด ฐานข้อมูลเหล่านีจัดเก็บข้อมูลใน ลักษณะทีไม่มีแบบแผน โดย Key จะสามารถชีไปทีข้อมูลประเภทใดก็ได้ เช่น Object, String หรือข้อมูลประเภทอืนๆ ข้อได้เปรียบหลักของฐานข้อมูลเหล่านีคือง่ายต่อการติดตังและเพิมข้อมูลเข้าไป สามารถดึงข้อมูลจากฐานข้อมูลเหล่านี ได้ด้วยจากการค้นหาของค่าคีย์ แต่ข้อเสียเปรียบหลักคือเราไม่สามารถหาองค์ประกอบใดๆ ตามค่าทีเก็บไว้ได้ ซึงเรา จําเป็นทีจะต้องค้นหาข้อมูลตามชือของคีย์เสมอ 3. Column Store Database ฐานข้อมูลประเภทนีเก็บข้อมูลในคอลัมน์ภายในคีย์สเปซ (Key Space) คีย์สเปซ ถูกกําหนดด้วย Name, Value and Timestamp ทีไม่ซํากันเสมอ ซึงค่อนข้างคล้ายกับฐานข้อมูลในแบบที 2 4. Graph Store Database ฐานข้อมูลประเภทนีได้รับการออกแบบมาสําหรับข้อมูลทีสามารถแสดงเป็นข้อมูลกราฟได้ง่าย ซึงหมายความ ว่าข้อมูลจะเชือมโยงกับความสัมพันธ์ของข้อมูลจํานวนไม่แน่นอน เช่น ความสัมพันธ์ในครอบครัวและสังคม เป็นต้น MongoDB Overview MongoDB เป็นฐานข้อมูลเชิงเอกสาร (Document Store) ข้ามแพลตฟอร์มทีให้ประสิทธิภาพสูง ความพร้อมใช้งานสูง และปรับขนาดได้ง่าย MongoDB works on concept of collection and document. ภาพแสดงการเปรียบเทียบของโครงสร้างฐานข้อมูล RDBMS กับ MongoDB - Database เป็นส่วนทีใช้เก็บ Collection หรือชุดข้อมูล - Collection หรือชุดข้อมูลเทียบได้กับตารางข้อมูลเชิงสัมพันธ์ - Document เอกสารทีจัดเก็บข้อมูลของคู่คีย์ (Key) และค่า (Value) ตารางแสดงความสัมพันธ์ของคําศัพท์ RDBMS กับ MongoDB RDBMS MongoDB Database Database Table Collection Tuple / Row Document or BSON Document Column Field Table Join Embedded Documents and Linking Primary Key Primary Key (Default key _id provided by MongoDB itself) Database Server and Client Mysqld / Oracle mongod Mysql / sqlplus mongo
  • 3. 3 50177004-Big Data Analytics-Thinnaphat Borirak การจัดเก็บข้อมูลแบบ JSON ใน MongoDB 1. เริมเอกสารด้วยการสร้างเครืองหมายปีกกาเปิด-ปิด { } 2. ข้อมูลแต่ละ Field คันด้วยเครืองหมายคอมม่า (,) 3. สามารถสร้างเอกสารย่อยในเอกสารหลักได้โดยการใส่ข้อมูลในเครืองหมายปีกกาเปิด-ปิด ตามตัวอย่างดังนี { _id:”1234567890t995a”, name: ”Abusayard TK Limon”, email: “abusa@example.com”, course: { course_name: “MongoDB” } } *** ในฐานข้อมูล MongoDB จะทําการแปลงเอกสาร JSON โดยเข้ารหัสให้อยู่ในรูปแบบ Binary หรือทีเรียกว่า Binary JSON: BSON เพือขยายความสามารถให้ JSON มีชนิดข้อมูลเพิมมากขึน มีการเข้ารหัส-ถอดรหัสทีด้วยภาษาที แตกต่างกัน Example: Shows the document structure of a blog site which is simply a comma separated key value pair. { _id: ObjectId(1ab23cd4567e) title: “MongoDB Overview”, description: “MongoDB is NoSQL Database”, by: “Top”, email: “thinnaphat.bo@gmail.com”, tags: [“mongodb”, “database”, “NoSQL”], likes: 100, comments: [ { user:”adam”, message: “Adam_first comment”, dateCreated: new Date(2023,1,20,2,15), like: 0 }, { user:”Tony”, message: “Tony_first comment”, dateCreated: new Date(2023,1,21,2,15), like: 3 } ] } NOTE _id คือ เลขฐานสิบหกขนาด 12 ไบต์ซึงรับประกันความเป็นเอกลักษณ์ของเอกสารทุกฉบับ โดยสามารถระบุ _id ขณะ ใส่เอกสารได้ หากไม่ได้ทําการระบุ โปรแกรมของ MongoDB จะให้รหัสเฉพาะสําหรับทุกเอกสารเอง ซึงจะมีทังหมด 12
  • 4. 4 50177004-Big Data Analytics-Thinnaphat Borirak ไบต์ - 4 ไบต์แรก สําหรับการระบุเวลาปัจจุบัน 3 ไบต์ถัดไปสําหรับรหัสเครือง 2 ไบต์ถัดไปสําหรับรหัสกระบวนการ ของเซิร์ฟเวอร์ MongoDB และ 3 ไบต์ทีเหลือเป็นค่าทีเพิมขึนแบบ incremental Value MongoDB Advantages Any Relational Database has a typical schema design that shows number of tables and the relationship between these tables. While in MongoDB there is no concept of relationship Northwind Database MongoDB Database ข้อดีของ MongoDB Over RDBMS  Schema less: MongoDB เป็นฐานข้อมูลเอกสารทีคอลเลกชันหนึงเก็บเอกสารทีแตกต่างกัน จํานวนฟิลด์ เนือหา และขนาดของเอกสารอาจแตกต่างกันไปในแต่ละเอกสาร  โครงสร้างของวัตถุชินเดียวมีความชัดเจน  ไม่มีการรวมทีซับซ้อน  ความสามารถในการค้นหาเชิงลึก MongoDB รองรับการสืบค้นแบบไดนามิกบนเอกสารโดยใช้ภาษาการสืบค้นตาม เอกสาร ซึงเกือบจะมีประสิทธิภาพเทียบเท่ากับ SQL  การปรับแต่ง MongoDB ปรับขนาดได้ง่าย  ไม่จําเป็นต้อง Conversion หรือ mapping of application objects to database objects  ใช้หน่วยความจําภายในสําหรับจัดเก็บชุดการทํางาน ทําให้เข้าถึงข้อมูลได้เร็วขึน
  • 5. 5 50177004-Big Data Analytics-Thinnaphat Borirak Why should use MongoDB  Document Oriented Storage : Data is stored in the form of JSON style documents  Index on any attribute  Replication & High Availability  Auto-Sharing  Rich Queries  Fast In-Place Updates  Professional Support By MongoDB Where should use MongoDB?  Big Data  Content Management and Delivery  Mobile and Social Infrastructure  User Data Management  Data Hub (ศูนย์กลางข้อมูลคือศูนย์กลางของการแลกเปลียนข้อมูลทีได้รับการสนับสนุนจากวิทยาศาสตร์ข้อมูล วิศวกรรมข้อมูล และเทคโนโลยีคลังข้อมูล เพือโต้ตอบกับปลายทาง เช่น แอปพลิเคชันและอัลกอริทึม) ตัวอย่างการนําข้อมูลไปใช้ในภาคอุตสาหกรรมรถยนต์ โดยทัวไปแล้วการจัดการกลุ่มยานพาหนะ ก็มีข้อมูลต่างๆทีเกียวข้องมากมาย ขึนอยู่กับวิธีทีบริษัทฯ นัน จัดการและใช้ข้อมูล อาทิเช่น กระบวนการเพือรวบรวมข้อมูล กระบวนการในการนําข้อมูลไปวิเคราะห์ และใช้ประโยชน์ จากข้อมูลให้เกิดประโยชน์สูงสุด อย่าง Optimum Automotive ซึงได้พัฒนา DATA HUB ทีไม่เหมือนใครโดยใช้อัลกอริทึม อัจฉริยะ (Smart Algorithms) ในการออกแบบและพัฒนาให้เกิดความก้าวหน้าทางด้านอุตสาหกรรมยานยนต์ดังนี ทีมา: https://www.optimum-automotive.com/?-data-hub_fleet_managment- Assignment: ยกตัวอย่างพร้อมเหตุผลประกอบในการนําข้อมูลมาใช้เพือให้เกิดประโยชน์ Assignment: คุณลองเข้าไปดูรายละเอียดของการใช้งานฐานข้อมูลในรูปแบบต่างๆ ในปีทีผ่านมา Most Popular Databases In The World (2023) แล้วตอบคําถามว่าฐานข้อมูลทีบริษัทฯ หรือหน่วยงานทีสังกัดอยู่นัน ใช้ระบบ ฐานข้อมูลใด เพราะเหตุใด มีแนวโน้มทีจะปรับเปลียนหรือไม่ อย่างไร
  • 6. 6 50177004-Big Data Analytics-Thinnaphat Borirak Workshop: การติดตัง MongoDB MongoDB (https://www.mongodb.com/) หากต้องการติดตัง MongoDB ก่อนอืนให้ดาวน์โหลด MongoDB รุ่นล่าสุด โดยสามารถเข้าไป Download โปรแกรมเพือ ใช้ในการจัดการได้ที https://www.mongodb.com/try/download/community ซึงผมจะติดตังโปรแกรมบน ระบบปฏิบัติการ Windows 64 bit และควรตรวจสอบให้แน่ใจว่า MongoDB เวอร์ชันทีใช้สามารถรันได้ผ่าน ระบบปฏิบัติการของเราด้วยครับ * สามารถพิมพ์คําสังทีใช้ในการตรวจสอบระบบปฏิบัติการได้ดังนี C:>wmic os get osarchitecture Link Download Program: https://drive.google.com/file/d/1AuSykawONILZ8e_2JzGYhgn3imDSoZXT/view?usp=sharing สําหรับบุคคลทีต้องการติดตังบนระบบปฏิบัติการอืน อย่างเช่น Ubuntu ก็สามารถเข้าไปทีลิงค์นีได้ https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/ เครืองมือพืนฐานในการใช้งาน - MongoDB Community เป็น MongoDB Server รุ่น Open Source - MongoDB Compass เป็นเครืองมือในการจัดเก็บข้อมูลในรูปแบบ GUI ในการอํานวยความสะดวกในการวิเคราะห์ ข้อมูล การดําเนินการเก็บข้อมูล (CRUD) รวมทังสืบค้นและจัดการ Index เป็นต้น
  • 7. 7 50177004-Big Data Analytics-Thinnaphat Borirak ขันตอนการติดตังโปรแกรม
  • 8. 8 50177004-Big Data Analytics-Thinnaphat Borirak ให้ลองทดสอบการ Connect ผ่าน Browser เพือเข้าใช้งาน mongodb://localhost:27017 เอกสารประกอบการศึกษาหลัก: https://www.mongodb.com/docs/ ทําความเข้าใจกับ MongoDB จากการอภิปรายในบทความแนะนํา เราพบว่าข้อมูลใน MongoDB มี Schema ทียืดหยุ่น ใน MongoDB เราไม่ จําเป็นต้องประกาศคอลัมน์ใหม่ของ Collection ก่อนทีจะแทรกข้อมูลในฐานข้อมูล เหมือนกับแนวคิด RDBMS ทัวไป ดังนันจึงมีข้อควรพิจารณาพืนฐานบางประการทีเราต้องจําไว้ในขณะทีออกแบบ Schema ใน MongoDB 1. ต้องออกแบบ schema ตามความต้องการของผู้พัฒนา 2. หากเราใช้ Object ตังแต่ 2 Object ขึนไป จําเป็นต้องรวมเป็น Object เดียว 3. เวลาเขียนข้อมูลสามารถทําการรวมข้อมูลตอนทีเขียนพร้อมกันได้ 4. สามารถเพิมประสิทธิภาพ Schema สําหรับการค้นหาข้อมูลทีบ่อยทีสุดได้ 5. สามารถทําหน้าทีรวมข้อมูลในเรืองของ Aggregation Functions ได้
  • 9. 9 50177004-Big Data Analytics-Thinnaphat Borirak What are the Documents in MongoDB Document เป็นหน่วยพืนฐานของข้อมูลใน MongoDB ซึงโดยทัวไปเทียบเท่ากับแถวตามหลักการออกแบบ แนวคิดของระบบ RDBMS เปรียบได้ว่า Document คือ หัวใจหลักของ MongoDB นันเอง โดย Document ในทีนีก็ คือ ชุดคีย์ทีเรียงลําดับกันซึงมีข้อมูลหรือค่าทีเกียวข้องกับการแสดงข้อมูลของ Document เหล่านัน ไม่ว่าจะเป็น Map, Hash หรือ Dictionary ในกรณีของ JavaScript เอกสารจะแสดงถึงข้อมูลทีเป็น Object เช่นเดียวกับรายละเอียดการใช้ งานทีสําคัญของ MongoDB Data Types of MongoDB - นันสามารถรองรับประเภทข้อมูลทีหลากหลายได้ในค่าของ Document ซึง MongoDB มัก ถูกมองว่าเป็นข้อมูลทีเหมือน JSON ทีเป็น JavaScript Object โดย JSON นันเป็นการแสดงข้อมูลอย่างง่าย ข้อดีของ JSON คือ ช่วยให้เข้าใจในเรืองของการวิเคราะห์ในแต่ละเฟสและง่ายต่อการจํา นอกจากนี JSON ยังยอมรับค่าเฉพาะ ข้อมูลทีเป็นประเภท null, Boolean, Number, String, Array และ Object ตามรายการด้านล่างคือประเภทข้อมูลทีมีอยู่ ใน MongoDB String This is most commonly used datatype to store the data. String in MongoDB must be UTF-8 valid. Integer This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server. Boolean This type is used to store a Boolean (true/ false) value. Double This type is used to store floating point values. Min/Max keys This type is used to compare a value against the lowest and highest BSON elements. Arrays This type is used to store arrays or list or multiple values into one key. Date This datatype is used to store the current date or time in UNIX time format. You can specify your own datetime by creating object of Date and passing day, month, year into it. Timestamp This can be handy for recording when a document has been modified or added. Object This datatype is used for embedded documents. Object ID This datatype is used to store the document’s ID. Null This type is used to store a Null value. Symbol This datatype is used identically to a string however, it's generally reserved for languages that use a specific symbol type. Binary data This datatype is used to store Binary data. Code This datatype is used to store JavaScript code into document. Regular expression This datatype is used to store regular expression การสร้างแบบจําลองข้อมูล MongoDB ให้คิดซะว่าข้อมูลใน MongoDB มี schema.documents ทียืดหยุ่นในคอลเล็กชันเดียวกันไม่จําเป็นต้องมีชุดฟิลด์หรือ โครงสร้างชุดเดียวกัน และฟิลด์ทัวไปในเอกสารของคอลเล็กชันอาจมีข้อมูลประเภทต่างๆ กัน ข้อควรพิจารณาบางประการขณะออกแบบสคีมา (Designing Schema) ใน MongoDB  ออกแบบสคีมาตามความต้องการของผู้ใช้  รวม Object เป็นเอกสารเดียวกันถ้าหากต้องการใช้ร่วมกัน แต่ถ้าไม่ควรให้แยกออกจากกัน  ทําสําเนาข้อมูลให้สมําเสมอ ให้คิดว่าพืนทีในการจัดเก็บเทียบกับเวลาทีใช้ในการคํานวณไม่ได้  ปรับสคีมาให้เหมาะสมสําหรับกรณีการใช้งานส่วนทีใช้บ่อยทีสุด
  • 10. 10 50177004-Big Data Analytics-Thinnaphat Borirak Example: สมมติว่าลูกค้าต้องการการออกแบบฐานข้อมูลสําหรับเว็บไซต์บล็อกของเขา และให้ดูความแตกต่างระหว่าง RDBMS และการออกแบบสคีมาของ MongoDB โดยเว็บไซต์นันจะมีข้อกําหนดดังต่อไปนี  ทุกโพสต์จะมีชือ คําอธิบาย และ URL ทีไม่ซํากัน  ทุกโพสต์สามารถมีหนึงแท็กหรือมากกว่า  ทุกโพสต์มีชือผู้เผยแพร่และจํานวนไลค์ทังหมด  ทุกโพสต์จะมีความคิดเห็นทีผู้ใช้ให้ไว้พร้อมกับชือ ข้อความ ข้อมูลเวลา และยอดไลค์  ในแต่ละโพสต์อาจมีความคิดเห็นเป็นศูนย์หรือมากกว่าก็ได้ ในการออกแบบ Schema-RDBMS สําหรับข้อกําหนดข้างต้นจะมีตารางอย่างน้อย 3 ตาราง ดังนี ในขณะทีการออกแบบ Schema ของ MongoDB จะมีหนึงโพสต์ทีเป็น Collection และมีโครงสร้างดังต่อไปนี { _id: POST_ID title: TITLE_OF_POST, description: POST_DESCRIPTION, by: POST_BY, url: URL_OF_POST, tags: [TAG1, TAG2, TAG3], likes: TOTAL_LIKES, comments: [ { user:'COMMENT_BY', message: TEXT, dateCreated: DATE_TIME, like: LIKES }, { user:'COMMENT_BY', message: TEXT, dateCreated: DATE_TIME, like: LIKES } ] } ดังนันในขณะทีแสดงข้อมูลใน RDBMS จะต้องทําการ Join สามตาราง ส่วนข้อมูลใน MongoDB จะแสดงเพียงแค่ Collection เดียวเท่านัน ข้อเสนอแนะ ให้เขียนคําสัง SQL ในการสร้างฐานข้อมูลเพือใช้ในการจัดเก็บข้อมูลสําหรับเว็บไซต์บล็อกของลูกค้า ตามตัวอย่างทีกําหนดให้ พร้อมทดสอบการเพิมชุดข้อมูลการ Post และ Comment ลงในฐานข้อมูล จากนันเขียนคําสัง SQL ในการแสดงรายการทีได้จากการจัดเก็บข้อมูลของเว็บบล็อกนัน
  • 11. 11 50177004-Big Data Analytics-Thinnaphat Borirak Workshop: Create Database The use Command MongoDB use DATABASE_NAME is used to create database. The command will create a new database, if it doesn't exist otherwise it will return the existing database. Syntax: Basic syntax of use DATABASE statement is as follows: use DATABASE_NAME Example: If you want to create a database with name <mydb>, then use DATABASE statement would be as follows: >use mydb switched to db mydb To check your currently selected database use the command db >db mydb If you want to check your databases list, then use the command show dbs. >show dbs admin 40.00 KiB config 60.00 KiB local 72.00 KiB Your created database (mydb) is not present in list. To display database you need to insert atleast one document into it. >db.movie.insert({"name":"Avatar"}) >show dbs admin 40.00 KiB config 108.00 KiB local 72.00 KiB mydb 40.00 KiB * In mongodb default database is test. If you didn't create any database then collections will be stored in test database. Example: Try writing a command to add two more movie titles. > >
  • 12. 12 50177004-Big Data Analytics-Thinnaphat Borirak Workshop: Drop Database The dropDatabase () Method MongoDB db.dropDatabase () command is used to drop a existing database. Syntax: Basic syn tax of dropDatabase () command is as follows: db.dropDatabase() This will delete the selected database. If you have not selected any database, then it will delete default 'test' database Example: First, check the list available databases by using the command show dbs >show dbs admin 40.00 KiB config 108.00 KiB local 72.00 KiB mydb 40.00 KiB If you want to delete new database <mydb>, then dropDatabase() command would be as follows: >use mydb switched to db mydb >db.dropDatabase() { ok: 1, dropped: 'mydb' } Now check list of databases >show dbs admin 40.00 KiB config 108.00 KiB local 72.00 KiB
  • 13. 13 50177004-Big Data Analytics-Thinnaphat Borirak Workshop: Create Collection The createCollection() Method MongoDB db.createCollection(name, options) is used to create collection. Syntax: Basic syntax of createCollection() command is as follows db.createCollection(name, options) In the command, name is name of collection to be created. Options is a document and used to specify configuration of collection Parameter Type Description Name String Name of the collection to be created Options Document (Optional) Specify options about memory size and indexing Options parameter is optional, so you need to specify only name of the collection. Following is the list of options you can use: Field Type Description capped Boolean (Optional) If true, enables a capped collection. Capped collection is a collection fixed size collection that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify size parameter also. autoIndexId Boolean (Optional) If true, automatically create index on _id field.s Default value is false. size number (Optional) Specifies a maximum size in bytes for a capped collection. If capped is true, then you need to specify this field also. max number (Optional) Specifies the maximum number of documents allowed in the capped collection. Examples: Basic syntax of createCollection() method without options is as follows >use mydb 'switched to db mydb' >db.createCollection("mycollection") { ok: 1 } You can check the created collection by using the command show collections >show collections mycollection
  • 14. 14 50177004-Big Data Analytics-Thinnaphat Borirak Following example shows the syntax of createCollection() method with few important options: >db.createCollection("mycollection_option",{capped:true,autoIndexId:true,size:6142800,max:10000}) { ok: 1 } In MongoDB you don't need to create collection. MongoDB creates collection automatically, when you insert some document. >db.mycollection_50177004.insert({"name":"Big Data Analytics"}) >show collections mycollection mycollection_50177004 mycollection_option Note: Rename Collection >db.collection_name.renameCollection("collection_New_name") Workshop: Drop Collection The drop() Method MongoDB's db.collection.drop() is used to drop a collection from the database. Syntax: Basic syntax of drop() command is as follows db.COLLECTION_NAME.drop() Example: First, check the available collections into your database mydb > use mydb 'already on db mydb' >show collections mycollection mycollection_50177004 mycollection_option Now drop the collection with the name mycollection > db.mycollection.drop() true drop() method will return true, if the selected collection is dropped successfully otherwise it will return false Again check the list of collections into database > show collections mycollection_50177004
  • 15. 15 50177004-Big Data Analytics-Thinnaphat Borirak mycollection_option Example: drop the collection with the name mycollection_50177004 and mycollection_option > > Now check the list of collections into database > Now check list of databases > Why is there no database named mydb? Workshop: Insert Document The insert() Method To insert data into MongoDB collection, you need to use MongoDB's insert() or save()method. Syntax Basic syntax of insert() command is as follows: >db.COLLECTION_NAME.insert(document) Example >db.mycollection.insert({ title: 'MongoDB Insert() Method', description: 'MongoDB is NoSQL Database', by: 'Abusayard TK Limon', url: 'https://www.mongodb.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }) Example >db.post.insert( [ { title: 'MongoDB Overview', description: 'MongoDB is no sql database', by: 'Abusayard TK Limon', url: 'https://www.mongodb.com',
  • 16. 16 50177004-Big Data Analytics-Thinnaphat Borirak tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }, { title: 'NoSQL Database', description: 'NoSQL database does not have tables', by: 'Thinnaphat', email: 'thinnaphat.bo@gmail.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 30, comments: [ { user:'Adam', message: 'Adam first comment', dateCreated: new Date(2023,01,28,2,35), like: 0 } ] } ] ) Note: InsertOne - db.Collection_name.insertOne( <Document> ) InsertMany - db.Collection_name.insertMany( [ <Document> ] ) Workshop: Basic Query Document The find() Method To query data from MongoDB collection, you need to use MongoDB's find() method. Syntax Basic syntax of find() method is as follows >db.COLLECTION_NAME.find() find() method will display all the documents in a non structured way. Example >db.post.find() >db.post.find({"title":"MongoDB Overview"}) The pretty() Method To display the results in a formatted way, you can use pretty() method. Syntax: >db.mycolection.find().pretty()
  • 17. 17 50177004-Big Data Analytics-Thinnaphat Borirak Example >db.mycollection.find().pretty( { title: 'MongoDB Insert() Method', description: 'MongoDB is NoSQL Database', by: 'Abusayard TK Limon', url: 'https://www.mongodb.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 } ) Assignment: ให้ทําการออกแบบฐานข้อมูลเพือใช้ในการจัดเก็บข้อมูลของสภาพภูมิอากาศ โดยใช้ตัวอย่างคําสังในการ สร้าง Schema ทีให้ จากนันลองเพิมข้อมูล พร้อมทําความเข้าใจหลักการทํางานนันว่ามีรูปแบบอย่างไร https://www.mongodb.com/docs/manual/core/timeseries/timeseries-procedures/