我的个人资料文档如下保存在我的数据库中:
{
"_id": {
"$oid": "5c3aa1de18ad682fb1444138"
},
"user": {
"$oid": "5c318f56e3224b15e0fdbd06"
},
"company": {
"info": {
"$oid": "5c318f56e3224b15e0fdbd07"
},
"position": "Stemuli"
},
"hometown": "Dallas",
"date": {
"$date": "2019-01-13T02:26:38.185Z"
},
"__v": 0
}
我正在尝试在新保存的配置文件中填充字段company.info
。
new Profile(profileFields).save().then(profile => {
Profile.populate(profile, { path: "company.info" }, (err, p) => {
console.log(err);
console.log(p);
});
});
我将公司字段返回以下内容
company: {id: "5c318f56e3224b15e0fdbd07", position: "Stemuli"}
此ID链接到的文档如下:
{
"_id": {
"$oid": "5c318f56e3224b15e0fdbd07"
},
"employees": [
{
"$oid": "5c318f56e3224b15e0fdbd06"
}
],
"name": "Company",
"__v": 14
}
如何获取它而不是ID来填充实际的公司文档?
配置文件架构
// Create Schema
const ProfileSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: "users"
},
company: {
id: { type: Schema.Types.ObjectId, ref: "industrypartners" },
position: { type: String }
},
work_experience: Array,
hometown: { type: String },
date: {
type: Date,
default: Date.now
}
});
module.exports = Profile = mongoose.model("profile", ProfileSchema);
行业合作伙伴架构
// Create Schema
const IndustryPartnerSchema = new Schema({
name: {
type: String,
required: true
},
schools: [{ type: Schema.Types.ObjectId, ref: "schools" }]
});
module.exports = IndustryPartner = mongoose.model(
"industrypartner",
IndustryPartnerSchema
);
答案 0 :(得分:0)
尝试一下:
new Profile(profileFields).save().then(profile => {
Profile.populate(Profile, {
path: "company.info",
select: {
_id: 1,
employees: 1,
name: 1,
}
}, (err, p) => {
console.log(err);
console.log(p);
});
});