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

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

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 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