Say you are working on a quiz API, with which anybody on the web can fetch random questions based on the defined category. You are done with your API and it has passed all test parameters, now for sake of users you want to add a feature that will let users decide how many questions they want to address and your API will provide them with that amount of questions. Here, comes the helpful mongo feature limit. Show Referring to mongo documentation, the limit can be understood as an option that limits the number of documents that has to be passed to the next pipeline for processing. Syntax: { $limit: <positive integer> } For Node.js we will use mongoose for this specific purpose. Assuming that you created a Post model for a Content Management System, let’s try fetching a certain number of posts using the find() method. Javascript
touch app.js0 touch app.js1 touch app.js2 touch app.js3 touch app.js4 touch app.js5 touch app.js6 touch app.js1 touch app.js8 touch app.js1 npm init0 npm init1
npm init3
npm init5 npm init6 npm init7 npm init8 npm init9 Pass the number of documents you want to fetch as an argument to limit. That’s a quite abstraction right? Worry not let’s walk through the process from the very beginning. Assuming a decent foundation of Node JS, express and mongoose. Starting off with creating posts and fetching posts as per defined limit. Workflow for follow along will be:
Setting up: Boiler Plate: touch app.js Setting up the NPM setup: npm init Installing dependencies: npm i express body-parser mongoose Project structure: Now, your file tree will look somewhat. Follow the below steps one by one to achieve the target: Step 1: Setting up the app.js. Filename: app.js Javascriptnpm i express body-parser mongoose0 npm i express body-parser mongoose1 npm i express body-parser mongoose2 npm i express body-parser mongoose3 npm i express body-parser mongoose4 npm i express body-parser mongoose2 npm i express body-parser mongoose6 npm i express body-parser mongoose7 npm i express body-parser mongoose2 npm i express body-parser mongoose9 node .\app.js0 npm i express body-parser mongoose9 node .\app.js2 node .\app.js3 npm i express body-parser mongoose9 node .\app.js5 node .\app.js6 node .\app.js7 node .\app.js8 node .\app.js9 URL - http://localhost:8000/post Method - POST Content-Type - application/json Body of the request should contain - i. title - title of your article ii. content - content of your article0 URL - http://localhost:8000/post Method - POST Content-Type - application/json Body of the request should contain - i. title - title of your article ii. content - content of your article1 npm i express body-parser mongoose2 node .\app.js7 node .\app.js8 URL - http://localhost:8000/post Method - POST Content-Type - application/json Body of the request should contain - i. title - title of your article ii. content - content of your article5 URL - http://localhost:8000/post Method - POST Content-Type - application/json Body of the request should contain - i. title - title of your article ii. content - content of your article0 URL - http://localhost:8000/post Method - POST Content-Type - application/json Body of the request should contain - i. title - title of your article ii. content - content of your article7 npm i express body-parser mongoose2 node .\app.js7 node .\app.js8 URL - http://localhost:8000/posts/?count={limit} Method - GET Example Request - http://localhost:8000/posts/?count=41 URL - http://localhost:8000/post Method - POST Content-Type - application/json Body of the request should contain - i. title - title of your article ii. content - content of your article0 URL - http://localhost:8000/post Method - POST Content-Type - application/json Body of the request should contain - i. title - title of your article ii. content - content of your article1 npm i express body-parser mongoose2 node .\app.js7 URL - http://localhost:8000/posts/?count={limit} Method - GET Example Request - http://localhost:8000/posts/?count=46 URL - http://localhost:8000/posts/?count={limit} Method - GET Example Request - http://localhost:8000/posts/?count=47 npm i express body-parser mongoose9 URL - http://localhost:8000/posts/?count={limit} Method - GET Example Request - http://localhost:8000/posts/?count=49 Post.find().limit(5).then(posts=>{ 0Post.find().limit(5).then(posts=>{ 1node .\app.js7 Post.find().limit(5).then(posts=>{ 3Post.find().limit(5).then(posts=>{ 4npm i express body-parser mongoose2 URL - http://localhost:8000/posts/?count={limit} Method - GET Example Request - http://localhost:8000/posts/?count=47 npm i express body-parser mongoose9
node .\app.js7 0 1 2node .\app.js7 4 5 6Run the app.js file node .\app.js Output: Now on hitting localhost:8000/test, you should expect. Now, when we are done with our basic app setting, lets create our required APIs. Creating Post Schema: Schemas can be easily created in Node using mongoose. Javascript
npm i express body-parser mongoose6 npm i express body-parser mongoose7 touch app.js6
touch app.js3 if 5node .\app.js7 if 7
touch app.js00 touch app.js01 touch app.js00 touch app.js03 touch app.js04 URL - http://localhost:8000/post Method - POST Content-Type - application/json Body of the request should contain - i. title - title of your article ii. content - content of your article0
touch app.js07
touch app.js09 touch app.js00 touch app.js01 touch app.js00 touch app.js03 touch app.js04 URL - http://localhost:8000/post Method - POST Content-Type - application/json Body of the request should contain - i. title - title of your article ii. content - content of your article0
touch app.js07 node .\app.js7 touch app.js07 node .\app.js7 touch app.js21 touch app.js04 npm init3 touch app.js6
touch app.js26 touch app.js27 touch app.js28 touch app.js29 API for post creation: Now, as we are done it creating the model and schemas we will move forward creating our first API and then testing it using Postman. Javascripttouch app.js30 touch app.js31 touch app.js32 touch app.js33
touch app.js35 touch app.js3 touch app.js37
touch app.js39
touch app.js41 node .\app.js7 touch app.js43 node .\app.js7 touch app.js45
touch app.js47
touch app.js49 touch app.js00 npm init5 touch app.js52
touch app.js54
touch app.js56 npm init8 touch app.js58 touch app.js00 touch app.js60
touch app.js43 touch app.js54 URL - http://localhost:8000/post Method - POST Content-Type - application/json Body of the request should contain - i. title - title of your article ii. content - content of your article Output: https://media.geeksforgeeks.org/wp-content/uploads/20210213002541/Postman-2021-02-13-00-10-25.mp4 API for fetching post as per the queried number of posts: Idea is to pass the count as a query in the API endpoint and then processing that query using basic validation and limit. Javascripttouch app.js64
URL - http://localhost:8000/posts/?count={limit} Method - GET Example Request - http://localhost:8000/posts/?count=49 touch app.js67 touch app.js33 node .\app.js7 touch app.js70 node .\app.js7 touch app.js72
touch app.js74
touch app.js76 touch app.js00 touch app.js78 touch app.js00 if touch app.js81 touch app.js1 touch app.js83 touch app.js3 touch app.js4 touch app.js86 touch app.js6 touch app.js1 touch app.js89 touch app.js1 npm init0 touch app.js92 touch app.js00 npm init3 touch app.js00 touch app.js96 touch app.js00 npm init5 touch app.js99
touch app.js54
npm init03 npm init04 npm init05
touch app.js54
touch app.js56 npm init8 touch app.js58 touch app.js1 npm init13 touch app.js1 npm init15
touch app.js43 touch app.js54 limits only accepts a number as an argument but by default query parameters is of string type which required us to typecast it into number. |