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

1.7K
Views
How to update nested objects in MongoDb with Mongoose

I am trying to update one key value in a nested object in MongoDB. currently, the way I am doing it the entire nested object is being overwritten, how can I change one key-value pair?

  try {
      Files.updateOne(
        { _id: document._id },
        {a:{b:{c:"new text", d: "not to change"}}},
        function (err, result) {
          if (err) {
            res.send(err);
          } else {
            res.send(result);
          }
        }
      );
    } catch (err) {
      res.status(400).send(err);
    }

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try this:

try {
  Files.updateOne(
    { _id: document._id },
    { "a.b.c": "new text" },
    function (err, result) {
      if (err) {
        res.send(err);
      } else {
        res.send(result);
      }
    }
  );
} catch (err) {
  res.status(400).send(err);
}
over 4 years ago · Santiago Trujillo 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!