DEV Community

Cover image for NodeJS, ExpressJS,  MongoDB - Paginate - series #04
Functional Javascript
Functional Javascript

Posted on • Edited on

1 1

NodeJS, ExpressJS, MongoDB - Paginate - series #04

Intro

A quick example on actually a very important feature: "paginate"

Always paginate your resultsets. This protects your system from accidental or malicious oversized resultsets being retrieved.

Pagination is very easy in MongoDB. See the notes section below.

app.post(apiEnum.api_find_artists__songRegex, async (req, res) => {
  let { searchTerm, page } = req.body;

  //#guard 1
  if (isNotBetween(page, 1, 500)) {
    page = 1; //defaultVal
  }

  //#guard 2
  if (isEmptyStrOrNil(searchTerm)) {
    return res.status(400).json([{ error: "the search term was empty" }]);
  }

  const regex = new RegExp(`${searchTerm}`, "i");
  res.json(await mgArr(dbEnum.nlpdb, collEnum.songsColl,
    copyField("searchResult", "albums"),
    unwindArr("searchResult"),
    unwindArr("searchResult.albumSongs"),
    matchRegex("searchResult.albumSongs.song", regex), //54
    paginate(50, page)
  ));
});
Enter fullscreen mode Exit fullscreen mode

Notes

  • See series #03 for an explanation of some of these stages like "copyField" and "unwindArr". Here we'll concentrate on the one database query stage, "paginate".

  • The above Node.js Express router endpoint returns the paged results of a user search for a string of characters in a song.

  • The paginate wrapper func wraps the skip and limit funcs

/**
@func
limit a resultset to a particular requested page of results

@param {number} lim - page size
@param {number} page - page number to retrieve
@return {object[]}
*/
export const paginate = (lim, page) => {
  return [
    skip(lim * (page - 1)), // 50 * 2 gets results 51 to 100
    limit(lim),
  ];
};
Enter fullscreen mode Exit fullscreen mode
  • The skip and limit funcs both wrap the MongoDB $skip and $limit pipeline stage operators
export const limit = lim => ({ $limit: lim });
Enter fullscreen mode Exit fullscreen mode
export const skip = n => ({ $skip: n });
Enter fullscreen mode Exit fullscreen mode
  • So paginate returns an arr of two stages because it uses two staging operators. You don't have think about that though.
    You only have to call paginate and pass in two numbers.

  • An example of the resultset in the UI:

song matches

What's Next

  • If you have any questions let me know

  • We'll keep moving the needle forward with more enterprise patterns in the subsequent articles in this series

Neon image

Set up a Neon project in seconds and connect from a Next.js application ⚡

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

Top comments (0)

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM apps—no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay