0

I have a small script that finds an element in array, and I want to add another field to this element. this is my script:

db.myCollection.find({)}).forEach((doc)=> {
      doc.someArray.forEach((someArrayObj)=> {
          if (someArrayObj.someField != undefined) {
            var arrayElement = doc.someOtherArray.find(ob => ob.id == someArrayObj.id)
            // i want to add a new field to this arrayElement 
          }
    });
});

how can I add another field and value to this element?

if the element is for example:

{
a : 1
b: 2
}

I want to update it to be:

{
a : 1
b: 2
c: true
}

how can I accomplish this?

JohnBigs
  • 2,691
  • 3
  • 31
  • 61
  • arrayElement.c = true or arrayElement['c'] = true – Jan Mar 23 '20 at 16:31
  • What is your MongoDB version ? You need to use `arrayFilters` in MongoDB to do that :: https://stackoverflow.com/questions/51324876/arrayfilters-in-mongodb , Also what is this condition doing `doc.someOtherArray` - Check against another array elements in same doc ? This question is a bit confusing as it's written as of now, Please edit with sample docs, filter conditions & required o/p.. – whoami - fakeFaceTrueSoul Mar 23 '20 at 16:43

0 Answers0