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

323
Vistas
In updateMany, update the field only if field exists, and do not create a new key

So what I want to do inside the $set is check if the field exists and then update the value, but only if this field exists. The $cond is very close to what I want to achieve but the problem is I can't stop it from creating the field.

I could filter to get only the existing field, but in this case I would have to update field by field instead of all at the same time, and this would take forever.

 const data = await connection.collection("snapshot").updateMany({}, [
    {
      $set: {
        field1: {
          $cond: [
            { field1: { $exists: true } },
            { field1: "newValue" },
            undefined, // do nothing
          ],
        },
        field2: {
          $cond: [
            { field2: { $exists: true } },
            { field2: "newValue" },
            undefined, // do nothing
          ],
        },
        field3: {
          $cond: [
            { field3: { $exists: true } },
            { field3: "newValue" },
            undefined, // do nothing
          ],
        },
      },
    },
  ]);

Am I missing something or is just impossible to do?

I'm using mongo 5.0

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

0

Operator $exists works only in find, not in Aggregation Pipelines. Checking for "field exists" is not trivial, see Or with If and In mongodb

I assume you like to update documents like { field3: "newValue" }, your update will do { field3: { field3: "newValue" } }

Over all it would be this one:

connection.collection("snapshot").updateMany({}, [
   {
      $set: {
         field1: {
            $cond: [
               { $ne: [{ $type: "$field1" }, "missing"] },
               "newValue",
               "$$REMOVE" // do nothing
            ]
         },
         field2: {
            $cond: [
               { $ne: [{ $type: "$field2" }, "missing"] },
               "newValue",
               "$$REMOVE" // do nothing
            ]
         },
         field3: {
            $cond: [
               { $ne: [{ $type: "$field3" }, "missing"] },
               "newValue",
               "$$REMOVE" // do nothing
            ]
         }
      }
   }
])
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