[英]Defining a property on mongo schema referencing another schema plus extra fields
[英]Referencing another schema in Mongoose WITH extra fields
var UserSchema = new Schema({
job : [{
type : mongoose.Schema.Types.ObjectId,
experience : String,
grade : String,
ref: 'Company'}]
});
用户可以有多个作业。 当我向用户添加作业时,我会这样:
如果我有 :
req.body.job == {type : company._id, experience : "bla bla", grade : "smth"}
和
function addJob(req, res, next) {
var userId = req.user._id;
var job = req.body.job;
return User.findById(userId).exec()
.then(user => {
user.job.push(job);
return user.save()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
});
}
我有一个错误:
Unhandled rejection CastError: Cast to ObjectId failed for value "[object Object]" at path "job"
但是,如果我这样做:
req.body.job == {type : company._id, experience : "bla bla", grade : "smth"}
同
function addJob(req, res, next) {
var userId = req.user._id;
var job = req.body.job;
return User.findById(userId).exec()
.then(user => {
user.job.push(job.type); // <----------
return user.save()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
});
}
它可以工作,但是experience
和grade
是不确定的。 如何指定这些字段?
编辑:
所以我像这样更改了UserSchema
:
var UserSchema = new Schema({
job : [{
_company : {
type : mongoose.Schema.Types.ObjectId,
ref: 'Company'
},
experience : String,
grade : String
}]
});
我试过了:
//req.body.job == {_company:{type : company._id}, experience : "bla bla", grade : "smth"}
function addJob(req, res, next) {
var userId = req.user._id;
var job = req.body.job;
return User.findById(userId).exec()
.then(user => {
user.job.push(job);
return user.save()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
});
}
我从服务器收到500(内部服务器错误)错误
但
//req.body.job == {_company:{type : company._id}, experience : "bla bla", grade : "smth"}
function addJob(req, res, next) {
var userId = req.user._id;
var job = req.body.job;
return User.findById(userId).exec()
.then(user => {
user.job.push(job._company); //<-----------------
return user.save()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
});
}
可以正常工作。 但是,在mongodb中,关于grade
和experience
领域仍然一无所有...
猫鼬保留了“类型”字段,然后
{
type : mongoose.Schema.Types.ObjectId,
experience : String,
grade : String,
ref: 'Company'
}
正在描述一种类型为ObjectId的实体,该实体引用公司(此实体与.populate一起使用)。 不幸的是,由于它作为对象ID保存在MongoDB中,因此其他字段将被忽略。
然后,您可以选择在其中一个字段中引用公司,并可以使用JobSchema检索额外的信息:
{
_company : {type: mongoose.Schema.Types.ObjectId, ref: 'Company'}
experience : String,
grade : String
}
您将能够填充工作的_company字段,而不会丢失“经验”和“等级”信息
在您的版本之后:
使用给定的JobSchema:
{
_company : {type: mongoose.Schema.Types.ObjectId, ref: 'Company'}
experience : String,
grade : String
}
您的工作实例应类似于{_company:ObjectId(“ .......”),经验:“ blabla”,等级:“ smth”}。 然后,您的req.body.job应该是{_company:company._id,经验:“ bla bla”,等级:“ smth”}
在您的第一个addJob中,您遇到500错误,因为您要求MongoDB将看起来像{type:ObjectId(“ ....”)}的文档转换为ObjectId。
第二个addJob不会触发错误,因为您正在上载一个看起来像{type:ObjectId(“ ...”)}的文档(并且会丢失成绩和经验字段),并且不需要任何Schema字段因此猫鼬会忽略您的字段类型,并将一个空对象上传到您的集合中。
tl; dr:应该是:
// req.body.job == {_company: company._id, experience : "bla bla", grade : "smth"}
function addJob(req, res, next) {
var userId = req.user._id,
job = req.body.job;
return User.findById(userId)
.exec()
.then(
user => {
user.job.push(job);
return user.save()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
}
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.