01MongoDB ODM คืออะไร

Schema, Model และ Validation

Object–Document Mapper (ODM) เช่น Mongoose เชื่อม object ใน application กับ MongoDB document โดยเพิ่ม schema, casting, validation, model methods, middleware และ helper สำหรับ query หรือ reference เหนือ native driver

policy-model.ts
const policySchema = new Schema({
  customerId: { type: Schema.Types.ObjectId, required: true },
  status: { type: String, enum: ['PENDING', 'APPROVED'] },
  premium: { type: Number, min: 0 },
}, { timestamps: true })

policySchema.index({ customerId: 1, status: 1, createdAt: -1 })

const policies = await Policy
  .find({ customerId, status: 'PENDING' })
  .sort({ createdAt: -1 })
  .limit(20)
  .lean()
ทางเลือกใช้เมื่อTrade-off
ODM

ทีมต้องการ model convention, validation และ productivity

มี document hydration, hooks และ generated query ที่ต้องเข้าใจ

Native Driver

ต้องการ control, overhead ต่ำ หรือใช้ MongoDB feature โดยตรง

ต้องจัดการ validation, mapping และ convention เองมากขึ้น

Aggregation Pipeline

group, reshape, lookup และวิเคราะห์ข้อมูลหลายขั้น

pipeline ซับซ้อนและต้องออกแบบ index/input reduction ให้ดี

ข้อควรระวังของ ODM

  • ODM schema ป้องกันเฉพาะ code path ที่ผ่าน ODM; writer อื่นยังต้องมี database schema validation ตามความเสี่ยง
  • populate() สะดวกแต่ไม่ใช่ relational join ฟรี—ตรวจจำนวน query, payload และพิจารณา embed หรือ aggregation
  • งาน read-only ใช้ lean/plain object เพื่อลด hydration cost เมื่อไม่ต้องใช้ document methods และ change tracking
  • ประกาศ index ใน schema ไม่แทน migration plan และการตรวจผลด้วย execution statistics
อ่านเอกสารทางการ: Mongoose Schemas ↗

02Embed หรือ Reference

Embed เมื่อข้อมูลมี lifecycle เดียวกัน, ถูกอ่านพร้อมกัน และขนาดไม่โตไม่สิ้นสุด ข้อดีคืออ่านจบในครั้งเดียวและ update ภายใน document เดียวเป็น atomic ส่วน Reference เหมาะกับ many-to-many, entity ที่เปลี่ยนแยกกัน หรือ array ที่โตแบบ unbounded

Embed

ที่อยู่ใน policy application, snapshot ของข้อมูลที่ต้อง audit และ item จำนวนจำกัด

Reference

customer ที่แชร์หลาย policy, catalog กลาง หรือความสัมพันธ์จำนวนมากและเปลี่ยนบ่อย

03Index และ Query Shape

Compound index ใช้ prefix ของ index จึงต้องเรียง field ให้ตรง query สำคัญ โดยทั่วไป equality fields มาก่อน sort และ range แต่ควรยืนยันด้วยexplain("executionStats") ดู `totalDocsExamined`, `totalKeysExamined` และจำนวนผลลัพธ์

mongosh
// Query pattern: รายการคำขอล่าสุดของลูกค้า แยกตามสถานะ
db.policyApplications.createIndex({
  customerId: 1,
  status: 1,
  createdAt: -1
})

db.policyApplications.find({
  customerId: customerId,
  status: "PENDING"
}).sort({ createdAt: -1 }).limit(20).explain("executionStats")

ทุก index เพิ่มต้นทุน write และ memory working set หลีกเลี่ยง index ซ้ำซ้อนและ array หลาย field ใน compound index ซึ่งอาจสร้าง multikey entries จำนวนมาก ตรวจ slow query และ index usage ก่อนตัดสินใจเพิ่ม

04Atomicity, Transactions และ Consistency

MongoDB รับประกัน atomicity ระดับ document ดังนั้นการ embed ช่วยลดความจำเป็นของ multi-document transaction หาก workflow ต้องเปลี่ยนหลาย document พร้อมกันสามารถใช้ transaction ได้ แต่มีต้นทุนและไม่ควรใช้เพื่อชดเชย schema ที่ไม่ตรง access pattern

Read concern กำหนดระดับความสอดคล้องของการอ่าน ส่วน write concern กำหนดว่าต้องรอ acknowledgement จาก replica มากเท่าไร การเลือกต้องสัมพันธ์กับ business risk เช่นข้อมูลชำระเงินควรให้ durability สูงกว่าข้อมูล analytics ชั่วคราว

05Replication และ Sharding

Replica set ให้ high availability และ failover ส่วน sharding กระจายข้อมูลและ throughput การเลือก shard key ต้องดู cardinality, distribution และ query targeting key แบบ monotonic อาจทำให้ write ไปรวมที่ shard เดียว ขณะที่ key แบบสุ่มกระจายดีแต่ range query ยากขึ้น

อ่านเอกสารทางการ: MongoDB Embedded Data Models ↗