Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

295
Visualizações
errmsg: 'Unsupported projection option: $push: { ... }', code: 2, codeName: 'BadValue' }

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' }

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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 }, ...)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda