Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

397
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda