DEV Community

loading...
Cover image for NodeJS, ExpressJS,  MongoDB - Paginate - series #04

NodeJS, ExpressJS, MongoDB - Paginate - series #04

functional_js profile image Functional Javascript Updated on ・2 min read

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

Discussion (0)

pic
Editor guide