如何在不使用 $facet 的情况下在 mongoDB 中实现分页,因为 AZURE 和 AWS 不支持它
How to achieve pagination in mongoDB without using $facet as AZURE and AWS do not support it
我有 collection 类似下面的东西
[
{
_id: id1,
user: user1,
city: "test1"
},
{
_id: id2,
user: user2,
city: "test1"
},
{
_id: id3,
user: user3,
city: "test2"
},
.....
]
我想得到类似的东西,进一步,我想根据 _id 等对结果进行排序
[
results: [
{
_id: id1,
user: user1,
city: "test1"
},
{
_id: id1,
user: user2,
city: "test1"
},
...
],
totalResults: 220,
pageNumber: 2
]
我也在项目中使用 moongoose
db.collection.aggregate([
{
"$match": {
query
}
},
{
$project: {
user: "$user"
}
},
{
"$sort": shortBy
},
{
$group: {
_id: null,
postCount: {
$sum: 1
},
results: {
$push: '$$ROOT'
}
}
},
{
$project: {
total: '$postCount',
results: {
$slice: [
'$results',
skip,
limit
]
}
}
}
])
我有 collection 类似下面的东西
[
{
_id: id1,
user: user1,
city: "test1"
},
{
_id: id2,
user: user2,
city: "test1"
},
{
_id: id3,
user: user3,
city: "test2"
},
.....
]
我想得到类似的东西,进一步,我想根据 _id 等对结果进行排序
[
results: [
{
_id: id1,
user: user1,
city: "test1"
},
{
_id: id1,
user: user2,
city: "test1"
},
...
],
totalResults: 220,
pageNumber: 2
]
我也在项目中使用 moongoose
db.collection.aggregate([
{
"$match": {
query
}
},
{
$project: {
user: "$user"
}
},
{
"$sort": shortBy
},
{
$group: {
_id: null,
postCount: {
$sum: 1
},
results: {
$push: '$$ROOT'
}
}
},
{
$project: {
total: '$postCount',
results: {
$slice: [
'$results',
skip,
limit
]
}
}
}
])