Firesbase database batch upload

Viewed 34

Is it possible to upload data in batch in Firebase? Say I have a collection into which I want to store 100 documents with the same fields but different field values. I have the data ready in a CSV or other text format. Is there a way to upload this data in batches? One of the fields in the documents is of imageUrl type and is a reference (pathname) to an image in Cloud Storage. How can I download paths from Storage? Thank you for your help in advance. bests

1 Answers

Is it possible to upload data in batch in Firebase?

Indeed there is. You can execute multiple write operations as a single batch. Here are the docs:

https://firebase.google.com/docs/firestore/manage-data/transactions#batched-writes

As you can see, you can add up to 500 operations to a single batch. If you need more than that, then you should consider performing multiple batch operations.

Why would you use a batch and not individual writes? It's because of always having consistent data. If you do 500 operations without a batch, and for some reason, the 350'th operation fails, then you'll end up having only 350 out of 500 completed operations. Using a batch will ensure you that all the operations are complete, or failed with an exception. There won't be partial data or rolled-back operations.

One of the fields in the documents is of imageUrl type and is a reference (pathname) to an image in Firebase Storage. How can I download paths from Storage?

If you want to download the data associated with the URLs that you already have, then you should perform separate Cloud Storage calls. There is no way you can download them at the same time you perform the batch operations.

Related