这是一个Controller,我试图捕捉多个候选人的id(ObjectId),并试图将其存储在数据库的Candidates数组中。但是数据没有被推送到数组类型的候选者列中。
routes.post('/Job/:id',checkAuthenticated,function(req,res){
var candidates=req.body.candidate;
console.log(candidates);
Job.update({_id:req.params.id},{$push:{Appliedby : req.user.username}},{$push:{Candidates:{$each:
candidates}}}
});
控制台屏幕输出
[ '5eb257119f2b2f0b4883558b', '5eb2ae1cff3ae7106019ad7e' ] //candidates
你必须做所有的 update
业务 ($set, $push, $pull, ...)
中的一个对象,而这个对象应该是传递给 update
方法后,过滤器对象
{$push:{Appliedby : req.user.username}},{$push:{Candidates:{$each: candidates}}
这将更新 Appliedby
数组,因为update中的第三个对象是为选项保留的(比如upset、new、......)。
你必须做这样的事情
{ $push: { Appliedby: req.user.username, Candidates: { $each: candidates } } }
那么整个查询应该是这样的
routes.post('/Job/:id', checkAuthenticated, function (req, res) {
var candidates = req.body.candidate;
console.log(candidates);
Job.update(
{ _id: req.params.id }, // filter part
{ $push: { Appliedby: req.user.username, Candidates: { $each: candidates } } } // update part in one object
)
});
这可以做的伎俩,我猜,希望它可以帮助。