Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

395
Views
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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!