同时填充多个字段

我有一个模式称为聊天:

var ChatSchema = new Schema({ vehicle: { type: Schema.Types.ObjectId, ref: 'Vehicle' }, messages: [String] }); 

…和另一个称为Vehicle的模式:

 var VehicleSchema = new Schema({ make: { type: Schema.Types.ObjectId, ref: 'Make' }, model: { type: Schema.Types.ObjectId, ref: 'Model' } }); 

我需要一个填充车辆的查询,也需要车辆内的“制造”和“模型”字段。 我尝试过,但无法使其工作。 尝试与path也没有成功。

 Chat.find(function (err, chats) { if (err) res.json({ success: false, response: err }); if (chats.length == 0) res.json({ success: false, response: "Nothing found." }); else res.json({ success: true, response: { chats: chats } }); }).populate('vehicle make model'); 

嵌套的人口必须在分开的步骤完成,这使得它有点尴尬,但你可以用这样的方法来做到这一点:

 Chat.find().populate('vehicle').exec(function (err, chats) { Make.populate(chats, 'vehicle.make', function (err, chats) { ModelModel.populate(chats, 'vehicle.model', function (err, chats) { if (err) res.json({ success: false, response: err }); if (chats.length == 0) res.json({ success: false, response: "Nothing found." }); else res.json({ success: true, response: { chats: chats } }); }); }); }); 

文档在这里 。