Reputation: 53
I have a problem on debugging this error can you help me?
This is my code
router.post('/accounts/show_accounts/:id', function(req,res){
Account.findOne(
{_id:req.params.id},
{$push: {team: {team_name: req.body.team_name}}},
{safe: true, upsert: true},
function(err, model) {
console.log(err);
}
)
});
And I am getting the below error
errmsg: 'Unsupported projection option: $push: { team: { team_name: “hahaha” } }', code: 2, codeName: 'BadValue' }
Upvotes: 3
Views: 4952
Reputation: 103355
As the error states, findOne()
method considers the document { "$push": { "team": { "team_name": req.body.team_name } } }
as a projection and in a projection field names do not start with $
. I believe you want to do an update operation, not a query. In that case, you need to use the findOneAndUpdate()
or findByIdAndUpdate()
method because the $push
operator is only used in an update operation, not a query like findOne()
:
router.post('/accounts/show_accounts/:id', function(req, res){
Account.findByIdAndUpdate(
req.params.id,
{ "$push": { "team": { "team_name": req.body.team_name } } },
{ "upsert": true, "new": true },
function(err, model) {
if (err) throw err;
console.log(model);
}
)
});
NB: findByIdAndUpdate(id, ...)
is equivalent to findOneAndUpdate({ _id: id }, ...)
Upvotes: 8