我有2个模型,我正在尝试使用填充函数来获取整理的响应。
我的模型如下
const mongoose = require('mongoose');
const RegisterEmpSchema = new mongoose.Schema({
empID: Number,
empName: String,
empPhone: String,
empProj: String
}, {
timestamps: true
});
const RegisterEntriesSchema = new mongoose.Schema({
registerType: String,
registerItemsQuantity: Number,
registerItemsDesc: String,
empID: {
type: mongoose.Schema.Types.Number,
ref: 'registerEmpModel'
}
}, {
timestamps: true
});
RegisterEntriesSchema.index({ createdAt: 1 }, { expires: '525601m' });
const registerEmpModel = mongoose.model('RegisterEmpSchema', RegisterEmpSchema, 'registerEmployeeCollection');
const registerEntriesModel = mongoose.model('RegisterEntriesSchema', RegisterEntriesSchema, 'registerEntriesCollection');
module.exports = {
registerEmpModel, registerEntriesModel,
}
当我尝试从registerEntries获取所有条目时,它仅返回registerEntries,并且不使用registerEmp值填充响应
// Retrieve and return all registers from the database.
exports.findAllRegisterEntries = (req, res) => {
registerEntriesModel.
find().
populate('RegisterEmpSchema').
exec(function (err, data) {
if (err) return console.log(err);
res.send(data);
});
};
我保存模型数据https://pastebin.com/fj84azkM的控制器代码
答案 0 :(得分:1)
此处ref
将是集合名称:
const RegisterEntriesSchema = new mongoose.Schema({
registerType: String,
registerItemsQuantity: Number,
registerItemsDesc: String,
empID: {
type: mongoose.Schema.Types.ObjectId,
ref: 'registerEmpModel' // Collection name
}
}, {
timestamps: true
});
您必须填充字段而不是模型:
registerEntriesModel.
find().
populate('empID').
exec(function (err, data) {
if (err) return console.log(err);
res.send(data);
});
答案 1 :(得分:1)
我认为您不能在这种情况下使用填充,因为您尝试填充empID字段,但是猫鼬不知道empID是员工模型中的empID,默认情况下它将尝试匹配_id上的empID员工模型的字段。
解决此问题的一种方法是在员工架构中使用_id字段而不是empID字段:
const RegisterEmpSchema = new mongoose.Schema(
{
_id: mongoose.Schema.Types.Number,
empName: String,
empPhone: String,
empProj: String
},
{
timestamps: true
}
);
并使用此示例正文创建员工:
{
"_id": 1,
"empName": "emp 1 name",
"empPhone":"emp 1 phone",
"empProj": "emp 1 proj"
}
现在您可以像这样填充:
exports.findAllRegisterEntries = (req, res) => {
registerEntriesModel
.find()
.populate("empID") // note that here we use the fieldName
.exec(function(err, data) {
if (err) return console.log(err);
res.send(data);
});
};
将会得到如下结果:
[
{
"_id": "5dea4f795668341d0cb51a46",
"registerType": "register type 1",
"registerItemsQuantity": 1,
"registerItemsDesc": "register desc",
"empID": {
"_id": 1,
"empName": "emp 1 name",
"empPhone": "emp 1 phone",
"empProj": "emp 1 proj",
"createdAt": "2019-12-06T12:54:06.446Z",
"updatedAt": "2019-12-06T12:54:06.446Z",
"__v": 0
},
"createdAt": "2019-12-06T12:54:17.986Z",
"updatedAt": "2019-12-06T12:54:17.986Z",
"__v": 0
},
{
"_id": "5dea4f7b5668341d0cb51a47",
"registerType": "register type 2",
"registerItemsQuantity": 2,
"registerItemsDesc": "register desc",
"empID": {
"_id": 2,
"empName": "emp 2 name",
"empPhone": "emp 2 phone",
"empProj": "emp 2 proj",
"createdAt": "2019-12-06T12:54:14.762Z",
"updatedAt": "2019-12-06T12:54:14.762Z",
"__v": 0
},
"createdAt": "2019-12-06T12:54:19.930Z",
"updatedAt": "2019-12-06T12:54:19.930Z",
"__v": 0
}
]
作为第二种选择,如果您不想在架构中进行任何更改,则可以使用以下聚合:
router.get("/entry", async (req, res) => {
const result = await registerEntriesModel.aggregate([
{
$lookup: {
from: "registerEmployeeCollection",
localField: "empID",
foreignField: "empID",
as: "employee"
}
},
{
$addFields: { employee: { $arrayElemAt: ["$employee", 0] } }
}
]);
res.send(result);
});
哪个会给出这样的输出:
[
{
"_id": "5dea49170817b24bb8f37665",
"registerType": "register type 1",
"registerItemsQuantity": 1,
"registerItemsDesc": "register desc",
"empID": 1,
"createdAt": "2019-12-06T12:27:03.841Z",
"updatedAt": "2019-12-06T12:27:03.841Z",
"__v": 0,
"employee": {
"_id": "5dea49020817b24bb8f37663",
"empID": 1,
"empName": "emp 1 name",
"empPhone": "emp 1 phone",
"empProj": "emp 1 proj",
"createdAt": "2019-12-06T12:26:42.213Z",
"updatedAt": "2019-12-06T12:26:42.213Z",
"__v": 0
}
},
{
"_id": "5dea491e0817b24bb8f37666",
"registerType": "register type 2",
"registerItemsQuantity": 2,
"registerItemsDesc": "register desc",
"empID": 2,
"createdAt": "2019-12-06T12:27:10.696Z",
"updatedAt": "2019-12-06T12:27:10.696Z",
"__v": 0,
"employee": {
"_id": "5dea490b0817b24bb8f37664",
"empID": 2,
"empName": "emp 2 name",
"empPhone": "emp 2 phone",
"empProj": "emp 2 proj",
"createdAt": "2019-12-06T12:26:51.130Z",
"updatedAt": "2019-12-06T12:26:51.130Z",
"__v": 0
}
}
]