将文件推入mongoose模型arrays中的子文件

在我的公司架构中,我有一个张贴的作业是types数组,并将保存子文档

companySchema.js

PostedJobs : [{ JobName : { type: String, required : true}, JobType : { type: String, required : true}, JobLocation : { type: String, required : true}, JobSalay: { type: String, required : true} }], 

在我/公司路线中,我通过模型中的Creator实体获得由特定用户注册的所有公司

得到我使用的用户公司

  router.get('/', isLoggedIn , function(req, res, next) { Company.find({'Creator': req.user.id}).then(function(companies) { res.render('Company', { "Companies" : companies }); }); }); 

获得公司后,我想访问一个特定的公司页面上点击公司名称(唯一)

 router.get('/:name' , isLoggedIn , function(req , res , next) { var name = req.params.name; Company.findOne({Name : name}).then(function(Company) { res.render('dashboard',{ "Company" : Company, errors : [] }); }) }); 

现在我想从POST路由发布到这个特定的公司的工作,因为我的req.body包括我分配给特定variables的JobName,JobType,JobLocation和JobSalary现在该如何将这个文档推到数组

邮政路线

 router.post('/:name' , isLoggedIn , function(req , res , next) { var JobName = req.body.JobName; var JobType = req.body.JobType; var JobLocation = req.body.JobLocation; var Salary = req.body.Salary; //push this job to that specific comapny }); 

我不知道你公司的模式,但是如果你想添加张贴到公司,你应该在其中定义一个数组字段。

 router.post('/:name' , isLoggedIn , function(req , res , next) { var JobName = req.body.JobName; var JobType = req.body.JobType; var JobLocation = req.body.JobLocation; var Salary = req.body.Salary; //push this job to that specific comapny // create the postedJob object var postedJob = {JobName : JobName, JobType : JobType, JobLocation : JobLocation, JobSalay:Salary}; // find the company in DB and add the postedJob to its array of postedJobs var name = req.params.name; Company.findOne({Name : name}).then(function(Company) { company.postedJobs.push(postedJob); company.save(); }); });