我想创建一个创建订单的应用程序,然后将其加载到列表中。问题是我无法一次加载所有订单。我想以10乘10加载它们。
但是,在MongoDB中很难做到这一点,因为没有像SQL这样的自动ID。我知道如何模拟自动ID,但我认为实现此功能应该更容易。
因此,对于一个MongoDB集合,我如何从最新的一个开始到开始的10个10个加载文档?
答案 0 :(得分:2)
您需要按字段对文档进行排序,然后使用skip和limit聚合。同样,为了获取记录总数,我们可以使用facet聚合。
以下是详细说明:
假设您有这8个文档在订单集中。
[
{
"_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
}
]
如果要分批获取这些文档,可以编写如下示例路径:
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);
});
我们在查询字符串中发送pageIndex和pageSize参数,例如http://...../orders?pageIndex=1&pageSize=3
当我们使用pageIndex=1
和pageSize=3
时,结果将是这样的:(如您所见,我们还返回记录的总数,以便客户可以建立分页号)
[
{
"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=2
和pageSize=3
时,结果将如下所示:
[
{
"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=3
和pageSize=3
时,结果将如下所示:
[
{
"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值。