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

158
Vistas
MongoDB - bulk updating values in arrays of nested objects

I have a collection in MongoDB that represents the chores kids needs to do each day and whether they are done. Here is an example of a document.

{
  "_id":"2022-02-13:joe",
  "kidName":"Joe",
  "chores": [
    {"chore":"Breakfast: clear dishes and utensils","done":true}, 
    {"chore":"Clearing dishes after meals","done":true},
    {"chore":"Put away coats, knapsack, and shoes","done":true}, 
    {"chore":"Clear toys","done":true}
   ],
    "date": 2022-02-13T00:00:00.000+00:00
  }

I want to change it so that, instead of a Boolean, done is an integer, where:

Meaning value
Done 1
Not Done -1
Unmarked 0

Is there a pipeline or trick in Compass to do this for all documents in the collection?

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

0

You could use the update method with aggregation pipeline to update the chores array. You would need to use the following pipeline to achieve the desired results:

db.collection.updateMany(
    { 'chores.0': { '$exists': true } },
    [
        { '$set': {
            'chores': {
                '$map': {
                    'input': '$chores',
                    'as': 'chore',
                    'in': {
                        '$mergeObjects': [
                            '$$chore',
                            { 
                                'done': {
                                    '$switch': {
                                        'branches': [
                                            { 'case': { $eq: [ '$$chore.done', true ] }, then: 1 },
                                            { 'case': { 
                                                $eq: [ 
                                                    { $ifNull: ['$$chore.done', 'Unmarked'] }, 
                                                    'Unmarked' 
                                                ] }, 
                                                then: 0 
                                            },
                                            { 'case': { $eq: [ '$$chore.done', false ] }, then: -1 },
                                        ],
                                        default: 0
                                    }
                                }
                            }
                        ]
                    }
                }
            } 
        } }
    ]
)
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