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

155
Views
How can I add data to a firestore array without overwriting it's previous content?

I'm trying to store book Ids in the books array in firestore. every time I add a new book it overwrites the first one.

await updateDoc(
  doc(dataBase, "users", user.uid),
  {
    currentlyReading: {
      arrayUnion(bookId)
    }
  },
  { merge: true }
);

I'm using updateDoc as stated in the documentation + merge: true, but it only stores one element. is there any way I can keep adding elements without overwriting? Thank you in advance.

this is a screen shot of the Document enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The { merge: true } prevents from existing fields from being removed when using setDoc() and doesn't work for array elements. There's no need for that option when using updateDoc(). One way would be to use arrayUnion() add new elements to the array.

import { doc, updateDoc, arrayUnion } from "firebase/firestore"

await updateDoc(doc(dataBase, "users", (user.uid)), {
  currentlyReading:{
    books: arrayUnion(bookId)
  }            
})
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!