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

504
Vistas
How to update objects in list in mongoose (in NodeJS)

I have an object with the following model:

export const Address = mongoose.model('Address',
    {   
        id: mongoose.SchemaTypes.ObjectId,
        customer_id: String,
        addresses: [{
            address_type: String,
            address_info: String,
        }]
    });

An example dataset is like this:

{
  "data": {
    "address": [
      {
        "customer_id": "12345123",
        "addresses": [
        {
            "address_type": ANDROID,
            "address_info": "dfjghjsdgf"
        },
        {
            "address_type": IOS,
            "address_info": "dwrw45345f"
        }
        ]
      },
      {
        "customer_id": "12345124",
        "addresses": [
        {
            "address_type": SMS,
            "address_info": "dfjghj231dgf"
        },
        {
            "address_type": IOS,
            "address_info": "ww242344234"
        }
        ]
      }
    ]
  }
}

It's clear that the addresses field of the Address model is a list, within which each object has two fields : address_type, and address_info.

I wonder how to update an address_info with filters in customer_id and address_type? For example, if I would like to update the ANDROID address of customer_id = 12345123 to abcdefg, it's expected to see the database being updated into :

{
  "data": {
    "address": [
      {
        "customer_id": "12345123",
        "addresses": [
        {
            "address_type": ANDROID,
            "address_info": "abcdefg"
        },
        {
            "address_type": IOS,
            "address_info": "dwrw45345f"
        }
        ]
      },
      {
        "customer_id": "12345124",
        "addresses": [
        {
            "address_type": SMS,
            "address_info": "dfjghj231dgf"
        },
        {
            "address_type": IOS,
            "address_info": "ww242344234"
        }
        ]
      }
    ]
  }
}

Reference: Mongoose :Find and filter nested array This problem talked about findOne, but I'm more interested in update.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

you can try like this

await Address.update(
   { customer_id: "12345123", "addresses.address_type": "ANDROID" },
   {$set : { "addresses.$.address_info" : "abcdefg" }} 
)

playground

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