mongodb - 为什么分页插件和跳过/限制在那个 mongodb 查询中不起作用?
问题描述
const paginateScheme = new Schema({
posts: [ { img: String, description: String, liked: String, postComId: String, comments: [ { author: String, text: String } ] } ],
}, {collection: "usersData"});
paginateScheme.plugin(mongoosePaginate);
const myModel = mongoose.model("sample", paginateScheme);
app.put("/api/pagination", function(req, res){
const options = {
page: 1,
limit: 3,
collation: {
locale: 'en',
},
};
const query = User.find({mail: 'user2@example.com'}, {posts: 1 });
myModel.paginate( query , options, function (err, result) {
if(err) return console.log(err);
res.send(result);
});
});
在哪里发布我要分页的对象数组。我检查了这个插件工作正常。当我使用 Model.find({}) 它通过外部对象分页没有任何问题。我尝试使用skip + limit,但它什么也没返回。
this query returns:
{
docs: [ { _id: 601a8f013d86dc237468467c, posts: [Array] } ],
totalDocs: 1,
limit: 3,
totalPages: 1,
page: 1,
pagingCounter: 1,
hasPrevPage: false,
hasNextPage: false,
prevPage: null,
nextPage: null
}
解决方案
你可以使用$slice
这样的数组分页(使用异步/等待)
let result = await myModel.find({mail: 'user2@example.com'}, {posts: {$slice: [0, 3]}})
推荐阅读
- python-3.x - Tensorboard 投影仪可视化 - PCA 不断加载
- javascript - 我有这样的 JSON 字符串化数据:
- android - 如何将图像移动到线性布局的底部
- go - 更好地理解 Kademlia 的 XOR 整数度量
- python - 插入均值后 numpy 数组形状发生变化
- oracle - 在 oracle Apex 中将金额格式设置为 $xxx,xx
- php - 如何在 PHP 中使用八进制、十六进制和 unicode 代码点表示法打印一个字母?
- java - 如何构建没有特定类型的堆排序类?
- c - 无法将 char 字符串插入 char 数组
- ros - Pepper 机器人安装错误( naoqi_driver: No definition of [boost] for OS version [] )