简体   繁体   English

使用 Mongoose-paginate 插件填充多个 Mongoose Model 字段

[英]Populate Multiple Mongoose Model Fields using Mongoose-paginate plugin

I have a Mongoose Schema (Appointment) with two ref fields (sender and receiver).我有一个 Mongoose 架构(约会),其中包含两个 ref 字段(发送方和接收方)。 Both referencing the same Schema (User).两者都引用相同的架构(用户)。 I am trying to populate these two fields using mongoose-paginate plugin.我正在尝试使用 mongoose-paginate 插件填充这两个字段。

The Appointment Schema snippet.约会架构片段。

 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;

The User Schema snippet.用户架构片段。

 var UserSchema = new Schema({ username: String, name: { first: String, middle: String, last: String }, .................. var User = mongoose.model('User', UserSchema); module.exports = User;

The mongoose paginate query. mongoose 分页查询。

 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}});

This works fine but i want to populate both the sender and receiver fields.这工作正常,但我想填充发件人和收件人字段。 Please how do i go about that.请问我 go 怎么办。 I tried passing the fields in JSON but didn't work.我尝试传递 JSON 中的字段,但没有成功。

I simply passed in an array of fields to populate and it worked like a charm. 我只是简单地传入了一系列字段以进行填充 ,所以它就像一种魅力。

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}});

For developers using mongoose-paginate-v2, you can pass an array object to in the pagination options to populate multiple references.对于使用 mongoose-paginate-v2 的开发人员,您可以将数组 object 传递给分页选项以填充多个引用。

Collection.paginate(query,{
   page : 1,
   limit : 10,
   populate : [
     {
       path : 'refName1',
       select : {field : 1}
     },
     {
       path : 'refName2',
       select : {field : 1}
     }
   ]
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM