Untitled

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

การออกแบบ schema ที่ดีควรคำนึงถึงสิ่งใดเป็นสำคัญ?

  • การละเลยฟิลด์เสริม (assistive fields) เพื่อลดความซับซ้อน
  • การระบุฟิลด์ที่จำเป็น (required fields) เพื่อให้ข้อมูลมีความสมบูรณ์ (correct)
  • การมีจำนวนฟิลด์น้อยที่สุดเท่าที่จะเป็นไปได้เพื่อความรวดเร็วในการประมวลผล
  • การใช้ชื่อฟิลด์ที่ไม่สื่อความหมายเพื่อลดขนาดของ schema

เหตุใดการกำหนด 'required fields' ใน schema จึงมีความสำคัญ?

  • เพื่อลดขนาดของฐานข้อมูล
  • เพื่อให้มั่นใจว่าข้อมูลที่จำเป็นต่อการใช้งานจะไม่สูญหาย (correct)
  • เพื่อลดความซับซ้อนในการพัฒนา application
  • เพื่อเพิ่มความเร็วในการค้นหาข้อมูล

ข้อใดต่อไปนี้อธิบายความหมายของ 'assistive fields' ได้ถูกต้องที่สุด?

  • ฟิลด์ที่ใช้ในการเชื่อมโยงข้อมูลกับ schema อื่น
  • ฟิลด์ที่ให้ข้อมูลเพิ่มเติมหรือคำแนะนำในการใช้งาน (correct)
  • ฟิลด์ที่ใช้ในการเข้ารหัสข้อมูล
  • ฟิลด์ที่ช่วยในการตรวจสอบความถูกต้องของข้อมูล

หาก schema ขาด 'required fields' จะส่งผลเสียอย่างไร?

<p>ข้อมูลบางส่วนอาจสูญหายหรือไม่สมบูรณ์ (D)</p> Signup and view all the answers

เพราะเหตุใดการออกแบบ schema ที่ดียังต้องคำนึงถึง 'discontent rosfields' นอกเหนือจาก 'required fields'?

<p>ตัวเลือกนี้ไม่สมเหตุสมผล เพราะว่า 'discontent rosfields' ไม่มีความเกี่ยวข้องกับการออกแบบ schema (D)</p> Signup and view all the answers

การออกแบบ Schema ใน MongoDB รูปแบบใดที่เหมาะสมกับการดึงข้อมูลที่เกี่ยวข้องบ่อยครั้งโดยไม่ต้อง Join หลายตาราง?

<p>Embedding Document (C)</p> Signup and view all the answers

ข้อใดเป็นข้อดีหลักของการใช้ Referencing ใน MongoDB?

<p>ลดความซ้ำซ้อนของข้อมูล (D)</p> Signup and view all the answers

สถานการณ์ใดที่เหมาะสมที่สุดในการเลือกใช้ Embedding แทน Referencing ใน MongoDB?

<p>เมื่อข้อมูลมีความสัมพันธ์แบบ One-to-Few ที่มีการดึงข้อมูลร่วมกันบ่อยครั้ง (D)</p> Signup and view all the answers

รูปแบบใดต่อไปนี้ที่ควรหลีกเลี่ยงในการออกแบบ Schema ของ MongoDB เพื่อป้องกันปัญหาด้านประสิทธิภาพ?

<p>Referencing documents ที่มีการ Join บ่อยครั้ง (C)</p> Signup and view all the answers

ข้อใดคือความแตกต่างหลักระหว่าง Embedding และ Referencing ใน MongoDB?

<p>Embedding เก็บข้อมูลไว้ใน Document เดียวกัน, Referencing เก็บข้อมูลแยกกัน (B)</p> Signup and view all the answers

การเลือกใช้ Embedding หรือ Referencing ใน MongoDB มีปัจจัยใดบ้างที่มีผลต่อการตัดสินใจ?

<p>ความถี่ในการแก้ไขข้อมูลและความสัมพันธ์ของข้อมูล (C)</p> Signup and view all the answers

หากต้องการออกแบบ Schema สำหรับระบบ E-commerce ที่มีการเก็บข้อมูลลูกค้าและ Orders จำนวนมาก ควรพิจารณาเลือกใช้รูปแบบใดเพื่อประสิทธิภาพในการดึงข้อมูล?

<p>Referencing ข้อมูล Orders จาก Document ของลูกค้า (A)</p> Signup and view all the answers

ในการออกแบบ Schema ของ MongoDB สำหรับระบบจัดการ Blog posts และ Comments หากต้องการให้การดึงข้อมูล Blog post พร้อม Comments ทำได้อย่างรวดเร็ว ควรเลือกใช้วิธีใด?

<p>เก็บ Comments ทั้งหมดไว้ใน Array ภายใน Blog post document (D)</p> Signup and view all the answers

สถานการณ์ใดที่ควรพิจารณาการอ้างอิง (referencing) แทนการฝัง (embedding) ตามหลักการที่ให้ไว้

<p>เมื่อความสัมพันธ์เป็นแบบหนึ่งต่อหลาย (one-to-many) (C)</p> Signup and view all the answers

เหตุผลหลักที่สนับสนุนการใช้การฝัง (embedding) ในความสัมพันธ์แบบหนึ่งต่อกลุ่มเล็กน้อย (one-to-few relationships) คืออะไร

<p>เพื่อลดความซับซ้อนในการเรียกค้นข้อมูล (A)</p> Signup and view all the answers

ข้อใดต่อไปนี้ ไม่ใช่ ปัจจัยที่ควรพิจารณาเมื่อเลือกระหว่างการฝัง (embedding) และการอ้างอิง (referencing)

<p>ภาษาโปรแกรมที่ใช้ในการพัฒนา (B)</p> Signup and view all the answers

สมมติว่าคุณกำลังออกแบบฐานข้อมูลสำหรับระบบจัดการลูกค้าสัมพันธ์ (CRM) ซึ่งลูกค้าหนึ่งรายสามารถมีที่อยู่ได้หลายแห่ง คุณควรเลือกใช้วิธีการใดในการจัดการความสัมพันธ์นี้

<p>อ้างอิง (referencing) ไปยังตารางที่อยู่ (A)</p> Signup and view all the answers

สถานการณ์ใดที่ 'ไม่' เหมาะสมกับการใช้การฝัง (Embedding)?

<p>เมื่อข้อมูลที่ถูกฝังมีการเปลี่ยนแปลงบ่อยครั้ง (B)</p> Signup and view all the answers

เหตุใดจึงไม่ควรให้ Arrays เติบโตโดยไม่มีขอบเขตจำกัดในฐานข้อมูล NoSQL?

<p>ถูกทุกข้อ (B)</p> Signup and view all the answers

สถานการณ์ใดที่ไม่ควรใช้วิธีการอ้างอิงด้วย ObjectID จำนวนมากใน Array?

<p>เมื่อจำนวนเอกสารที่อ้างอิงมีจำนวนมาก (เช่น หลายพันเอกสาร) (D)</p> Signup and view all the answers

ข้อใดคือลักษณะสำคัญของ Fixed-size collection (Capped Collection) ใน MongoDB?

<p>มีการเขียนทับข้อมูลเก่าโดยอัตโนมัติเมื่อถึงขนาดสูงสุดที่กำหนด (B)</p> Signup and view all the answers

Capped Collection เหมาะสมกับการใช้งานประเภทใดมากที่สุด?

<p>การจัดเก็บข้อมูล Log ที่มีการเขียนอย่างต่อเนื่องและต้องการพื้นที่จำกัด (A)</p> Signup and view all the answers

พารามิเตอร์ใดที่ใช้ในการกำหนดขนาดของ Capped Collection ใน MongoDB?

<p><code>cappedSize</code> (ขนาดสูงสุดในหน่วยไบต์) (D)</p> Signup and view all the answers

หากต้องการจำกัดจำนวนเอกสารใน Capped Collection ควรใช้พารามิเตอร์ใด?

<p><code>maxDocuments</code> (B)</p> Signup and view all the answers

ข้อใดคือข้อดีของการใช้ Capped Collection?

<p>สามารถจัดการพื้นที่จัดเก็บได้อย่างมีประสิทธิภาพสำหรับข้อมูลที่มีการเขียนอย่างต่อเนื่อง (B)</p> Signup and view all the answers

การออกแบบฐานข้อมูลแบบ Many-to-Many ที่ไม่ดีอาจนำไปสู่ปัญหาใด?

<p>ทำให้เกิดความยุ่งยากในการจัดการความสัมพันธ์ของข้อมูล (A)</p> Signup and view all the answers

เหตุใดการใช้ Array ขนาดใหญ่ในการเก็บข้อมูลอ้างอิง (References) ใน NoSQL จึงเป็นสิ่งที่ไม่ควรทำ?

<p>เพราะประสิทธิภาพในการสืบค้นข้อมูลจะลดลงเมื่อ Array มีขนาดใหญ่ (B)</p> Signup and view all the answers

Collection ประเภทใดใน MongoDB ที่เหมาะสำหรับการเก็บข้อมูลที่มีลักษณะเป็น Stream และต้องการรักษาลำดับการ Insert?

<p>Capped Collection (B)</p> Signup and view all the answers

ข้อใดคือความแตกต่างที่สำคัญระหว่าง Capped Collection กับ Collection ปกติ?

<p>Capped Collection มีขนาดคงที่และเขียนทับข้อมูลเก่าเมื่อเต็ม (B)</p> Signup and view all the answers

การกำหนดขนาดเริ่มต้น (initial size) ให้กับ Capped Collection มีผลต่อสิ่งใด?

<p>ประสิทธิภาพในการเขียนข้อมูลในช่วงแรก (C)</p> Signup and view all the answers

หากต้องการสร้าง Capped Collection ที่มีขนาด 10MB และจำกัดจำนวนเอกสารไว้ที่ 1000 เอกสาร ควรใช้คำสั่งใด?

<p><code>db.createCollection('myCollection', { capped: true, size: 10000000, max: 1000 })</code> (A)</p> Signup and view all the answers

ข้อใดคือข้อจำกัดของการใช้ Capped Collection?

<p>ไม่สามารถแก้ไข (Update) เอกสารที่มีขนาดใหญ่ขึ้นใน Capped Collection ได้ (B)</p> Signup and view all the answers

สถานการณ์ใดที่ควรหลีกเลี่ยงการใช้ความสัมพันธ์แบบ Many-to-Many โดยใช้ Array ของ References ใน MongoDB?

<p>เมื่อจำนวนความสัมพันธ์มีแนวโน้มที่จะเพิ่มขึ้นอย่างมากและไม่สามารถคาดการณ์ได้ (B)</p> Signup and view all the answers

Flashcards

Required Fields

ฟิลด์ที่ schema กำหนดว่าจำเป็นต้องมี

Content Fields

ฟิลด์ที่ schema อนุญาตให้ใส่ได้

Assistive Fields

ฟิลด์ที่ช่วยในการเข้าถึงสำหรับผู้พิการ

Schema Furnishes

รูปแบบเฉพาะของ schema

Signup and view all the flashcards

Discontent Ros Fields

การไม่เห็นด้วยหรือไม่สอดคล้องของฟิลด์

Signup and view all the flashcards

การอ้างอิง (Referencing)

การอ้างอิงถึงข้อมูลที่อยู่ในที่อื่น แทนที่จะรวมข้อมูลนั้นไว้โดยตรง

Signup and view all the flashcards

การฝัง (Embedding)

การรวมข้อมูลที่เกี่ยวข้องไว้ภายในโครงสร้างข้อมูลเดียว

Signup and view all the flashcards

หนึ่งต่อหนึ่ง (One-to-One)

เมื่อความสัมพันธ์เป็นแบบหนึ่งต่อหนึ่ง การฝัง (embedding) จะเหมาะสมกว่า

Signup and view all the flashcards

หนึ่งต่อจำนวนน้อย (One-to-Few)

เมื่อความสัมพันธ์เป็นแบบหนึ่งต่อจำนวนน้อย การฝัง (embedding) มักจะเหมาะสมกว่า

Signup and view all the flashcards

หนึ่งต่อจำนวนมาก (One-to-Many)

เมื่อความสัมพันธ์เป็นแบบหนึ่งต่อจำนวนมาก การอ้างอิง (referencing) มักจะเหมาะสมกว่า

Signup and view all the flashcards

ประเภทข้อมูล MongoDB

ประเภทของข้อมูลใน MongoDB

Signup and view all the flashcards

Field Level Schema

โครงสร้าง Schema ในระดับ Field

Signup and view all the flashcards

การออกแบบ Schema

หลักการออกแบบ Schema ใน MongoDB

Signup and view all the flashcards

ERROR: * แทนแตกตาราง

การใช้ * แทนการแตกตาราง

Signup and view all the flashcards

รวมข้อมูลใน Document

การรวมข้อมูลที่เกี่ยวข้องไว้ใน Document เดียว

Signup and view all the flashcards

Reference

การอ้างอิง Document อื่น (คล้าย Foreign Key)

Signup and view all the flashcards

Embedding

การฝัง Document ไว้ใน Document อื่น

Signup and view all the flashcards

Reference vs. Embedding

เปรียบเทียบการใช้ Reference กับ Embedding

Signup and view all the flashcards

ขนาดอาร์เรย์ที่จำกัด

หลีกเลี่ยงการเติบโตของอาร์เรย์อย่างไร้ขีดจำกัดในเอกสาร

Signup and view all the flashcards

การอ้างอิง vs. การฝัง

หลีกเลี่ยงการฝังเอกสารจำนวนมาก; อ้างอิงแทน

Signup and view all the flashcards

ObjectID อาร์เรย์

หลีกเลี่ยงการใช้อาร์เรย์ของ ObjectID สำหรับการอ้างอิงจำนวนมาก

Signup and view all the flashcards

Fixed Collection

คอลเลกชันที่มีขนาดคงที่ ซึ่งเขียนทับรายการเก่าสุดเมื่อเต็ม

Signup and view all the flashcards

Capped Collection

คอลเลกชันที่มีขนาดคงที่ ซึ่งเขียนทับรายการเก่าสุดเมื่อเต็ม

Signup and view all the flashcards

ขนาด Capped Collection

ระบุขนาดสูงสุดของ Capped Collection เป็นไบต์

Signup and view all the flashcards

จำนวนเอกสารสูงสุด

ระบุจำนวนเอกสารสูงสุดที่อนุญาตใน Capped Collection

Signup and view all the flashcards

Overwrite entries

Automatically overwrite its oldest entries when it reaches its maximum size

Signup and view all the flashcards

Study Notes

MongoDB

  • ฐานข้อมูล NoSQL ประเภท Document Database ที่ใช้ JSON object ในการเก็บข้อมูล
  • MongoDB เป็น schema-less ทำให้ไม่จำเป็นต้องระบุ field หรือ content ของ field
  • เหมาะกับข้อมูล unstructured หรือข้อมูลที่กำหนด schema ได้ยาก
  • สามารถ scale ขยายระบบได้ดี ทั้งด้าน data volume และ network traffic

ประเภทของ NoSQL Databases

  • Document Databases (e.g., MongoDB)
  • Key-values stores (e.g., Redis)
  • Column-oriented databases (e.g., Apache Hbase, Apache Parquet)
  • Graph databases (e.g., Neo4j)
  • Time series databases (e.g., InfluxDB)

CAP Theorem

  • Consistency (C): การอ่านข้อมูลจะได้รับข้อมูลที่อัปเดตล่าสุด หรือ error
  • Availability (A): การอ่านข้อมูลสำเร็จแน่นอน แต่ไม่รับประกันความถูกต้องล่าสุด
  • Partition Tolerance (P): ระบบยังทำงานได้แม้มีการ drop message หรือ network partition ระหว่าง distributed nodes

คำศัพท์ที่ควรรู้

  • Database เทียบเท่า Database ใน SQL
  • Collection คือ Table
  • Document คือ Row
  • Field คือ Column
  • Index คือ Index
  • $lookup คือ Table joins

Schema Definition

  • MongoDB สามารถกำหนด schema ในระดับ field ได้
  • ตัวอย่าง schema: กำหนด required fields, types, minimum/maximum values, unique items, patterns
  • ตัวอย่างการออกแบบ: Post ต้องมีชื่อ, คำอธิบาย, ชื่อเจ้าของ URL, จำนวนไลค์ และอาจมี tags และ comments
  • ตัวอย่าง code schema:
{
  "bsonType": "object",
  "required": ["name", "age", "favoriteColors"],
  "properties": {
    "name": { "bsonType": "string" },
    "age": { "bsonType": "int", "minimum": 13, "exclusiveMinimum": false },
    "favoriteColors": {
      "bsonType": "array",
      "uniqueItems": true,
      "items": {
        "bsonType": "object",
        "properties": {
          "rank": { "bsonType": "int" },
          "name": { "bsonType": "string" },
          "hexCode": { "bsonType": "string", "pattern": "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$" }
        }
      }
    }
  }
}

Common types in MongoDB

  • Objects, UUIDs, ObjectIds
  • Arrays, Strings, Mixed
  • Numbers, Sets
  • Booleans, Dictionaries

Embedding vs. Referencing

  • Embedding คือการนำข้อมูลที่เกี่ยวข้องมารวมกันใน document เดียว
  • Referencing คือการเก็บ reference ไปยัง document อื่น (ใช้ $lookup ในการ join)

Embedding

  • ข้อดี: เรียกข้อมูลได้ใน query เดียว, ไม่ต้อง implement joins เอง, อัปเดตข้อมูลที่เกี่ยวข้องเป็น atomic operation
  • ข้อเสีย: Document ใหญ่เกินไปถ้า fields ส่วนใหญ่ไม่เกี่ยวข้อง, MongoDB มี limit ขนาด document ที่ 16MB
  • เหมาะสำหรับ: ข้อมูลที่มักถูกใช้งานร่วมกัน, ความสัมพันธ์แบบ one-to-one หรือ one-to-few

Referencing

  • ข้อดี: Document เล็กกว่า, ลดการ duplication ของข้อมูล
  • ข้อเสีย: ต้อง query หลายครั้ง หรือใช้ $lookup เพื่อดึงข้อมูลที่เกี่ยวข้อง
  • เหมาะสำหรับ: ข้อมูลที่มีความสัมพันธ์แบบ one-to-many หรือ many-to-many

Types of Relationships

  • One-to-One: Prefer key-value pairs ภายใน document
  • One-to-Few: Prefer embedding
  • One-to-Many: Prefer referencing
  • One-to-Squillions: Referencing
  • Many-to-Many: Referencing
  • Array ไม่ควรมีขนาดใหญ่เกินไป:
    • ถ้ามีข้อมูลไม่มาก ( a couple of hundred documents ) ให้ใช้ embedding
    • แต่ถ้ามีข้อมูลเยอะ ( a few thousand documents ) ให้ใช้ referencing

Rule of thumb

  • Favor embedding unless there is a compelling reason not to
  • หากต้องการเข้าถึง object หนึ่งๆ โดยเฉพาะ ไม่ควร embed

Capped Collections

  • Collection ที่มีขนาดคงที่ เมื่อข้อมูลเต็ม จะ overwrite ข้อมูลเก่าสุด
  • ต้องกำหนดขนาด (size) และจำนวน document สูงสุด (max)
  • ใช้สำหรับเก็บ log หรือข้อมูลที่ไม่ต้องการเก็บไว้นาน

CRUD Operations

  • db.COLLECTION_NAME.insert(document): เพิ่ม document
  • db.COLLECTION_NAME.find(): ค้นหา document
    • สามารถใช้ .pretty() เพื่อให้อ่านง่ายขึ้น
  • db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA): แก้ไข document
  • db.COLLECTION_NAME.remove(DELETION_CRITERIA): ลบ document
  • db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION): ทำ aggregation

Query Operators

  • Equality: {<key>:<value>}
  • Less Than: {<key>:{$lt:<value>}}
  • Less Than or Equals: {<key>:{$lte:<value>}}
  • Greater Than: {<key>:{$gt:<value>}}
  • Greater Than or Equals: {<key>:{$gte:<value>}}
  • Not Equals: {<key>:{$ne:<value>}}
  • AND: {attr:{$op:value), attr:{$op:value}}
  • OR: {$or: [{attr:{$op:value}}, {attr:{$op:value}}]}

Projection

  • db.COLLECTION_NAME.find({},{KEY:1}): เลือกว่าจะแสดง fields ไหน (1 = แสดง, 0 = ไม่แสดง ยกเว้น _id ที่ต้องกำหนดเป็น 0 เพื่อไม่ให้แสดง)

All และ In Operators

  • $all: select documents ที่มี array field ซึ่งมี values ครบตามที่กำหนด
  • $in: select documents ที่มี array field ซึ่งมี values อย่างน้อยหนึ่งตัวตรงกับที่กำหนด

Limit/Skip Records

  • db.COLLECTION_NAME.find().limit(NUMBER): จำกัดจำนวน document ที่แสดง
  • db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER): ข้าม document ไปจำนวนหนึ่งก่อนแสดง

Aggregation

  • การประมวลผลข้อมูลและสรุปผล
  • $group: จัดกลุ่ม documents ตาม field ที่กำหนด
  • $sum: รวมค่า
  • $avg: หาค่าเฉลี่ย
  • อื่นๆ: $min, $max, $push, $addToSet, $first, $last
  • $match: กรอง documents ตามเงื่อนไข
  • $lookup: Join ข้อมูลจาก collection อื่น
  • $unwind: แยก array field ออกเป็นหลาย documents

Backup and Restore

  • mongodump: สร้าง backup ของฐานข้อมูล
    • สามารถเลือก backup ทั้ง database, collection หรือระบุเงื่อนไขได้
  • mongorestore: Restore ข้อมูลจาก backup
  • mongoexport: Export ข้อมูลเป็น JSON หรือ CSV
  • mongoimport: Import ข้อมูลจาก JSON หรือ CSV

Monitoring Tools

  • mongostat: แสดงสถานะการทำงานของ MongoDB (inserts, queries, updates, deletes, cursors)
  • mongotop: แสดงสถิติการใช้ทรัพยากรของแต่ละ collection

Authentication and Authorization

  • MongoDB มีระบบ authentication (ยืนยันตัวตน) และ authorization (กำหนดสิทธิ์)
  • ต้อง enable authentication ก่อนจึงจะสามารถสร้าง user ได้

คำสั่งที่ใช้ในการจัดการ authentication

  • use admin: เปลี่ยนไปใช้ฐานข้อมูล admin
  • db.createUser(): สร้าง user ใหม่
  • db.auth(): ยืนยันตัวตนด้วย user ที่สร้างไว้
  • db.dropUser(): ลบ user
  • แก้ไข mongod.conf เพื่อเปิด/ปิด authentication และกำหนด port

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Summary: MongoDB

More Like This

Untitled
110 questions

Untitled

ComfortingAquamarine avatar
ComfortingAquamarine
Untitled Quiz
6 questions

Untitled Quiz

AdoredHealing avatar
AdoredHealing
Untitled
6 questions

Untitled

StrikingParadise avatar
StrikingParadise
Untitled
53 questions

Untitled

ProperMahoganyObsidian avatar
ProperMahoganyObsidian
Use Quizgecko on...
Browser
Browser