I have a firestore database that stores some documents by their ID in multiple locations. I want to write a function that can access all instances of this document by ID and update/delete them without knowing exactly where they are.
For instance, in a shopping example with multiple users each having a subcollection with a cart, wishlist, and owned. The document ID may or may not exist in each one of the user doc's subcollections. I'm trying to write a function that iterates through these lists and performs the action upon matching the docID, yet have been unsuccessful in finding an example of this.
I understand how to add all references to a batch but not how to find them without a crazy amount of read/write operations over the entire user collection. Is this possible?
My initial thought is to:
let batch = admin.firestore().batch();
let allUsers = db.collection('users').get()
.then(snapshot => {
snapshot.forEach(doc => {
batch.delete(db.collection('users').doc(doc.id).collection('cart').doc(ID_TO_MATCH));
batch.delete(db.collection('users').doc(doc.id).collection('wishlist').doc(ID_TO_MATCH));
batch.delete(db.collection('users').doc(doc.id).collection('owned').doc(ID_TO_MATCH));
});
})
.catch(err => {
console.log('Error getting documents', err);
});
return batch.commit().then(function () {
// should delete all the direct referenes
});
This would work, as firestore returns a success for deletion of documents that don't exits. The obvious downside is A LOT of document reads as the number of users grow. Is there anyway to use .where()
queries to make this more efficient?