Ben
Ben

Reputation: 1837

Write large batch of documents to firestore

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

Upvotes: 0

Views: 359

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

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:

Upvotes: 1

Related Questions