4

I am trying to update multiple records in an array in the document and entire collection at the same time.

My data is like this:

  comments : [ 
            {
                "text" : "Good one",
                "_id" : ObjectId("5722101611a53cca08544d25"),
                "author" : {
                    "id" : "123",
                    "name" : null,
                    "photo" : null
                }
            },
{
                "text" : "nice one",
                "_id" : ObjectId("5722103511a53cca08544d24"),
                "created" : ISODate("2016-04-28T13:29:25.444Z"),
                "author" : {
                    "id" : "123",
                    "name" :null,
                    "photo":null
                }
            },
             {
            "text" : "Good one",
            "_id" : ObjectId("5718768d98eb065c035b645"),
            "author" : {
                "id" : "456"
            }
        } 

        ]

In the above comments array there are two records with author.id=123 I want to update those two at a time. My logic is as follows :

Event.update({'comments.author.id':_id},
                { $set:{ 
                  "comments.$.author.name":username,
                  "comments.$.author.photo":photoUrl
                  }
                },{multi:true},(err,commentupdate)=>{
                console.log("commentupdate:"+JSON.stringify(commentupdate))
              })

From the above query it is updating the very first record in that array and the second record is not updating.Please suggest me any solution to update both records at a time.

Mahesh
  • 395
  • 1
  • 2
  • 18

0 Answers0