我有一个用户架构,每个用户都有多条消息,我试图使用mongoose-paginate-2对消息进行分页。我不知道如何对消息进行分页。
聊天模式:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const ChatGroupSchema = new Schema({
name: {
type: String,
required: true
},
users: [
{
type: Schema.Types.ObjectId,
ref: "user"
}
],
messages: [
{
type: Schema.Types.ObjectId,
ref: "message"
}
]
});
const ChatGroupModel = mongoose.model("chatGroup", ChatGroupSchema);
消息架构:
const mongoose = require("mongoose");
var mongoosePaginate = require("mongoose-paginate-v2");
const Schema = mongoose.Schema;
const MessageSchema = new Schema({
to: {
type: Schema.Types.ObjectId,
ref: "chatGroup"
},
from: {
type: Schema.Types.ObjectId,
ref: "user"
},
message: {
type: String
},
createdAt: {
type: Date,
default: Date.now
}
});
MessageSchema.plugin(mongoosePaginate);
const MessageModel = mongoose.model("message", MessageSchema);
然后这是我的查询
ChatGroupModel.findOne({ _id: _id })
.populate({
path: "message",
options: {
offset: 1,
limit: 10,
sort: { createdAt: -1 }
},
populate: [
{
path: "to",
model: "chatGroup"
},
{
path: "from",
model: "user"
}
]
});
我目前可以按每个chatGroup获取消息,但是我希望对这些消息进行分页。请帮忙:D