node.js - 猫鼬使用跳过和限制填充数组
问题描述
型号是
const mongoose = require('mongoose')
const Schema = mongoose.Schema
var UserActivitySchema = new Schema({
author: {type: mongoose.Schema.Types.ObjectId, required: true, unique: true, ref: 'user'},
readHistory: [{articleid: {type: mongoose.Schema.Types.ObjectId, ref: 'article'}, date_read: {type: Date, default: Date.now()}}],
watchHistory: [{videoid: {type: mongoose.Schema.Types.ObjectId, ref: 'video'}, date_watch: {type: Date, default: Date.now()}}],
searchHistory: [{query: String, date:{type: Date, default: new Date()}}]
})
module.exports = mongoose.model('useractivity',UserActivitySchema, 'useractivities')
在这里,我想获取我使用过查询的 readHistory
if (req.query.page) var page = parseInt(req.query.page);
else var page = 1;
UserActivity.findOne({author: req.client.userId}).select('readHistory')
.populate({path: 'readHistory.articleid', select: 'title description views thumbnailURI', options: { skip: (page-1)*10, limit: 10}, populate: {path: 'author', select: 'displayName -_id'}})
.exec().then(activity=>{
if (!activity || activity.readHistory.length <= 0)
res.json({success: false, message: 'You have not read any article yet.'})
else {
res.json({success: true, readHistory: activity.readHistory})
}
}).catch(err=>{
res.json({success: false, message: err.message})
})
但它返回数组中的所有文档。如何解决这个问题?
解决方案
推荐阅读
- javascript - 将字符串拆分为每行 36 个字符的数组,不分词
- vuejs2 - 在 XAMPP 上使用 Vue CLI 3
- javascript - 未捕获的 TypeError:URL 不是 javascript 中的构造函数
- c++ - 条件变量的“等待”函数在提供谓词时导致意外行为
- botframework - Bot Framework 网络聊天+语音+特定语音
- wordpress - 如何修复古腾堡 InnerBlocks 以响应 RangeControl
- docker - docker run 失败并显示 /bin/sh:0 -c 需要一个参数
- bixby - 如何在 Bixby 编程中访问 Content Provider
- python - 将 Web 模板与 Flask 集成
- angular - 数据对象中的角度拆分逗号分隔值