如何在mongoDB的3个集合中填充

时间:2018-08-07 10:40:49

标签: node.js mongodb mongoose

我有三个集合,例如UserProgram和`Agenda。这些模型如下。

用户模型

const mongoose = require('mongoose');

const UserSchema = mongoose.Schema({
    name: {type:String},
    email: {type:String}
},{timestamps:true
}
);

module.exports = mongoose.model('User', UserSchema);

程序模型

const mongoose = require('mongoose');

const NoteSchema = mongoose.Schema({
    name: {type:String},
    timefrom: {type:Date},
    timeto: {type:Date},
    status: {type:String},
    venue: {type:String},
    timetype: {type:Number},
    userid:{type:mongoose.Schema.Types.ObjectId,ref : 'User', required: true},
    logo :{type:String,default: 'programe'}
},{timestamps:true
});

module.exports = mongoose.model('Program', NoteSchema);

议程模型

const mongoose = require('mongoose');

const AgendaSchema = mongoose.Schema({
    name: {type:String},
    timefrom: {type:Date},
    timeto: {type:Date},
    status: {type:String},
    proorder: {type:String},
    proid:{type:mongoose.Schema.Types.ObjectId,ref : 'Program', required: true}
},
{timestamps:true}
);

module.exports = mongoose.model('Agenda', AgendaSchema);

现在我只获得议程和计划数据。

议程控制器

// Retrieve and return all agenda from the database.
exports.findAll = (req, res) => {

    Agenda.find()
    .populate('proid')
    //.populate('userid')

    .then(agendas => {
        res.send(agendas);
    }).catch(err => {
        res.status(500).send({
            message: err.message || "Some error occurred while retrieving agenda."
        });
    });
};

当我转到该URL和GET方法时,我想填充agenda文档(已完成),相关的program文档(已完成)和相关的user文档(此我想要)?

这样的通缉查询

SELECT * 
FROM users, programs, agendas
WHERE agendas.proid = programs.id AND programs.userid = users.id

2 个答案:

答案 0 :(得分:2)

您可以使用$lookup聚合

Agenda.aggregate([
  { "$lookup": {
    "from": Program.collection.name,
    "let": { "proid": "$proid" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$_id", "$$proid" ] } } },
      { "$lookup": {
        "from": User.collection.name,
        "let": { "userid": "$userid" },
        "pipeline": [
          { "$match": { "$expr": { "$eq": [ "$_id", "$$userid" ] } } },
        ],
        "as": "userid"
      }},
      { "$unwind": "$userid" }
    ],
    "as": "proid"
  }},
  { "$unwind": "$proid" }
])

或通过填充

Agenda.find()
  .populate([{ path: 'proid', populate: { path: 'userid' }}])

两者都会给您相同的结果

答案 1 :(得分:1)

您可以使用.populate(){ populate : { path : 'field' } }。 示例:

Agenda.find().                    //Collection 1
        populate({path:'proid',   //Collection 2
        populate:{path:'userid'   //Collection 3
        }})
    .exec();

在收集完4个之后,您需要确定模型(如果可以的话) 示例:

    Agenda.find().                                    //Collection 1
        populate({path:'proid',                       //Collection 2
        populate:{path:'userid',                      //Collection 3
        populate:{path:'otherFiel', model: Collection //Collection 4 and more
        }}})
    .exec();

最后,如果只想获取某些Collection的某些字段,则可以使用属性select 示例:

Agenda.find().                                    
        populate({path:'proid', select:'name status venue'                  
        populate:{path:'userid'               
        }})
    .exec();