When I try to batch insert records in intervals of 450 (I chose this to make sure I was below the 500 firestore limit) I get an error for an invalid argument. Below is the exact code I am using.
I am using firestore within react-native with the below dependencies and versions:
"@react-native-firebase/app": "^19.1.0",
"@react-native-firebase/firestore": "^19.1.0",
"react-native": "^0.73.5",
I am inserting records into a sub collection that does not yet exist. user_tracks
is the top level collection that exists and the document id is a record indicator that is just used to link the top and sub collection. The document id corresponds to an existing document id under the users collection and finally the sub collection tracks
is what I am ultimately trying to insert into.
export const updateUserLikedSongs = async (documentId, likedSongs) => {
// Batch write 500 at a time
console.log(`There are ${likedSongs.length} songs to insert.`);
if (likedSongs.length <= 0) {
console.log("No more documents to insert.");
return true;
}
const batchSize = 450; // Firestore batch size limit
var batch = firestore().batch();
likedSongs.slice(0, batchSize).forEach((song) => {
const docRef = firestore().collection("user_tracks").doc(documentId).collection("tracks").doc();
batch.set(docRef, song); // Use song directly, no need for Object.assign
});
try {
await batch.commit();
console.log(`Successfully wrote ${batchSize} records.`);
// Recursively call updateUserLikedSongs with the remaining songs
return updateUserLikedSongs(documentId, likedSongs.slice(batchSize));
} catch (error) {
console.error("Error while batch writing:", error);
throw error; // Rethrow the error to be caught by the caller
}
};
I am able to loop through my liked songs array and insert them into the sub collection 1 at a time, however this seems extremely in-efficient and I would like some help to figure out what I am doing wrong with the batch insert. This method also throws a warning because of the number of promises waiting to resolve.
export const updateUserLikedSongs = async (documentId, likedSongs) => {
likedSongs = likedSongs.slice(0, 750)
// Batch write 500 at a time
console.log(`There are ${likedSongs.length} songs to insert.`);
const batchSize = 499; // Firestore batch size limit
const batches = [];
for (let i = 0; i < likedSongs.length; i += batchSize) {
const batch = likedSongs.slice(i, i + batchSize);
const batchPromises = batch.map(async (track) => {
const docRef = firestore()
.collection(`user_tracks/${documentId}/tracks`)
.doc();
await docRef.set(track); // Set each record in Firestore
});
batches.push(Promise.all(batchPromises));
}
try {
// Wait for all batches to complete
await Promise.all(batches);
console.log("All records created successfully.");
} catch (error) {
console.error("Error creating records:", error);
}
};
I have tried this on android and ios through react-native though I think that doesn’t matter since the javascript code and dependencies are ultimately the same.
Please offer some tips and guidance.