1

I have the following JSON structure:

 JSON =  { "change_number": "CRQ91320s23",
         "change_description": "Nexusg work",
         "date": "2/10/2020",
         "changeowner" : "Jeffre Hocking",
         "implementer" : "john Elazi",
         "PVT_testers": {
           "Epic": {
             "contact": "nathanbdas",
             "email": "[email protected]" }}

I am using mongoDB + NodeJS to interface with it..

I want to add another entry beside "Epic" under "PVT_testers"..

This is my code, and when I run it, it replaces Epic with EPIC2 rather than adding to the same level.

Have cleaned the code to make it easier.

MongoClient.connect(url, function(err, db) {
  var dbo = db.db("radar");
  var myquery = {  "change_number" : req.params.id };
  var newvalues = { $set: {"PVT_testers"  : {"EPIC2" : {"das2": "asd", "das2": "asd", "results": { "pre_outcome":"sucess" }}}     }}
   dbo.collection("changes").updateOne(myquery, newvalues, function(err, res) {
      if (err) return res.json("Error:Database error" ); 
    console.log(res)
    db.close();
});

1 Answer 1

2

When using $set operator, you are updating the whole object (sub-document in mongodb linguo). To add a new field, use the dot notation:

var newvalues = {
  $set: {
    "PVT_testers.EPIC2": {
      "das2": "asd", 
      "das2": "asd", 
      "results": { 
        "pre_outcome": "sucess" 
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that helps a bit.. But the new addition is what looks like a list?
you mean like an array ?

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.