0Pricing
MongoDB Academy · บทเรียน

การข้ามและการจำกัด: การแบ่งหน้าแบบออฟเซ็ต

ผู้เรียนจะสร้างการแบ่งหน้าแบบหมายเลขหน้าตามรูปแบบดั้งเดิมด้วย skip() และ limit() และวัดต้นทุนด้านประสิทธิภาพเมื่อใช้กับคอลเลกชันขนาดใหญ่

การข้ามและการจำกัด: การแบ่งหน้าแบบออฟเซ็ต เป็นบทเรียน MongoDB Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน MongoDB Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส MongoDB Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What Is Offset Pagination?

Offset pagination—also called page-number pagination—divides results into fixed-size pages and uses a page number to determine how far into the result set to start. Page 1 shows items 1-20, page 2 shows items 21-40, and so on. In MongoDB this is implemented with skip() to jump over earlier results and limit() to restrict how many documents are returned per page.

Using skip() and limit()

limit(N) tells the cursor to return at most N documents. skip(N) tells MongoDB to skip the first N documents before returning any results. Together they implement page-number pagination: to get page P with size S items per page, use skip((P-1)*S) and limit(S).

const PAGE = 3;
const PAGE_SIZE = 20;

// Page 3 of 20 results per page
const products = await db.collection('products')
  .find({ isActive: true })
  .sort({ createdAt: -1 })
  .skip((PAGE - 1) * PAGE_SIZE)  // skip 40 docs (pages 1 and 2)
  .limit(PAGE_SIZE)               // return next 20
  .toArray();

console.log('Page 3 results:', products.length);

Counting Total Pages

Offset pagination usually requires a total document count to display page numbers in the UI. Use countDocuments(filter) to count matching documents before applying pagination. Run the count and the paged query in parallel to avoid adding latency. The total count divided by the page size (rounded up) gives the total number of pages.

const filter = { isActive: true };

const [total, results] = await Promise.all([
  db.collection('products').countDocuments(filter),
  db.collection('products')
    .find(filter)
    .sort({ createdAt: -1 })
    .skip((PAGE - 1) * PAGE_SIZE)
    .limit(PAGE_SIZE)
    .toArray()
]);

const totalPages = Math.ceil(total / PAGE_SIZE);
console.log('Total:', total, 'Pages:', totalPages);

The Hidden Cost of skip()

MongoDB implements skip() by scanning and discarding the first N documents. Even with an index, MongoDB must walk through and count N index entries before returning results. On page 1, skip is 0—fast. On page 500 with 20 items per page, skip is 9980—MongoDB must traverse nearly 10,000 entries just to find where page 500 starts. This is the core performance problem with offset pagination.

O(skip + limit) Query Complexity

The time to execute a skip/limit query grows linearly with the skip amount. The query cost is O(skip + limit)—the server must examine skip documents before returning limit documents. For page 1 this is O(20); for page 1000 with 20 items per page it is O(20020). As users navigate to higher page numbers, queries get progressively slower, often going from milliseconds to seconds on large collections.

When Offset Pagination Is Acceptable

Despite its performance limitations, offset pagination is acceptable in these scenarios: (1) the collection has fewer than a few thousand documents; (2) users rarely navigate beyond the first few pages; (3) the feature requires jumping directly to a page number (e.g., 'go to page 47'). Many admin dashboards and search results with low page depth fit this profile. Use keyset pagination for infinite scroll or large data sets.

Implementing an API Endpoint With Offset Pagination

A typical REST list endpoint accepts page and limit query parameters, validates them, and applies skip/limit accordingly. Always cap the maximum limit to prevent clients from requesting thousands of documents in a single call, which would exhaust server memory.

// Express route: GET /api/products?page=2&limit=20
async function listProducts(req, res) {
  const page  = Math.max(1, parseInt(req.query.page)  || 1);
  const limit = Math.min(100, parseInt(req.query.limit) || 20); // cap at 100
  const skip  = (page - 1) * limit;

  const [total, items] = await Promise.all([
    Product.countDocuments({ isActive: true }),
    Product.find({ isActive: true }).sort('-createdAt').skip(skip).limit(limit).lean()
  ]);

  res.json({ page, limit, total, totalPages: Math.ceil(total / limit), items });
}

Data Consistency Issues With Offset Pagination

Offset pagination has a subtle correctness problem: if documents are inserted or deleted between page requests, items can shift positions in the sorted result set. A document inserted between page 1 and page 2 pushes every subsequent document forward, causing one item to appear on both pages (duplicate) or be skipped entirely. This is called the page drift problem and is inherent to offset pagination.

Estimating vs Exact Count

For very large collections, countDocuments(filter) can be slow because it scans the index. An alternative is estimatedDocumentCount(), which is O(1) but counts all documents in the collection without applying a filter. For simple cases where you want the total without filtering, the estimated count is much faster. For filtered counts on large collections, consider caching the count or using Atlas's faceted search.

// O(1) but no filter support
const approxTotal = await db.collection('products').estimatedDocumentCount();

// Exact count with filter (slower on large collections)
const exactTotal = await db.collection('products').countDocuments({ isActive: true });

Combining skip/limit With Projections

Always combine pagination with a tight projection for list endpoints. Fetching all fields while paginating defeats the purpose: you're still paying to transfer full document payloads for every page. A projection that returns only summary fields (name, price, thumbnail) reduces bandwidth by 80-95% compared to fetching full documents, making pagination viable at larger page offsets.

const SUMMARY = { _id: 1, name: 1, price: 1, thumbnailUrl: 1, rating: 1 };

const items = await db.collection('products')
  .find({ isActive: true })
  .projection(SUMMARY)
  .sort({ rating: -1 })
  .skip((PAGE - 1) * PAGE_SIZE)
  .limit(PAGE_SIZE)
  .toArray();

When to Switch to Keyset Pagination

Switch from offset to keyset (cursor) pagination when: users scroll infinitely through results (no page numbers needed), the collection has more than 100,000 documents, page load times increase noticeably for higher page numbers, or data changes frequently between page requests. Keyset pagination is always O(log n) regardless of position in the result set, because it uses a range query on an indexed field instead of skip.

Quick Check

Test your understanding of MongoDB & NoSQL Databases concepts from this lesson.

Lesson Recap

In this lesson you learned: offset pagination uses skip((page-1)*size) and limit(size) to fetch a page, skip() scans and discards documents so deep pages become progressively slower, and offset pagination is acceptable for small collections or shallow page depths but keyset pagination is better at scale. Next up we implement keyset pagination with range queries for consistent O(log n) performance.

คำถามที่พบบ่อย

บทเรียน “การข้ามและการจำกัด: การแบ่งหน้าแบบออฟเซ็ต” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การข้ามและการจำกัด: การแบ่งหน้าแบบออฟเซ็ต” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส MongoDB Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส MongoDB Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การข้ามและการจำกัด: การแบ่งหน้าแบบออฟเซ็ต”

ผู้เรียนจะสร้างการแบ่งหน้าแบบหมายเลขหน้าตามรูปแบบดั้งเดิมด้วย skip() และ limit() และวัดต้นทุนด้านประสิทธิภาพเมื่อใช้กับคอลเลกชันขนาดใหญ่ คุณปฏิบัติ MongoDB Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน MongoDB Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน MongoDB Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การข้ามและการจำกัด: การแบ่งหน้าแบบออฟเซ็ต” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน MongoDB Academy นี้ได้ไหม

ได้ บทเรียน MongoDB Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การเรียงลำดับด้วย sort() และหลายคีย์
  2. การข้ามและการจำกัด: การแบ่งหน้าแบบออฟเซ็ต
  3. การแบ่งหน้าแบบคีย์เซ็ตด้วยการค้นหาแบบช่วง
  4. การรวม sort, skip, limit และโพรเจกชัน
← กลับไปที่ MongoDB Academy