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

107
Views
2D array Firestore using ArrayUnion

I'm trying to create a 2D array in firestore, which can be updated using array union, but am struggling to work out how to do this. I can do it in a 1D array easily with this:

db.collection('subjects').doc('exampleDoc').update({ units: firestore.FieldValue.arrayUnion({ "name": "Example Unit", "id": "123456787654323456765" }) });

Which produced something like this:

name: 'Example Subject',
units: [
    {
        id: '123456787654323456765',
        name: 'Example Unit'
    }
]

But how can I do this with a 2D array with the data below as an example document?

name: 'Example Subject',
units: [
    {
        id: '123456787654323456765',
        name: 'Example Unit',
        subunits: [
            {
                id: '234568765434567',
                name: 'Example Subunit'
            }
        ]
    }
]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You won't be able to do this with a simple arrayUnion. That's because Firestore does not support syntax to locate a specific indexed array element like it can with named, nested fields.

What you'll have to do is read the document, modify the field in memory to make it look the way you want, then update that entire field back to the document.

about 4 years ago · Juan Pablo Isaza 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!