我需要在MongoDB中按分页对数据进行排序。

huangapple go评论84阅读模式
英文:

I will have to sort data in mongodb sort data by pagination

问题

I will have to sort data by data and according to the pagination.
This is the query which I used:

response.data = await distributorDoc.find().sort({"TimeStamp":-1,});

It is the pagination:

pagination: {
    totalCount: 0,
    pageCount: 0,
    currentPage: page,
    perPage: reqData.perPage || perPageCount
}
response.data = await distributorDoc.find().sort({"TimeStamp":-1,"perpage"==100});
英文:

I will have to sort data by data and acccording to the pagination.
This is the query which I used
response.data = await distributorDoc.find().sort({"TimeStamp":-1,});
It is the pagination
pagination: {
totalCount: 0,
pageCount: 0,
currentPage: page,
perPage: reqData.perPage || perPageCount
}

response.data = await distributorDoc.find().sort({"TimeStamp":-1,"perpage"==100});

答案1

得分: 1

你可以尝试使用 MongoDB 中的 limitskip 方法。

在 MongoDB 中,limit() 函数用于指定要返回的结果的最大数量。

如果您希望在某些文档之后获取特定数量的结果,可以使用 skip() 函数。

用于分页的示例 Node.js 代码:

function fetchDocs(pageNumber, nPerPage) {
    console.log('Page: ' + pageNumber);
    distributorDoc.find()
      .sort({'TimeStamp': -1})
      .skip(pageNumber > 0 ? ((pageNumber - 1) * nPerPage) : 0)
      .limit(nPerPage)
      .forEach(doc => {
          console.log(doc);
      });
 }

在这里阅读更多关于它们的信息:

limit()

skip()

查看这个链接以了解其他替代方法。

希望这是你正在寻找的。

英文:

You could try with limit and skip methods from MongoDB.

The limit() function in MongoDB is used to specify the maximum number of results to be returned

If you want to get certain number of results after some documents, you could use skip() function.

Sample Node JS code for pagination:

    function fetchDocs(pageNumber, nPerPage) {
        console.log('Page: ' + pageNumber);
        distributorDoc.find()
          .sort({'TimeStamp': -1})
          .skip(pageNumber > 0 ? ((pageNumber - 1) * nPerPage) : 0)
          .limit(nPerPage)
          .forEach(doc => {
              console.log(doc);
          });
     }

Read more about them here

limit()

skip()

Checkout this link for other alternatives

Hope this is what you're looking for.

答案2

得分: 1

以下是一个示例,假设您有以下变量:

page:当前页码。
perPage:每页要显示的项目数。
distributorDoc:用于分销商文档的Mongoose模型。

// 根据请求数据设置page和perPage变量,或使用默认值。
const page = reqData.page || 1;
const perPage = reqData.perPage || perPageCount;

// 使用countDocuments()方法计算totalCount。
const totalCount = await distributorDoc.countDocuments();

// 通过将totalCount除以perPage并四舍五入,计算pageCount。
const pageCount = Math.ceil(totalCount / perPage);

// 计算skipItems以确定根据当前页码跳过多少项目。
const skipItems = (page - 1) * perPage;

// 修改查询以包括.skip(skipItems)和.limit(perPage)以实现分页。
response.data = await distributorDoc.find()
  .sort({ "TimeStamp": -1 })
  .skip(skipItems)
  .limit(perPage);

// 使用新值更新response.pagination对象。
response.pagination = {
  totalCount: totalCount,
  pageCount: pageCount,
  currentPage: page,
  perPage: perPage
};
英文:

Here's an example of how you can do it assuming you have the following variables:

page: The current page number.
perPage: The number of items you want to show per page.
distributorDoc: The Mongoose model for your distributor document.

// Set the page and perPage variables based on the request data or use the default values.
const page = reqData.page || 1;
const perPage = reqData.perPage || perPageCount;

// Calculate the totalCount using the countDocuments() method.
const totalCount = await distributorDoc.countDocuments();

// Calculate the pageCount by dividing the totalCount by perPage and rounding up.
const pageCount = Math.ceil(totalCount / perPage);

// Calculate the skipItems to determine how many items to skip based on the current page number.
const skipItems = (page - 1) * perPage;

// Modify the query to include .skip(skipItems) and .limit(perPage) for implementing pagination.
response.data = await distributorDoc.find()
  .sort({ "TimeStamp": -1 })
  .skip(skipItems)
  .limit(perPage);

// Update the response.pagination object with the new values.
response.pagination = {
  totalCount: totalCount,
  pageCount: pageCount,
  currentPage: page,
  perPage: perPage
};

huangapple
  • 本文由 发表于 2023年3月31日 16:27:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定