简体   繁体   中英

Issue with Pagination in nodejs with mongoose and query optimization

Having issue with database query in mongoose I am setting value but not getting correct not sure why, Also want to optimize the database query. I am using mongoose for counting how many records are there with matching query params(pagination) I have to make separate query. and finding the actual records with model.find({}) have to make separate query.

But actual problem is with pagination details I am trying to get Example in below code if I set page = 1, page_size = 10 and my row_count is 3 then I suppose to get from 1, and to 1 but instead I am getting from 1 and to 11 .

Not sure what I am doing wrong here.

const pagination = async (model, query, page_number, page_size, order, order_by, next) => {
    const pageS = parseInt(page_number)
    let page = +pageS || 1;
    const limit = parseInt(page_size)
    let per_page = +page_size || 10;

    if (page < 1) {

        page = 1;

    }

    if (per_page < 1) {

        per_page = 1;

    }

    const startIndex = (
      page - 1
    ) * per_page;

    const endIndex = page * page_size
    const key = `${order}`
    const results = {}

    // here reading the data count from database
    const resultCount = await model.countDocuments(query).exec();

    if (endIndex < resultCount) {
        results.next = {
          page: page + 1,
          page_size: limit
        }
      }

    if (startIndex > 0) {
        results.previous = {
          page: page - 1,
          page_size: limit
        }
      }

      try {
      // here trying to search the query with applied pagination
        const data = await model.find(query)
            .limit(per_page)
            .skip(startIndex)
            .sort({ [key] : order_by })
            .exec()

        // here I am passing details  but not getting exact to and from; from is working expected but not to

        // Example if I set page = 1, page_size = 10 and my row_count is 3 then I suppose to get from 1, and to 1 but intead I am getting from 1 and to 11.  
        const pagination_details = {
            data: data,
            meta: {
                page,
                page_size: per_page,
                row_count: parseInt(resultCount, 10),
                page_count: Math.ceil(resultCount / per_page ),
                from:startIndex + 1,
                to: endIndex + 1,
                order: order,
                order_by: order_by
            }
        }
        return pagination_details
        next()
      } catch (e) {
        console.log(e);
        console.error(e);
      }

};

Can anyone help me here to achieve the right data, what I am making mistake here. Might be something logical mistake

You have forgotten to divide the start and end indexes by the per_page to get page numbers, try replacing:

                from:startIndex + 1,
                to: endIndex + 1,

with:

                from: Math.floor(startIndex / per_page) + 1,
                to: Math.ceil(endIndex / per_page) + 1,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM