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

342
Views
Mongo DB: Append a value to a nested array

I think there might be better ways to store data but the app I am working on has some data stored like this.

I have a document with nested arrays. In this particular example, would it be possible to append a new element "value3" to an array inside products whose "productId" matches the condition. So, basically look for productId and append "value3" to the array which has matching "productId".

I am just learning about MongoDB so if you can provide some suggestions for changing schema, even that would be very helpful.

P.S. Not sure if it helps but I am using node.js

{"_id":
    {"$oid":"601baf0e5c307422c0fa958c"},
    "sale":"Test Sale",
    "products":[
      [
        ["productId","value1","value2"],
        ["productId","value1","value2"]
      ],
      [
        ["productId","value1","value2"],
        ["productId","value1","value2"]
      ],
      [
        ["productId","value1","value2"],
        ["productId","value1","value2"]
      ]
    ],
    "collectionProducts":["id","id","id"]
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Maybe something like this:

 db.store.update({},{$push:{"products.$[].$[k]":"value3"}},{ arrayFilters: [{ k:'productId' }]})
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!