从MongoDB集合中以10个为块加载文档的最简单方法 [英] Easiest way to load documents from MongoDB collection in chunks of 10

查看:59
本文介绍了从MongoDB集合中以10个为块加载文档的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个创建订单的应用程序,然后将其加载到列表中.问题是我无法一次加载所有订单.我想以10乘10加载它们.

I want to create an application that creates orders and then loads them on a list. The problem is that I cannot load all orders at once. I want to load them 10 by 10.

但是,这在MongoDB中很难做到,因为没有像SQL那样的自动ID.我知道如何模拟自动ID,但我认为实现此功能应该更容易.

However, this looks very difficult to do in MongoDB, as there are no automatic id like in SQL. I know how to simulate an automatic id, but I think it should be easier to implement this feature.

那么,对于一个MongoDB集合,我如何从最新的一个开始到开始的10个10个加载文档?

So, given a MongoDB collection, how do I load documents 10 by 10, starting from the latest one up to the beggining?

推荐答案

您需要按字段对文档进行排序,然后使用限制聚合.同样,要获取记录总数,我们可以使用 facet 聚合

You need to sort your document by a field then use skip & limit aggregation. Also to get the total number of records we can use facet aggregation.

这是详细的解释:

假设您有8个文档在订单集中.

Let's say you have these 8 documents in orders collection.

[
    {
        "_id": "5e390fc33285e463a0799689",
        "customer": "Max",
        "total": 10,
        "orderDate": "2020-02-04T06:31:31.311Z",
        "__v": 0
    },
    {
        "_id": "5e390fd03285e463a079968a",
        "customer": "John",
        "total": 9.2,
        "orderDate": "2020-02-04T06:31:44.190Z",
        "__v": 0
    },
    {
        "_id": "5e390fda3285e463a079968b",
        "customer": "Smith",
        "total": 11.3,
        "orderDate": "2020-02-04T06:31:54.248Z",
        "__v": 0
    },
    {
        "_id": "5e390fdf3285e463a079968c",
        "customer": "Smith",
        "total": 12.3,
        "orderDate": "2020-02-04T06:31:59.993Z",
        "__v": 0
    },
    {
        "_id": "5e390fec3285e463a079968d",
        "customer": "Jimmy",
        "total": 15.6,
        "orderDate": "2020-02-04T06:32:12.336Z",
        "__v": 0
    },
    {
        "_id": "5e390ffd3285e463a079968e",
        "customer": "Wesley",
        "total": 11,
        "orderDate": "2020-02-04T06:32:29.670Z",
        "__v": 0
    },
    {
        "_id": "5e3910163285e463a079968f",
        "customer": "Adam",
        "total": 6.1,
        "orderDate": "2020-02-04T06:32:54.131Z",
        "__v": 0
    },
    {
        "_id": "5e3910213285e463a0799690",
        "customer": "Michael",
        "total": 7.2,
        "orderDate": "2020-02-04T06:33:05.166Z",
        "__v": 0
    }
]

如果要分批获取这些文档,可以编写如下示例路径:

If we wanted to get these documents in chunks, we can write a sample route like this:

router.get("/orders", async (req, res) => {
  const page = req.query.pageIndex ? +req.query.pageIndex : 1;
  const limit = req.query.pageSize ? +req.query.pageSize : 10;
  const skip = (page - 1) * limit;

  const result = await Order.aggregate([
    {
      $sort: {
        orderDate: -1
      }
    },
    {
      $facet: {
        totalRecords: [{ $count: "total" }],
        data: [{ $skip: skip }, { $limit: limit }]
      }
    }
  ]);
  res.send(result);
});

我们像这样http://...../orders?pageIndex=1&pageSize=3

当我们使用pageIndex=1pageSize=3时,结果将是这样的:(如您所见,我们还返回记录总数,以便客户端可以建立分页号)

When we use pageIndex=1 and pageSize=3, the result will be like this: (as you see we also return the total number of records so that client can build the pagination numbers)

[
    {
        "totalRecords": [
            {
                "total": 8
            }
        ],
        "data": [
            {
                "_id": "5e3910213285e463a0799690",
                "customer": "Michael",
                "total": 7.2,
                "orderDate": "2020-02-04T06:33:05.166Z",
                "__v": 0
            },
            {
                "_id": "5e3910163285e463a079968f",
                "customer": "Adam",
                "total": 6.1,
                "orderDate": "2020-02-04T06:32:54.131Z",
                "__v": 0
            },
            {
                "_id": "5e390ffd3285e463a079968e",
                "customer": "Wesley",
                "total": 11,
                "orderDate": "2020-02-04T06:32:29.670Z",
                "__v": 0
            }
        ]
    }
]

当我们使用pageIndex=2pageSize=3时,结果将如下所示:

When we use pageIndex=2 and pageSize=3, the result will be like this:

[
    {
        "totalRecords": [
            {
                "total": 8
            }
        ],
        "data": [
            {
                "_id": "5e390fec3285e463a079968d",
                "customer": "Jimmy",
                "total": 15.6,
                "orderDate": "2020-02-04T06:32:12.336Z",
                "__v": 0
            },
            {
                "_id": "5e390fdf3285e463a079968c",
                "customer": "Smith",
                "total": 12.3,
                "orderDate": "2020-02-04T06:31:59.993Z",
                "__v": 0
            },
            {
                "_id": "5e390fda3285e463a079968b",
                "customer": "Smith",
                "total": 11.3,
                "orderDate": "2020-02-04T06:31:54.248Z",
                "__v": 0
            }
        ]
    }
]

当我们使用pageIndex=3pageSize=3时,结果将如下所示:

When we use pageIndex=3 and pageSize=3, the result will be like this:

[
    {
        "totalRecords": [
            {
                "total": 8
            }
        ],
        "data": [
            {
                "_id": "5e390fd03285e463a079968a",
                "customer": "John",
                "total": 9.2,
                "orderDate": "2020-02-04T06:31:44.190Z",
                "__v": 0
            },
            {
                "_id": "5e390fc33285e463a0799689",
                "customer": "Max",
                "total": 10,
                "orderDate": "2020-02-04T06:31:31.311Z",
                "__v": 0
            }
        ]
    }
]

对于您的情况,您需要发送10作为pageSize值.

For your case, you need to send 10 as pageSize value.

这篇关于从MongoDB集合中以10个为块加载文档的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆