การสืบค้นและกรองข้อมูลด้วย Mongoose
ก้าวไปไกลกว่าการทำ CRUD พื้นฐาน แล้วเรียนรู้การสร้างคำสืบค้นประสิทธิภาพสูงใน Mongoose ด้วยตัวกรอง ตัวดำเนินการ การเรียงลำดับ การแบ่งหน้า และการเลือกฟิลด์
การสืบค้นและกรองข้อมูลด้วย Mongoose เป็นบทเรียน Node.js Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Node.js Backend Development Bootcamp และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Beyond Find-All
Fetching every document with Model.find() works for tiny collections, but real apps need to filter, sort, limit, and shape their results.
Mongoose gives you a fluent query API that mirrors MongoDB's query language.
const users = await User.find({ active: true });Filtering by Field
Pass an object to find() to match documents by exact field values. Multiple keys are combined with logical AND.
const admins = await User.find({ role: 'admin', active: true });Comparison Operators
MongoDB operators start with $. The most common comparisons are:
$gt/$gtegreater than (or equal)$lt/$lteless than (or equal)$nenot equal
const adults = await User.find({ age: { $gte: 18 } });Matching Multiple Values
Use $in to match any value from a list, and $nin to exclude a list. This is cleaner than chaining many OR conditions.
const team = await User.find({ role: { $in: ['admin', 'editor'] } });Logical OR
The $or operator takes an array of conditions; a document matches if any one is true.
const results = await User.find({
$or: [{ city: 'Berlin' }, { city: 'Paris' }]
});Sorting Results
Chain .sort() to order results. Use 1 for ascending and -1 for descending. You can sort by multiple fields.
const recent = await User.find().sort({ createdAt: -1, name: 1 });Limiting and Skipping
Pagination relies on .limit() (max documents) and .skip() (how many to bypass). For page 2 with 10 per page, skip 10 and limit 10.
const page = 2, perPage = 10;
const items = await User.find()
.skip((page - 1) * perPage)
.limit(perPage);Selecting Specific Fields
Use .select() (projection) to return only the fields you need. Prefix a field with - to exclude it. This reduces payload size and hides sensitive data.
const safe = await User.find().select('name email -_id');
const noPass = await User.find().select('-password');Finding a Single Document
findOne() returns the first match or null. findById() is a shortcut for matching by _id.
const user = await User.findOne({ email: 'a@b.com' });
const byId = await User.findById('652f1c...');Counting Documents
To know how many documents match a filter without fetching them all, use countDocuments(). This is essential for showing total pages in pagination.
const total = await User.countDocuments({ active: true });
console.log('Active users:', total);Text & Regex Search
For partial matches, use a regular expression. The $regex operator with the i option makes a case-insensitive search.
const matches = await User.find({
name: { $regex: 'an', $options: 'i' }
});Quick Check
Test your Mongoose querying skills.
Recap
You can now build rich Mongoose queries:
- Filter by field and combine with operators like
$gte,$in,$or - Order results with
.sort() - Paginate with
.skip()and.limit() - Shape output with
.select() - Count matches with
countDocuments()
These tools turn a basic data layer into a flexible, production-ready API.
คำถามที่พบบ่อย
บทเรียน “การสืบค้นและกรองข้อมูลด้วย Mongoose” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสืบค้นและกรองข้อมูลด้วย Mongoose” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Node.js Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสืบค้นและกรองข้อมูลด้วย Mongoose”
ก้าวไปไกลกว่าการทำ CRUD พื้นฐาน แล้วเรียนรู้การสร้างคำสืบค้นประสิทธิภาพสูงใน Mongoose ด้วยตัวกรอง ตัวดำเนินการ การเรียงลำดับ การแบ่งหน้า และการเลือกฟิลด์ คุณปฏิบัติ Node.js Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Node.js Backend Development Bootcamp หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Node.js Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การสืบค้นและกรองข้อมูลด้วย Mongoose” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Node.js Backend Development Bootcamp นี้ได้ไหม
ได้ บทเรียน Node.js Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เชื่อมต่อ Node.js กับ MongoDB
- ODM Mongoose สำหรับสร้างแบบจำลองข้อมูล
- การดำเนินการ CRUD ด้วย Mongoose
- การสืบค้นและกรองข้อมูลด้วย Mongoose