0

I have an Excel sheet that contains 5 columns of data that I would like to add to a Firestore collection.

In order to do so, I wrote a script that reads the Excel and creates 5 ArrayLists that describes each column.

In the end, I have 5 ArrayLists with around 27,000 items in each one of them.

I had like to create now 27,000 documents in that collection.

How can I iterate those 27,000 items to create 27,000 documents?

Basically what I want to do is:

private void addData2DB(ArrayList<String> titles, ArrayList<String> authors,ArrayList<String> publishers, ArrayList<String> genres, ArrayList<String> pages){

    for (int i = 0;  i < titles.size(); i++) {
        Map<String, Object> data = new HashMap<>();
        data.put("title", titles.get(i));
        data.put("author", authors.get(i));
        data.put("publisher", publishers.get(i));
        data.put("genre", genres.get(i));
        data.put("pages", pages.get(i));

        db.collection("users")
                .add(data)
                .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                    @Override
                    public void onSuccess(DocumentReference documentReference) {
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                    }
                });
    }
    
}

I read there is a WriteBatch but it says it support only up to 500 writes.

How can I overcome this situation?

Thank you

Ben
  • 1,737
  • 2
  • 30
  • 61
  • 1
    Does this answer your question? [What is the fastest way to write a lot of documents to Firestore?](https://stackoverflow.com/questions/58897274/what-is-the-fastest-way-to-write-a-lot-of-documents-to-firestore) – Roger May 10 '21 at 07:53

1 Answers1

1

I read there is a WriteBatch but it says it supports only up to 500 writes.

Yes, only 500 writes are currently allowed. What you can do is write 500 documents at a time, and continue in this way until you have all 27k documents added on Firebase servers. The best/fastest way in which you can perform these write operation is explained by Frank van Puffelen in the following answer:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193