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

322
Views
How to convert structure of mongo db document's object

I have a document with the following structure:

id:  6229d3fd40bac327243e4ww2
userID: "594e710c-c632-3ae-929a-f13e294ee6ef"
appID: "622994e2b98225609b89affd"
notifications:Object
     6229d3e040bac3272qwe4441: 1  
     622ew3e041bac3272qwe4441: 1

I want to update this structure to this:

id:  6229d3fd40bac327243e4ww2
userID: "594e710c-c632-43ae-929a-f13e294ee6ef"
appID: "622994e2b98225609b89affd"
notifications:Object
     6229d3e040bac3272qwe4441: {a:1, b:false}  
     622ew3e040bac3272qwe4441: {a:1, b:false}

I am having trouble with iterating the notification objects fields as its is random hex string. I have tried using following solutions but not able to get the expected result MongoDB Aggregation pipeline: use of For loop

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

db.collection.update({},
[
  {
    $set: {
      notifications: {
        $arrayToObject: {
          $map: {
            input: { $objectToArray: "$notifications" },
            as: "n",
            in: {
              k: "$$n.k",
              v: { a: "$$n.v", b: false }
            }
          }
        }
      }
    }
  }
],
{
  multi: true
})

mongoplayground

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!