想知道如何使一个复杂的模型合适的岗位要求
app.post('/tree',(req,res)=>{
var tree = new Tree({
firstName: req.body.firstName,
middleName: req.body.middleName,
lastName: req.body.lastName,
alias: [],
father: req.body.father,
mother: req.body.mother,
relationship: [
{
relation: req.body.relation,
children: []
}
]
});
})
这是架构
var mongoose = require('mongoose');
var ObjectId = mongoose.Schema.Types.ObjectId;
var schema = new mongoose.Schema({
firstName: {
type: String,
required: true,
minlength:1,
trim:true
},
middleName: {
type: String,
trim: true
},
lastName: {
type: String,
trim: true
},
alias: [String],
father: ObjectId,
mother: ObjectId,
relationship: [
{
relation: ObjectId,
children: [ObjectId]
}
]
});
var Tree = mongoose.model('Tree',schema);
module.exports = {Tree};
即时通讯新编程所以我很抱歉,如果它是一个愚蠢的问题。希望我provied细节足以得到正确的答案。
为了节省您的tree
可以使用save()
功能,这样tree.save()
。变更后,它看起来像这样
app.post('/tree',(req,res)=>{
var tree = new Tree({
firstName: req.body.firstName,
middleName: req.body.middleName,
lastName: req.body.lastName,
alias: [],
father: req.body.father,
mother: req.body.mother,
relationship: [
{
relation: req.body.relation,
children: []
}
]
});
tree.save().then(err => {
if (err) return res.send(500); // Error while saving
res.send(201); // Return status code Created => Success
});
})
注意:您发送POST请求到您的HTTP(S)服务器,而不是MongoDB的