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

161
Vistas
Can't update values in mongodb database

Username doesn't get updated after running this. I also tried UpdateOne and it didn't work as well. The code was supposed to update an array but I tried updating the username to make it easier to track down the problem.

const mongoose = require('mongoose');

const schema = mongoose.Schema;

const userSchema = new schema({
    Username:{
        type: String,
        required: true
    },
    Password:{
        type: String,
        required: true
    },
    Cart: Array
});

const User = mongoose.model('User', userSchema);
module.exports = User;

.

app.post('/putbox', (req, res) => {


    var query = { Username: user };

    var newvalues = { $push: { Cart: "Boxing" } };

    try{

        User.find(query).then((d)=>{

            var values = d[0].Cart;
            values.push("boxing")

            User.findByIdAndUpdate(d._id, {Username: "test1"})

        });

    } catch(err) {
        console.log(err);
    } 

    res.redirect('/boxing');

});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I believe the syntax is not correct. The first element of updateOne searches for matches to update. You are just passing d._id which is not the same as the _id key in your db structure. Try this one

User.updateOne({ _id: d._id }, {Username: "test1"})
about 4 years ago · Juan Pablo Isaza 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