0

I have 2 documents in a collection with a structure like below. I want to update "Source" as Home where environment is "QA" and Name is "Alan".

{
  environment: DevA,
  data: [
   { 
     Name: "John",
     Source: "Home"
   },
   { 
     Name: "Alan",
     Source: "Office"
   },
   { 
     Name: "Susan",
     Source: "Office"
   }
  ],
},
{
  environment: DevB,
  data: [
   { 
     Name: "John",
     Source: "Home"
   },
   { 
     Name: "Alan",
     Source: "Office"
   },
   { 
     Name: "Susan",
     Source: "Office"
   }
  ],
}

I tried the following code below. But it did not update it, neither gave an error.

 collection1.update_one({ 'environment':'QA', 'data.Name':'Alan'},
                          { $'set': {
                                     "data":"Source":"NewValue"
                                    }
                          })
5
  • 1
    Possibly because you have spelled environment as environemnt? Although I suspect that would raise an error. Commented Sep 28, 2022 at 13:54
  • umm, no. That's a typo here in question only. I'll correct. May be I have given multiple filter condn.. Could that be issue? Commented Sep 28, 2022 at 16:07
  • 1
    When you say that you have a document with a structure shown in the question, does that represent two different documents? In either case, those documents do not match the condition of environment: 'QA' so it would make sense that no update was performed if that was the data the operation was tested against. To your overall question - you'll probably want to look into arrayFilters since you want to manipulate the data array. See here Commented Sep 28, 2022 at 17:10
  • @user20042973 I am able to do it for one nested array [answer posted below] in the document say for name = "Alan" using below answer. However, I also want to update for more than one nested arrays elements like for where name="Alan" *& name="John", update source as "Office". Commented Sep 29, 2022 at 8:34
  • Great, looks like we're making progress! Two thoughts regarding your latest comment and the answer, the first is that you don't need $elemMatch here if you are only searching a single field (Name) in the data array. You'd only need it if you added a second field in there. The second thing is to search for multiple names you probably want $in. So the second predicate should probably change to 'data.Name': { $in: [ "Alan", "John" ] } Commented Sep 29, 2022 at 12:45

1 Answer 1

1

I was able to update it using following code:

collection1.update_one( 
{
  "environment":"DevB",
  "data" {"$elemMatch": { "Name": "Alan"} }
},
{
  "$set" : {
          "data.$.source":"ABC"
           }   
 })
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.