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

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