使用 Mongoose-paginate 插件填充多个 Mongoose 模型字段
Populate Multiple Mongoose Model Fields using Mongoose-paginate plugin
我有一个带有两个引用字段(发送者和接收者)的 Mongoose 模式(约会)。两者都引用相同的架构(用户)。我正在尝试使用 mongoose-paginate 插件填充这两个字段。
约会架构片段。
var paginate = require('mongoose-paginate');
var AppointmentSchema = new Schema({
dateCreated: {type: Date, default: Date.now},
sender: {type: ObjectId, ref: 'User'},
receiver: {type: ObjectId, ref: 'User'},
message: String,
.........................
AppointmentSchema.plugin(paginate);
var Appointment = mongoose.model('Appointment', AppointmentSchema);
module.exports = Appointment;
用户架构片段。
var UserSchema = new Schema({
username: String,
name: {
first: String,
middle: String,
last: String
},
..................
var User = mongoose.model('User', UserSchema);
module.exports = User;
猫鼬分页查询。
Appointment.paginate({}, request.query.page, request.query.limit,
function(error, pageCount, appointments, itemCount) {
if (error) {
return console.log('ERRRRRRRRRR'.red);
} else {
console.log(appointments);
return response.render('message/inbox',
{
pageTitle: "Trash",
messages: appointments,
pageCount: pageCount,
itemCount: itemCount,
currentPage: currentPage
});
}
}, {columns: {}, populate: 'receiver', populate: 'sender', sortBy: {title: -1}});
这工作正常,但我想填充发件人和收件人字段。请问我该怎么做。我尝试传递 JSON 中的字段,但没有成功。
我只是将字段数组传递给 populate,它的效果非常好。
Appointment.paginate({}, request.query.page, request.query.limit,
function(error, pageCount, appointments, itemCount) {
if (error) {
return console.log('ERRRRRRRRRR'.red);
} else {
console.log(appointments);
return response.render('message/inbox',
{
pageTitle: "Trash",
messages: appointments,
pageCount: pageCount,
itemCount: itemCount,
currentPage: currentPage
});
}
}, {columns: {}, populate: ['receiver', 'sender'], sortBy: {title: -1}});
对于使用 mongoose-paginate-v2 的开发者,您可以在分页选项中传递一个数组对象来填充多个引用。
Collection.paginate(query,{
page : 1,
limit : 10,
populate : [
{
path : 'refName1',
select : {field : 1}
},
{
path : 'refName2',
select : {field : 1}
}
]
})
我有一个带有两个引用字段(发送者和接收者)的 Mongoose 模式(约会)。两者都引用相同的架构(用户)。我正在尝试使用 mongoose-paginate 插件填充这两个字段。
约会架构片段。
var paginate = require('mongoose-paginate');
var AppointmentSchema = new Schema({
dateCreated: {type: Date, default: Date.now},
sender: {type: ObjectId, ref: 'User'},
receiver: {type: ObjectId, ref: 'User'},
message: String,
.........................
AppointmentSchema.plugin(paginate);
var Appointment = mongoose.model('Appointment', AppointmentSchema);
module.exports = Appointment;
用户架构片段。
var UserSchema = new Schema({
username: String,
name: {
first: String,
middle: String,
last: String
},
..................
var User = mongoose.model('User', UserSchema);
module.exports = User;
猫鼬分页查询。
Appointment.paginate({}, request.query.page, request.query.limit,
function(error, pageCount, appointments, itemCount) {
if (error) {
return console.log('ERRRRRRRRRR'.red);
} else {
console.log(appointments);
return response.render('message/inbox',
{
pageTitle: "Trash",
messages: appointments,
pageCount: pageCount,
itemCount: itemCount,
currentPage: currentPage
});
}
}, {columns: {}, populate: 'receiver', populate: 'sender', sortBy: {title: -1}});
这工作正常,但我想填充发件人和收件人字段。请问我该怎么做。我尝试传递 JSON 中的字段,但没有成功。
我只是将字段数组传递给 populate,它的效果非常好。
Appointment.paginate({}, request.query.page, request.query.limit,
function(error, pageCount, appointments, itemCount) {
if (error) {
return console.log('ERRRRRRRRRR'.red);
} else {
console.log(appointments);
return response.render('message/inbox',
{
pageTitle: "Trash",
messages: appointments,
pageCount: pageCount,
itemCount: itemCount,
currentPage: currentPage
});
}
}, {columns: {}, populate: ['receiver', 'sender'], sortBy: {title: -1}});
对于使用 mongoose-paginate-v2 的开发者,您可以在分页选项中传递一个数组对象来填充多个引用。
Collection.paginate(query,{
page : 1,
limit : 10,
populate : [
{
path : 'refName1',
select : {field : 1}
},
{
path : 'refName2',
select : {field : 1}
}
]
})