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

341
Views
MONGO : How to update a field using value of another field

I have a collection of people:

{
   name: Juan,
   age: 21
}

and now , I want to achieve the following:

{
   name: Juan,
   age: 21,
   name_reply:Juan
}

I have tried using:

db.people.updateMany(
    {"name_reply":null},
    [{$set:{name_reply:"$name"}}]
);

but the following appears. Expected type object but found array.

How could I update a mongo field using the value of another field?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

  db.collection.update({
        "name_reply": null
    },
   [
    {
     "$set": {
        "name_reply": "$name"
     }
    }
  ],
   {
     multi: true
   })

But indeed this is only for mongodb version >=4.2 where you can provide aggregation pipeline to the update query

playground

for earlier versions you can check this answer here

something like this could do the job most probably:

db.collection.find({"name_reply":null}).snapshot().forEach( 
              function (doc) { 
                 doc.name_reply = doc.name; 
                 db.collection.save(doc); 
               }
             );
over 4 years ago · Santiago Trujillo 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!