Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

396
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda