2

This is a Controller in which I'm trying to catch multiple candidates id(ObjectId) and try to store it in the database in the array Candidates. But data is not getting pushed in Candidates column of Array type.

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}}}
});

Console screens output

[ '5eb257119f2b2f0b4883558b', '5eb2ae1cff3ae7106019ad7e' ] //candidates

1 Answer 1

1

you have to do all the update operations ($set, $push, $pull, ...) in one object, and this object should be the second argument passed to the update method after the filter object

{$push:{Appliedby : req.user.username}},{$push:{Candidates:{$each: candidates}}

this will update the Appliedby array only, as the third object in update is reserved for the options (like upsert, new, ....)

you have to do something like that

{ $push: { Appliedby: req.user.username, Candidates: { $each: candidates } } }

then the whole query should be something like that

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
    )
});

this could do the trick I guess, hope it helps

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot, it helped me. Sorry for the late reply. :)
great, could you accept the answer, so anyone can find it helpful?
I have a reputation of less than 15 so it is recorded but not showing in public.
sorry, I meant accepting my answer, there is no restriction on this I guess, the restriction is on the upvotes, read this stackoverflow.com/help/someone-answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.