Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!