javascriptfirebasegoogle-cloud-firestore

is there a way to update docs from a query without having to loop through the results?


I'm trying to update a field in all the docs returned by a query. The code below works but I was wondering if there's a way to accomplish this without having to loop the result of the query and running updateDoc on every item.

    const query_ref = query(
        collection(db, "somecollection"),
        where("something_id", "==", this.something_id)
    );
    const snapshot = await getDocs(query_ref);
    for (const doc of snapshot.docs) {
        const ref = doc(db, "somecollection", doc.id);
        await updateDoc(ref, {field: value});
    }

Solution

  • Firestore doesn't support so-called update queries, where you pass the conditions and the new values in a single API call. So you'll have to indeed first execute the query, and then update each document as you do now.

    Also see: