【发布时间】:2021-08-11 09:45:19
【问题描述】:
我有以下 postSchema 并希望根据 updatedAt 字段获取数据。当人们发表评论时,我将 numberofreply 增加一并且它的 updatedAt 被更新。我应该如何获取无限滚动的数据,我应该为此操作使用索引吗?
const postScheme = mongoose.Schema(
{
post: {
type: String,
trim: true,
},
numberOfReply: {
type: Number,
default: 0
},
owner: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
hasImage: {
type: Boolean,
},
image: {
type: String,
trim: true
},
},
{timestamps: true}
)
这是我用来获取第一页的内容
Post.Post.find({}).sort({'updatedAt': -1}).limit(10).populate('owner').populate('coin').exec(function (err, posts) {
res.send(posts)
})
这是无限滚动的
Post.Post.find({isCoin: true, updatedAt: {$lt: req.body.last}}).sort({'updatedAt': -1}).populate('owner').limit(
10).exec(function (err, posts) {
res.send(posts)
})
【问题讨论】:
标签: node.js mongodb mongoose indexing pagination