Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

293
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda