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

122
Vistas
How to set values of nested array of objects in mongodb

I have a data model which looks like this, so each documents has services array and each service contains an items array and I want to update properties in items array.

{
    services: [
        {
            id: '1',
            name: 'Service 01',
            items: [
                {
                    id: '1',
                    name: '',
                    qty: 10
                },
                {
                    id: '2',
                    name: '',
                    qty: 10
                },
            ]
        },
        {
            id: '2',
            name: 'Service 02',
            items: [
                {
                    id: '3',
                    name: '',
                    qty: 10
                },
                {
                    id: '4',
                    name: '',
                    qty: 10
                },
            ]
        },
    ]
}

I want to set all the quantities inside services -> items to 0, What will be query for doing it I tried to do,

updateMany({}, { $set: { 'services.items.qty': 0 } });

but it's not working. Let me know if you need more details.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

the all positional operator $[] operator can be used to update all elements

playground

db.collection.update({},
{
  $set: {
    "services.$[].items.$[].qty": 0
  }
})
about 4 years ago · Juan Pablo Isaza 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