Cara menggunakan nodejs mongodb skip limit

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




    Post.find().limit(5).then(posts=>{

        if

    touch app.js
    0

    touch app.js
    1
    touch app.js
    2
    touch app.js
    3
    touch app.js
    4
    touch app.js
    5
    touch app.js
    6

    touch app.js
    1
    touch app.js
    8

    touch app.js
    1
    npm init
    0
    npm init
    1

        

    npm init
    3

        

    npm init
    5
    npm init
    6

    npm init
    7
    npm init
    8
    npm init
    9

    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: 

    1. Setting up environment
    2. Creating Post schema with mongoose
    3. Developing API for post creation
    4. Developing API for fetching the specified number of post from database

    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. 

    Cara menggunakan nodejs mongodb skip limit

    Follow the below steps one by one to achieve the target: 

    Step 1: Setting up the app.js. 

    Filename: app.js

    Javascript




    npm i express body-parser mongoose
    0
    npm i express body-parser mongoose
    1
    npm i express body-parser mongoose
    2

    npm i express body-parser mongoose
    3
    npm i express body-parser mongoose
    4
    npm i express body-parser mongoose
    2

    npm i express body-parser mongoose
    6
    npm i express body-parser mongoose
    7
    npm i express body-parser mongoose
    2

    npm i express body-parser mongoose

    node .\app.js  
    0

    npm i express body-parser mongoose

    node .\app.js  
    2

    node .\app.js  
    3

    npm i express body-parser mongoose

    node .\app.js  
    5

    node .\app.js  
    6

    node .\app.js  
    7
    node .\app.js  
    8
    node .\app.js  
    9
    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
    0
    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
    1
    npm i express body-parser mongoose
    2

    node .\app.js  
    7
    node .\app.js  
    8
    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
    5
    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
    0
    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
    7
    npm i express body-parser mongoose
    2

    node .\app.js  
    7
    node .\app.js  
    8
    URL - http://localhost:8000/posts/?count={limit}
    Method - GET
    Example Request - http://localhost:8000/posts/?count=4
    1
    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
    0
    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
    1
    npm i express body-parser mongoose
    2

    node .\app.js  
    7
    URL - http://localhost:8000/posts/?count={limit}
    Method - GET
    Example Request - http://localhost:8000/posts/?count=4
    6

    URL - http://localhost:8000/posts/?count={limit}
    Method - GET
    Example Request - http://localhost:8000/posts/?count=4
    7

    npm i express body-parser mongoose

    URL - http://localhost:8000/posts/?count={limit}
    Method - GET
    Example Request - http://localhost:8000/posts/?count=4
    9Post.find().limit(5).then(posts=>{0Post.find().limit(5).then(posts=>{1

    node .\app.js  
    7Post.find().limit(5).then(posts=>{3Post.find().limit(5).then(posts=>{4
    npm i express body-parser mongoose
    2

    URL - http://localhost:8000/posts/?count={limit}
    Method - GET
    Example Request - http://localhost:8000/posts/?count=4
    7

    npm i express body-parser mongoose

    Post.find().limit(5).then(posts=>{8

    node .\app.js  
    7    0    1    2

    node .\app.js  
    7    4    5    6

    Run the app.js file

    node .\app.js  

    Output: Now on hitting localhost:8000/test, you should expect.

    Cara menggunakan nodejs mongodb skip limit

    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




        7

    npm i express body-parser mongoose
    6
    npm i express body-parser mongoose
    7
    touch app.js
    6

    if

    if2

    if3

    touch app.js
    3 if5

    node .\app.js  
    7if7

        if9

    touch app.js
    00
    touch app.js
    01

    touch app.js
    00
    touch app.js
    03
    touch app.js
    04
    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
    0

        

    touch app.js
    07

        

    touch app.js
    09

    touch app.js
    00
    touch app.js
    01

    touch app.js
    00
    touch app.js
    03
    touch app.js
    04
    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
    0

        

    touch app.js
    07

    node .\app.js  
    7
    touch app.js
    07

    node .\app.js  
    7
    touch app.js
    21
    touch app.js
    04
    npm init
    3

    touch app.js
    6

    if

    touch app.js
    26

    touch app.js
    27
    touch app.js
    28
    touch app.js
    29

    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.

    Javascript




    touch app.js
    30

    touch app.js
    31
    touch app.js
    32
    touch app.js
    33

        

    touch app.js
    35
    touch app.js
    3
    touch app.js
    37

        

    touch app.js
    39

        

    touch app.js
    41

    node .\app.js  
    7
    touch app.js
    43

    node .\app.js  
    7
    touch app.js
    45

        

    touch app.js
    47

        

    touch app.js
    49

    touch app.js
    00
    npm init
    5
    touch app.js
    52

        

    touch app.js
    54

        

    touch app.js
    56
    npm init
    8
    touch app.js
    58

    touch app.js
    00
    touch app.js
    60

        

    touch app.js
    43

    touch app.js
    54

    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. 

    Javascript




    touch app.js
    64

    if

    URL - http://localhost:8000/posts/?count={limit}
    Method - GET
    Example Request - http://localhost:8000/posts/?count=4
    9
    touch app.js
    67
    touch app.js
    33

    node .\app.js  
    7
    touch app.js
    70

    node .\app.js  
    7
    touch app.js
    72

        

    touch app.js
    74

        

    touch app.js
    76

    touch app.js
    00
    touch app.js
    78

    touch app.js
    00if
    touch app.js
    81

    touch app.js
    1
    touch app.js
    83
    touch app.js
    3
    touch app.js
    4
    touch app.js
    86
    touch app.js
    6

    touch app.js
    1
    touch app.js
    89

    touch app.js
    1
    npm init
    0
    touch app.js
    92

    touch app.js
    00
    npm init
    3

    touch app.js
    00
    touch app.js
    96

    touch app.js
    00
    npm init
    5
    touch app.js
    99

        

    touch app.js
    54

        

    npm init
    03

    npm init
    04
    npm init
    05

        

    touch app.js
    54

        

    touch app.js
    56
    npm init
    8
    touch app.js
    58

    touch app.js
    1
    npm init
    13

    touch app.js
    1
    npm init
    15

        

    touch app.js
    43

    touch app.js
    54

    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.