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

232
Views
MongoDB update - set field from nested field

I want to set a field to a value of a nested field

given

{
  "_id":"myId",
  "data":{
    "id":"asdfasdfasdf",
    "text":"Wonderful text"
  }
}

expected

{
  "_id":"myId",
  "messageId":"asdfasdfasdf",
  "data":{
    "id":"asdfasdfasdf",
    "text":"Wonderful text"
  }
}

Is there a possibility to do something like this?

db.myCollection.updateMany({},{ $set: {"messageId": "$data.id"} },false,true)

I am using MongoCompass -> _MongoSH

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Yes, it's possible doing this using pipelined updates, added at version 4.2.

This is how you use it:

db.collection.updateMany(
{},
[
  {
    $set: {
      "message": "$data.id"
    }
  }
])

Mongo Playground

For lesser Mongo versions you have to read the document first and then use the values for the update.

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!