Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
How do you add multiple documents at once to a collection in Firestore?

I am somewhat new to Firebase and looking for the ability to write multiple JSON objects into a collection under random IDs in one operation. I am just using firestore and auth(not shown) along with react.

So far I have

Function TestDocs () {
    {addDoc(collection(db, 'randomData'),  {**object 1 example data**},{**object 2 more example data**}
   )
}

I have a button on a webpage which adds the data into the DB. When I check Firestore only the first object is loaded and not the second... on top of this, assuming that I wanted to add hundreds of objects(not just 2), how would I go about this?

I was looking through the documentation for Firestore batch writes but it only lists set(), update() and delete() as permitted operations so I am not sure how it would work. Thanks in advance

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You'll want to do this in two steps:

  1. Create a reference to a new document with a new unique ID:

    // Generate two new document references with a generated ids
    const newRef1 = doc(collection(db, "randomData"));
    const newRef2 = doc(collection(db, "randomData"));
    
  2. Write to that reference in your batch or transaction.

Also see:

  • Firestore - batch.add is not a function (which contains the older namespaced syntax for doing the same).
  • The last snippet in the documentation on adding a document, which shows this approach.
about 4 years ago · Juan Pablo Isaza Relatório

0

I found another way to do this without using Firebases batch function. Each transaction or batch of writes can write to a maximum of 500 documents. I was looking to update my app with more than 500 and found a video on YouTube showing another way to do this.

I had to tweak the script a bit to get what I needed but the idea is there.

https://www.youtube.com/watch?v=Qg2_VFFcAI8

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda