1

I got the following Firebase document:

{
  "customField1": "customValue1",
  "customField2": "customValue2",
  "fieldToChange: "valueToChange"
}

I wish to know how to do two operations:

  1. Find the document which has "customField1" set to "customValue1" and update "valueToChange" to something else.

  2. Find the document which has "customField1" set to "customValue1" AND "customField2" set to "customValue2" and update "valueToChange" to something else.

How can I achieve this?

I only see options where you need the docId:

const uploadCollection = this.db.collection<any>('uploads');
    return await uploadCollection.doc(docId).update({ 'fieldToChange': 'another value' });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
robtot
  • 863
  • 1
  • 9
  • 31
  • There is no concept of update queries in Firestore. To update a document, you need to know the full path to that document. So if you want to update documents that match a certain condition, you'll need to execute a query for that condition, loop over all resulting documents, and update each of them. See https://stackoverflow.com/questions/54541228/update-cloud-firestore-document-without-id/54542410#54542410 – Frank van Puffelen Oct 28 '19 at 17:18

1 Answers1

4

Some form of this is probably what you're looking for:

var uploads = db.collection("uploads").where("customField1", "==", "customValue1");

uploads.get().then(querySnapshot => {
    querySnapshot.forEach(doc => {
        doc.ref.update({ 'fieldToChange': 'another value' });
    });
});

see here for more info: https://firebase.google.com/docs/firestore/query-data/queries

Chris Sandvik
  • 1,787
  • 9
  • 19