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

104
Visualizações
MongoDB javascript string equality condition

I have a mongodb query or script that will update all employee team project and will change the team project to "DUMMY" only when the current project is "bbb234". I got a weird behavior since there are some project (bbb234) which are not changed to "DUMMY". I am not sure if my condition is wrong but only the first 10 or 12 records are changed. Only late records are not changed even when the team project is "bbb234".

To understand what I meant, please see the script below.

db.getCollection('employees').find(
    { "teams.project" : {
            $in: [ "aaa123", "bbb234" ]
      }
    }
).forEach(e => {
    // skip when there are no teams
    if (!e.teams) {
        return;
    }

    e.teams.forEach(team => {

         if (team.project === "bbb234") {
             team.project = 'DUMMY';
         }

        // This is OK, it will display 'DUMMY' for all team with previous project value of `bbb234`
        // print(team.project); 

        // When displaying team, the condition is `team.project === "bbb234"` is not always true
        // The first 10 employee records have updated team project, 
        // while after that...  the other records still show team.project = "bbb234"
        // print(team);
    });

    // Update employee with the corrected/updated team project
    db.getCollection('employees').updateOne({"_id": e._id}, {$set: {"teams": e.teams}});
})

When I check the records, teams.project value is String. I am not sure where in my script is wrong or not able to handle the condition correctly. I know this is pretty easy but I cannot figure out where I did wrong and why only the early records are updated/changed.

Update:

I tried .toArray() but still I am getting the same error/issue. I really find this wierd

var _employees = db.getCollection('employees').find(
    { "teams.project" : {
            $in: [ "aaa123", "bbb234" ]
      }
    }
).toArray();

_employees .forEach(e => {
    // skip when there are no teams
    if (!e.teams) {
        return;
    }

    e.teams.forEach(team => {

         if (team.project === "bbb234") {
             team.project = 'DUMMY';
         }

        // Still, this is OK
        // print(team.project); 

        // Same issue as with the previous query/script
        // print(team);
    });

    // Update employee with the corrected/updated team project
    db.getCollection('employees').updateOne({"_id": e._id}, {$set: {"teams": e.teams}});
})
about 4 years ago · Santiago Gelvez
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