update cloud firestore document without id

后端 未结 1 1583
不知归路
不知归路 2020-12-12 04:47

My Cloud Firestore looks like this:

users
  ├────random_id_1───{name, email, ...}
  ├────random_id_2───{name, email, ...}
 ...
  └────random_id_n───{name, em         


        
1条回答
  •  囚心锁ツ
    2020-12-12 05:21

    Firestore can only update documents for which it knows the complete reference, which requires the document ID. On your current structure, you will have to run a query to find the document. So something like:

    firebase.firestore().collection("users")
      .where("name", "==", "Daniel")
      .get(function(querySnapshot) {
        querySnapshot.forEach(function(document) {
         document.ref.update({ ... }); 
        });
      });
    

    If you have another attribute that is unique, I'd always recommend using that as the IDs for the documents. That way you're automatically guaranteed that only one document per user can exist, and you save yourself having to do a query to find the document.

    0 讨论(0)
提交回复
热议问题