1

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);
    }

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.