如何使用 MongoDB 聚合进行分页? [英] How to use MongoDB aggregation for pagination?
问题描述
我想执行一个进行基本分页的聚合查询:
- 查找属于某个
company_id
的所有订单 - 按
order_number
对订单进行排序 - 计算文档总数
- 跳到例如文档编号
100
并传递其余的 - 将文档数量限制为例如
2
并将它们传递给 - 最后返回计数和文档中选定的几个字段
这是查询的细分:
db.Order.collection.aggregate([
这会找到所有匹配的文档:
{ '$match' : { "company_id" : ObjectId("54c0...") } },
这对文档进行排序:
{ '$sort' : { 'order_number' : -1 } },
这会计算文档并传递未修改的文档,但我肯定做错了,因为从这里开始变得很奇怪:
<代码> {$组":{'_id':空,'计数':{'$sum':1},条目":{$push":$$ROOT"}}},
这似乎跳过了一些文件:
{ "$skip" : 100 },
这应该限制文档,但它没有:
{ "$limit" : 2 },
这确实返回计数,但它不会返回数组中的文档,而是返回包含每个字段的数组:
{ '$project' : {'计数':1,'entries' : {'_id' : "$entries._id", 'order_number' : "$entries.order_number"}}}])
结果如下:
<预><代码>[{_id":空,计数":300,条目":[{"_id" : [ObjectId('5a5c...'), ObjectId('5a5c...')],订单号":[4346",4345"]},{"_id" : [ObjectId('5a5c...'), ObjectId('5a5c...')],订单号":[4346",4345"]},...]}]我哪里弄错了?
要计算总计并返回子集,您需要对同一数据集应用分组和跳过/限制.为此,您可以使用 facets
例如显示第 3 页,每页 10 个文档:
db.Order.aggregate([{ '$match' : { "company_id" : ObjectId("54c0...") } },{ '$sort' : { 'order_number' : -1 } },{'$facet':{元数据: [ { $count: "total" }, { $addFields: { page: NumberInt(3) } } ],data: [ { $skip: 20 }, { $limit: 10 } ]//在这里添加投影希望你重新塑造文档} }])
它将返回一个包含 2 个字段的文档:
<代码>{元数据":[{总":300,页面":3}],数据" : [{... 原始文件 ...},{……另一个文件……},{... 最多 10 个文档 ...}]}
I want to perform an aggregation query that does basic pagination:
- Find all orders that belongs to a certain
company_id
- Sort the orders by
order_number
- Count the total number of documents
- Skips to e.g. document number
100
and passes on the rest - Limits the number of documents to e.g.
2
and passes them on - Finishes by returning the count and a selected few fields from the documents
Here is a breakdown of the query:
db.Order.collection.aggregate([
This finds all matching documents:
{ '$match' : { "company_id" : ObjectId("54c0...") } },
This sorts the documents:
{ '$sort' : { 'order_number' : -1 } },
This counts the documents and passes the unmodified documents, but I'm sure doing it wrong, because things turn weird from here:
{
'$group' : {
'_id' : null,
'count' : { '$sum' : 1 },
'entries' : { '$push' : "$$ROOT" }
}
},
This seems to skip some documents:
{ "$skip" : 100 },
This is supposed to limit the documents, but it does not:
{ "$limit" : 2 },
This does return the count, but it does not return the documents in an array, instead it returns arrays with each field:
{ '$project' : {
'count' : 1,
'entries' : {'_id' : "$entries._id", 'order_number' : "$entries.order_number"}
}
}
])
This is the result:
[
{ "_id" : null,
"count" : 300,
"entries" : [
{
"_id" : [ObjectId('5a5c...'), ObjectId('5a5c...')],
"order_number" : ["4346", "4345"]
},
{
"_id" : [ObjectId('5a5c...'), ObjectId('5a5c...')],
"order_number" : ["4346", "4345"]
},
...
]
}
]
Where do I get it wrong?
To calculate totals and return a subset, you need to apply grouping and skip/limit to the same dataset. For that you can utilise facets
For example to show 3rd page, 10 documents per page:
db.Order.aggregate([
{ '$match' : { "company_id" : ObjectId("54c0...") } },
{ '$sort' : { 'order_number' : -1 } },
{ '$facet' : {
metadata: [ { $count: "total" }, { $addFields: { page: NumberInt(3) } } ],
data: [ { $skip: 20 }, { $limit: 10 } ] // add projection here wish you re-shape the docs
} }
] )
It will return a single document with 2 fields:
{
"metadata" : [
{
"total" : 300,
"page" : 3
}
],
"data" : [
{
... original document ...
},
{
... another document ...
},
{
... etc up to 10 docs ...
}
]
}
这篇关于如何使用 MongoDB 聚合进行分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!