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

145
Views
How to move MongoDB document fields to an array of objects?

Given a collection of documents similar to the following document

{
    "_id": {
        "$oid": "60582f08bf1d636f4b762ebc"
    }
    "experience": [{
        "title": "Senior Customer Success Account Manager",
        "company": "Microsoft",
        "tenure": 8
    }, {
        "title": "Senior Service Delivery Manager",
        "company": "Microsoft",
        "tenure": 34
    }],
    "company3": "Oracle",
    "tenure3": 10,
    "title3": "Senior Customer Success Manager - EMEA |Oracle Marketing Cloud"
}

How would I write an updateMany or other shell command to move company3, tenure3 and title3 inside the experience array as a new object {company: <company3 value>, title: <title3 value>, tenure: <tenure3 value>} ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Seems like you're looking for this aggregation update:

db.collection.update({},
[
  {
    $set: {
      experience: {
        $concatArrays: [
          "$experience",
          [
            {
              company: "$company3",
              title: "$title3",
              tenure: "$tenure3"
            }
          ]
        ]
      }
    }
  },
  {
    $unset: "company3"
  },
  {
    $unset: "tenure3"
  },
  {
    $unset: "title3"
  }
],
{
  multi: true
})

Playground: https://mongoplayground.net/p/xoEveE0rdBN

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!