Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

Update String inside object inside array mongodb in nodejs ?

By M.K. Verma · 2 May 2022

Update String inside object inside array mongodb in nodejs ?

Try This 

db.collection.update({
  "_id": ObjectId("000000000000000000000001"),
  "side1.username": "test1"
},
{
  "$set": {
    "side1.$.id": "43242342"
  }
})

Try it on

Example updated document:

[
  {
    "_id": ObjectId("000000000000000000000001"),
    "side1": [
      {
        "id": "43242342",
        "username": "test1"
      },
      {
        "id": "id2",
        "username": "test2"
      },
      {
        "id": "id3",
        "username": "test3"
      }
    ],
    "side2": [
      {
        "id": "id4",
        "username": "test4"
      },
      {
        "id": "id5",
        "username": "test5"
      },
      {
        "id": "id6",
        "username": "test6"
      }
    ]
  }
]