Monday, 22 May 2017

how to update inside array in mongo

I have this collection

"roomid" : "590ba5386431210c688d07555910cff9cdf7b10bfc309f8d",
"messages": [
      {
        "messagestring": "hello",
        "sender": "590ba5386431210c688d0755",
        "time": "05:21:53",
        "date": "2017-05-23",
        "status": "unread",
        "messageimage": []
      },
      {
        "messagestring": "whatsup",
        "sender": "5910cff9cdf7b10bfc309f8d",
        "time": "05:23:52",
        "date": "2017-05-23",
        "status": "unread",
        "messageimage": []
      }
    ]

I want to update all status "unread" value into "read" value What I did was this code below, but it only update the first status

        Messages.update({roomid: roomid, 'messages.status': 'unread'},
            { '$set':  { 'messages.$.status': 'read' }},
            (err, result) => {
                if (err) {
                  console.log({ error: 'Unable to update status. due to ' + err, });
                } else {
                  console.log(result);
                }
            }
        );

my problem is how will I update all the status field that contains "unread" value into "read" value.



via JerVi

No comments:

Post a Comment