Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 5, 2021 02:49 am GMT

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 resulsets 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

/**@funclimit 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


Original Link: https://dev.to/functional_js/nodejs-expressjs-redis-mongodb-series-04-jle

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To