且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更新不带ID的Cloud Firestore文档

更新时间:2023-02-14 09:19:47

Firestore只能更新其知道完整引用的文档,该文档需要文档ID.在当前结构上,您将必须运行查询以找到文档.像这样:

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({ ... }); 
    });
  });

如果您还有另一个唯一的属性,我总是建议您使用该属性作为文档的ID.这样,您将自动保证每个用户只能存在一个文档,并且省去了查询来查找文档的麻烦.

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.