MongoDB รวมเงื่อนไข query โดยใช้ logical operator ได้แก่ $and, $or, $not, $nor รวมถึงการ AND โดยปริยายของหลาย field ใน filter สิ่งเหล่านี้ช่วยให้คุณแสดง logic ของ query ที่ซับซ้อนเพื่อค้นหา document ที่คุณต้องการได้อย่างแม่นยำ
MongoDB รวมเงื่อนไข query โดยใช้ logical operator ได้แก่ $and, $or, $not, $nor รวมถึงการ AND โดยปริยายของหลาย field ใน filter สิ่งเหล่านี้ช่วยให้คุณแสดง logic ของ query ที่ซับซ้อนเพื่อค้นหา document ที่คุณต้องการได้อย่างแม่นยำ
// multiple fields in one filter = AND (all must match)
db.users.find({ active: true, age: { $gt: 18 } });
// → active = true AND age > 18 (no explicit $and needed)
กรณีที่ง่ายที่สุด: การระบุหลายเงื่อนไขใน filter จะ AND กันโดยปริยาย ทั้งหมด/ทั้งคู่ต้องตรงกัน
// $or takes an array of conditions; a document matches if ANY is true
db.users.find({
$or: [
{ age: { $lt: 18 } },
{ age: { $gt: 65 } }
]
});
// → age < 18 OR age > 65
// usually implicit, but $and is needed when combining conditions on the SAME field
db.products.find({
$and: [
{ price: { $gt: 100 } },
{ price: { $lt: 500 } }
]
});
// (often you can write { price: { $gt: 100, $lt: 500 } } instead — cleaner)
// $and is also useful to combine $or groups:
db.users.find({
$and: [
{ $or: [{ role: "admin" }, { role: "editor" }] },
{ active: true }
]
});
db.products.find({ price: { $not: { $gt: 100 } } }); // NOT (price > 100) → price <= 100 or missing
db.users.find({ $nor: [{ active: true }, { premium: true }] }); // NEITHER condition true
// real queries combine logical + comparison operators
db.orders.find({
status: "shipped", // implicit AND
$or: [
{ total: { $gt: 1000 } },
{ priority: "high" }
]
});
// → status = shipped AND (total > 1000 OR priority = high)
Logical operator เป็นสิ่งจำเป็นสำหรับการ แสดงเงื่อนไข query ที่ซับซ้อน ใน MongoDB query จริงมักต้องการมากกว่าเงื่อนไขเดียว (การรวมหลายเกณฑ์ด้วย logic AND/OR) ดังนั้นการเข้าใจสิ่งเหล่านี้จึงเป็นความรู้พื้นฐานที่ใช้ในชีวิตประจำวันสำหรับการกรองข้อมูลอย่างมีประสิทธิภาพ
การรู้ implicit AND ของหลาย field ใน filter (กรณีที่ง่ายและพบบ่อยที่สุด การระบุเงื่อนไขที่ทั้งหมดต้องตรงกัน), $or (การ match เงื่อนไขใด ๆ ในหลายเงื่อนไข ความต้องการที่พบบ่อยสำหรับทางเลือก), $and (AND แบบชัดเจน จำเป็นสำหรับการรวมเงื่อนไขบน field เดียวกันหรือการจัดกลุ่ม clause $or) และ $not/$nor (การปฏิเสธ) ครอบคลุมองค์ประกอบพื้นฐานทาง logic สำหรับการแสดงข้อกำหนดของ query
การเข้าใจวิธีการ รวม logical และ comparison operator (การสร้าง query เช่น "status shipped AND (total > 1000 OR priority high)") เป็นสิ่งจำเป็นสำหรับ query แบบหลายเงื่อนไขที่สมจริงที่แอปพลิเคชันต้องการ
เนื่องจากการกรองข้อมูลด้วยเงื่อนไขที่รวมกันเป็นความต้องการที่พบบ่อย (การค้นหา document ที่ตรงกับเกณฑ์ที่ซับซ้อน) และเนื่องจาก logical operator ของ MongoDB (implicit AND, $or, $and, $not/$nor) เป็นวิธีที่คุณแสดง logic นี้ รวมกับ comparison operator สำหรับเงื่อนไขจริง การเข้าใจ logical operator จึงเป็นความรู้ MongoDB ที่จำเป็นและใช้บ่อยสำหรับการเขียน query ที่มีประสิทธิภาพ เทียบเท่ากับการเชี่ยวชาญ AND/OR/NOT ใน WHERE clause ของ SQL และจำเป็นสำหรับการค้นหา document ที่คุณต้องการได้อย่างแม่นยำด้วยเงื่อนไขที่มักซับซ้อนที่แอปพลิเคชันจริงต้องการ