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

$150K MiniMax AI Agent Challenge — Build Smarter, Remix Bolder, Win Bigger!

Join the $150k MiniMax AI Agent Challenge — Build your first AI Agent 🤖

Developers, innovators, and AI tinkerers, build your AI Agent and win $150,000 in cash. 💰

Read more →

Top comments (0)

Short-term memory for faster AI agents

Short-term memory for faster AI agents

AI agents struggle with latency and context switching. Redis fixes it with a fast, in-memory layer for short-term context—plus native support for vectors and semi-structured data to keep real-time workflows on track.

Start building

👋 Kindness is contagious

Explore this insightful piece, celebrated by the caring DEV Community. Programmers from all walks of life are invited to contribute and expand our shared wisdom.

A simple "thank you" can make someone’s day—leave your kudos in the comments below!

On DEV, spreading knowledge paves the way and fortifies our camaraderie. Found this helpful? A brief note of appreciation to the author truly matters.

Let’s Go!