AshanD
AshanD

Reputation: 527

Mongoose findOneAndUpdate inside nested Arrays

I need to update the type, key and values in the 'fields' array if the componentId and the wfInstanceId id matches.

This is the document that i need to do so.

{
    "_id": {
        "$oid": "586b6d756937c22f207dd5af"
    },
    "wfInstanceId": "0111",
    "workflowId": "ash3",
    "folderURL": "ash3",
    "teamName": "teamName",
    "dataStream": [
        {
            "componentInstanceId": "componentInstanceId1",
            "componentId": "componentId1",
            "_id": {
                "$oid": "586b6d756937c22f207dd5b0"
            },
            "fields": [
                {
                    "type": "String",
                    "value": "value1",
                    "key": "key",
                    "_id": {
                        "$oid": "586b6d756937c22f207dd5b1"
                    }
                },
                {
                    "type": "String",
                    "value": "value2",
                    "key": "key1",
                    "_id": {
                        "$oid": "586b6d756937c22f207dd5b1"
                    }
                }
            ]
        },
        {
            "componentInstanceId": "componentInstanceId2",
            "componentId": "componentId22",
            "_id": {
                "$oid": "586b6d756937c22f207dd5b0"
            },
            "fields": [
                {
                    "type": "String",
                    "value": "value1",
                    "key": "key",
                    "_id": {
                        "$oid": "586b6d756937c22f207dd5b1"
                    }
                },
                {
                    "type": "String",
                    "value": "value2",
                    "key": "key2",
                    "_id": {
                        "$oid": "586b6d756937c22f207dd5b1"
                    }
                }
            ]
        }
    ],
    "id": "38f356f0-d196-11e6-b0b9-3956ed7f36f0",
    "__v": 0
}

I tried this like this, Also i tried $set over $push which is also doesn't work.

Model.findOneAndUpdate( {'wfInstanceId': '0111', 'dataStream.componentId': 'componentId1'}, {$push : { 'dataStream.fields.$.key' : 'sdsdds' }}, { upsert: true }, function (err, data) {
                if (err) {
                    reject(err);
                    console.log('error occured' + err);
                }
                console.info("succesfully saved");
                resolve (data);
            });

As they describe in this DOC it shouldbe working for update method that is also didn't work for me. Any help will be really appreciated to overcome this issue i'm facing.

Upvotes: 1

Views: 2495

Answers (1)

Simran
Simran

Reputation: 2810

As you are using $ operator it updates only the first matched array element in each document.

As of now it is not possible in mongoose to directly update all array elements.

You can do this in your case:

db.collection.find({'wfInstanceId': '0111', 'dataStream.componentId': 'componentId1'})
  .forEach(function (doc) {
    doc.datastream.forEach(function (datastream) {
      if (dataStream.componentId === componentId1) {
        dataStream.fields.forEach(function(fields){
         // you can also write condition for matching condition in field
           dataStream.fields.key="";
           dataStream.fields.value="";
           dataStream.fields.type="";
         }
      }
    });
    db.collection.save(doc);
  }); 

It is normal javascript code. I think it's clearer for mongo newbies.

Upvotes: 2

Related Questions