Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

325
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!