且构网

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

更新没有 ID 的云 Firestore 文档

更新时间:2023-02-14 08:57:20

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()
  .then(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.